diff --git a/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java index 8023500f92..daec128c11 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java @@ -110,7 +110,7 @@ public PhpClientCodegen() { instantiationTypes.put("array", "array"); instantiationTypes.put("map", "map"); - + // provide primitives to mustache template List sortedLanguageSpecificPrimitives= new ArrayList(languageSpecificPrimitives); Collections.sort(sortedLanguageSpecificPrimitives); @@ -295,6 +295,12 @@ public void processOpts() { if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) { this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION)); } + if (StringUtils.isBlank(composerVendorName) && additionalProperties.get(CodegenConstants.GIT_USER_ID) != null) { + additionalProperties.put(CodegenConstants.GIT_USER_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_USER_ID).toString())); + } + if (StringUtils.isBlank(composerProjectName) && additionalProperties.get(CodegenConstants.GIT_REPO_ID) != null) { + additionalProperties.put(CodegenConstants.GIT_REPO_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_REPO_ID).toString())); + } additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); diff --git a/src/main/resources/handlebars/php/.php_cs b/src/main/resources/handlebars/php/.php_cs index 6b8e23c818..4fbe53ec5f 100644 --- a/src/main/resources/handlebars/php/.php_cs +++ b/src/main/resources/handlebars/php/.php_cs @@ -1,18 +1,23 @@ level(Symfony\CS\FixerInterface::PSR2_LEVEL) +return PhpCsFixer\Config::create() ->setUsingCache(true) - ->fixers( - [ - 'ordered_use', - 'phpdoc_order', - 'short_array_syntax', - 'strict', - 'strict_param' - ] - ) - ->finder( - Symfony\CS\Finder\DefaultFinder::create() - ->in(__DIR__) + ->setRules([ + '@PSR2' => true, + 'ordered_imports' => true, + 'phpdoc_order' => true, + 'array_syntax' => [ 'syntax' => 'short' ], + 'strict_comparison' => true, + 'strict_param' => true, + 'no_trailing_whitespace' => false, + 'no_trailing_whitespace_in_comment' => false, + 'braces' => false, + 'single_blank_line_at_eof' => false, + 'blank_line_after_namespace' => false, + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->exclude('test') + ->exclude('tests') + ->in(__DIR__) ); diff --git a/src/main/resources/handlebars/php/HeaderSelector.mustache b/src/main/resources/handlebars/php/HeaderSelector.mustache index 909beb134d..72f585b46c 100644 --- a/src/main/resources/handlebars/php/HeaderSelector.mustache +++ b/src/main/resources/handlebars/php/HeaderSelector.mustache @@ -97,4 +97,3 @@ class HeaderSelector } } } - diff --git a/src/main/resources/handlebars/php/ObjectSerializer.mustache b/src/main/resources/handlebars/php/ObjectSerializer.mustache index 7dac80ddae..dff5417ab1 100644 --- a/src/main/resources/handlebars/php/ObjectSerializer.mustache +++ b/src/main/resources/handlebars/php/ObjectSerializer.mustache @@ -33,11 +33,12 @@ class ObjectSerializer * Serialize data * * @param mixed $data the data to serialize + * @param string $type the SwaggerType of the data * @param string $format the format of the Swagger type of the data * * @return string|object serialized form of $data */ - public static function sanitizeForSerialization($data, $format = null) + public static function sanitizeForSerialization($data, $type = null, $format = null) { if (is_scalar($data) || null === $data) { return $data; @@ -48,6 +49,11 @@ class ObjectSerializer $data[$property] = self::sanitizeForSerialization($value); } return $data; + } elseif ($data instanceof \stdClass) { + foreach ($data as $property => $value) { + $data->$property = self::sanitizeForSerialization($value); + } + return $data; } elseif (is_object($data)) { $values = []; $formats = $data::swaggerFormats(); @@ -57,7 +63,7 @@ class ObjectSerializer if ($value !== null && !in_array($swaggerType, [{{&primitives}}], true) && method_exists($swaggerType, 'getAllowableEnumValues') - && !in_array($value, $swaggerType::getAllowableEnumValues())) { + && !in_array($value, $swaggerType::getAllowableEnumValues(), true)) { $imploded = implode("', '", $swaggerType::getAllowableEnumValues()); throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'"); } @@ -108,23 +114,22 @@ class ObjectSerializer * later. * * @param string[]|string|\DateTime $object an object to be serialized to a string - * @param string|null $format the format of the parameter * * @return string the serialized object */ - public static function toQueryValue($object, $format = null) + public static function toQueryValue($object) { if (is_array($object)) { return implode(',', $object); } else { - return self::toString($object, $format); + return self::toString($object); } } /** * Take value and turn it into a string suitable for inclusion in * the header. If it's a string, pass through unchanged - * If it's a datetime object, format it in RFC3339 + * If it's a datetime object, format it in ISO8601 * * @param string $value a string which will be part of the header * @@ -138,7 +143,7 @@ class ObjectSerializer /** * Take value and turn it into a string suitable for inclusion in * the http body (form parameter). If it's a string, pass through unchanged - * If it's a datetime object, format it in RFC3339 + * If it's a datetime object, format it in ISO8601 * * @param string|\SplFileObject $value the value of the form parameter * @@ -156,18 +161,16 @@ class ObjectSerializer /** * Take value and turn it into a string suitable for inclusion in * the parameter. If it's a string, pass through unchanged - * If it's a datetime object, format it in RFC3339 - * If it's a date, format it in Y-m-d + * If it's a datetime object, format it in ISO8601 * * @param string|\DateTime $value the value of the parameter - * @param string|null $format the format of the parameter * * @return string the header string */ - public static function toString($value, $format = null) + public static function toString($value) { - if ($value instanceof \DateTime) { - return ($format === 'date') ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM); + if ($value instanceof \DateTime) { // datetime in ISO8601 format + return $value->format(\DateTime::ATOM); } else { return $value; } @@ -276,7 +279,7 @@ class ObjectSerializer return new \SplFileObject($filename, 'r'); } elseif (method_exists($class, 'getAllowableEnumValues')) { - if (!in_array($data, $class::getAllowableEnumValues())) { + if (!in_array($data, $class::getAllowableEnumValues(), true)) { $imploded = implode("', '", $class::getAllowableEnumValues()); throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'"); } diff --git a/src/main/resources/handlebars/php/api_test.mustache b/src/main/resources/handlebars/php/api_test.mustache index b2561cfe89..c381754766 100644 --- a/src/main/resources/handlebars/php/api_test.mustache +++ b/src/main/resources/handlebars/php/api_test.mustache @@ -21,6 +21,7 @@ namespace {{invokerPackage}}; use {{backslash}}{{invokerPackage}}\Configuration; use {{backslash}}{{invokerPackage}}\ApiException; use {{backslash}}{{invokerPackage}}\ObjectSerializer; +use PHPUnit\Framework\TestCase; /** * {{classname}}Test Class Doc Comment @@ -30,37 +31,38 @@ use {{backslash}}{{invokerPackage}}\ObjectSerializer; * @author Swagger Codegen team * @link https://github.com/swagger-api/swagger-codegen */ -{{#operations}}class {{classname}}Test extends \PHPUnit_Framework_TestCase +{{#operations}}class {{classname}}Test extends TestCase { /** * Setup before running any test cases */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { } /** * Setup before running each test case */ - public function setUp() + public function setUp(): void { } /** * Clean up after running each test case */ - public function tearDown() + public function tearDown(): void { } /** * Clean up after running all test cases */ - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { } {{#operation}} + {{#contents}} /** * Test case for {{{operationId}}} @@ -68,9 +70,10 @@ use {{backslash}}{{invokerPackage}}\ObjectSerializer; * {{{summary}}}. * */ - public function test{{vendorExtensions.x-testOperationId}}() + public function test{{vendorExtensions.x-testOperationId}}{{#isForm}}Form{{/isForm}}() { } + {{/contents}} {{/operation}} } {{/operations}} diff --git a/src/main/resources/handlebars/php/composer.mustache b/src/main/resources/handlebars/php/composer.mustache index 0a7732fafe..1d9038cf7d 100644 --- a/src/main/resources/handlebars/php/composer.mustache +++ b/src/main/resources/handlebars/php/composer.mustache @@ -8,6 +8,7 @@ "swagger", "php", "sdk", + "rest", "api" ], "homepage": "http://swagger.io", @@ -19,16 +20,16 @@ } ], "require": { - "php": ">=5.5", + "php": "^7.4 || ^8.0", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/guzzle": "^7.3", + "guzzlehttp/psr7": "^1.7 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^4.8", - "squizlabs/php_codesniffer": "~2.6", - "friendsofphp/php-cs-fixer": "~1.12" + "phpunit/phpunit": "^8.0 || ^9.0", + "friendsofphp/php-cs-fixer": "^3.5" }, "autoload": { "psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" } diff --git a/src/main/resources/handlebars/php/model_generic.mustache b/src/main/resources/handlebars/php/model_generic.mustache index ca67203013..be1beb0983 100644 --- a/src/main/resources/handlebars/php/model_generic.mustache +++ b/src/main/resources/handlebars/php/model_generic.mustache @@ -166,7 +166,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{#discriminator}} // Initialize discriminator property with the model name. - $discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap); + $discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap, true); $this->container[$discriminator] = static::$swaggerModelName; {{/discriminator}} } @@ -353,7 +353,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return boolean */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->container[$offset]); @@ -366,7 +366,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return mixed */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; @@ -380,7 +380,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return void */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { if (is_null($offset)) { @@ -397,7 +397,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return void */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->container[$offset]); diff --git a/src/main/resources/handlebars/php/model_test.mustache b/src/main/resources/handlebars/php/model_test.mustache index 99064308f5..f96884d1db 100644 --- a/src/main/resources/handlebars/php/model_test.mustache +++ b/src/main/resources/handlebars/php/model_test.mustache @@ -21,6 +21,8 @@ namespace {{invokerPackage}}; +use PHPUnit\Framework\TestCase; + /** * {{classname}}Test Class Doc Comment * @@ -30,34 +32,34 @@ namespace {{invokerPackage}}; * @author Swagger Codegen team * @link https://github.com/swagger-api/swagger-codegen */ -class {{classname}}Test extends \PHPUnit_Framework_TestCase +class {{classname}}Test extends TestCase { /** * Setup before running any test case */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { } /** * Setup before running each test case */ - public function setUp() + public function setUp(): void { } /** * Clean up after running each test case */ - public function tearDown() + public function tearDown(): void { } /** * Clean up after running all test cases */ - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { }