From e639dad66a1cf4ed516b9eab4bc6fcccf8d7991b Mon Sep 17 00:00:00 2001 From: Yuriy Belenko Date: Mon, 9 May 2022 19:34:38 +0300 Subject: [PATCH] [php] Add PSR-12 rule set (#12302) * Upgrade PHPCS-Fixer to 3.5 version This version requires PHP 7.4 or 8.0 which perfectly meets our supported PHP right now. * Rename config file to .php-cs-fixer.dist.php This way it can be overwritten with local file(same name without "dist" part). Also it's recommended filename from the package doc. * Add PSR12 rule set to config I've changed config file a bit to look like example from the doc. Ref: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/HEAD/doc/config.rst * Ignore new cache format * Remove blank line after class opening * Set constants in models public * Refresh samples --- .../codegen/languages/PhpClientCodegen.java | 2 +- .../main/resources/php/.php-cs-fixer.dist.php | 29 +++++++++++++++++++ .../src/main/resources/php/.php_cs | 23 --------------- .../main/resources/php/ApiException.mustache | 1 - .../resources/php/HeaderSelector.mustache | 1 - .../src/main/resources/php/composer.mustache | 2 +- .../src/main/resources/php/gitignore | 1 + .../main/resources/php/model_enum.mustache | 2 +- .../main/resources/php/model_generic.mustache | 2 +- .../petstore/php/OpenAPIClient-php/.gitignore | 1 + .../.openapi-generator/FILES | 2 +- .../OpenAPIClient-php/.php-cs-fixer.dist.php | 29 +++++++++++++++++++ .../php/OpenAPIClient-php/composer.json | 2 +- .../OpenAPIClient-php/lib/ApiException.php | 1 - .../OpenAPIClient-php/lib/HeaderSelector.php | 1 - .../lib/Model/EnumArrays.php | 8 ++--- .../OpenAPIClient-php/lib/Model/EnumClass.php | 6 ++-- .../OpenAPIClient-php/lib/Model/EnumTest.php | 20 ++++++------- .../OpenAPIClient-php/lib/Model/MapTest.php | 4 +-- .../php/OpenAPIClient-php/lib/Model/Order.php | 6 ++-- .../OpenAPIClient-php/lib/Model/OuterEnum.php | 6 ++-- .../lib/Model/OuterEnumDefaultValue.php | 6 ++-- .../lib/Model/OuterEnumInteger.php | 6 ++-- .../Model/OuterEnumIntegerDefaultValue.php | 6 ++-- .../php/OpenAPIClient-php/lib/Model/Pet.php | 6 ++-- .../lib/Model/SingleRefType.php | 4 +-- 26 files changed, 105 insertions(+), 72 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/php/.php-cs-fixer.dist.php delete mode 100644 modules/openapi-generator/src/main/resources/php/.php_cs create mode 100644 samples/client/petstore/php/OpenAPIClient-php/.php-cs-fixer.dist.php diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java index 6049bfa394b..3cceb9a5f5b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java @@ -108,7 +108,7 @@ public class PhpClientCodegen extends AbstractPhpCodegen { supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist")); supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml")); - supportingFiles.add(new SupportingFile(".php_cs", "", ".php_cs")); + supportingFiles.add(new SupportingFile(".php-cs-fixer.dist.php", "", ".php-cs-fixer.dist.php")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); } } diff --git a/modules/openapi-generator/src/main/resources/php/.php-cs-fixer.dist.php b/modules/openapi-generator/src/main/resources/php/.php-cs-fixer.dist.php new file mode 100644 index 00000000000..af9cf39fddb --- /dev/null +++ b/modules/openapi-generator/src/main/resources/php/.php-cs-fixer.dist.php @@ -0,0 +1,29 @@ +in(__DIR__) + ->exclude('vendor') + ->exclude('test') + ->exclude('tests') +; + +$config = new PhpCsFixer\Config(); +return $config->setRules([ + '@PSR12' => 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, + 'no_leading_import_slash' => false, + ]) + ->setFinder($finder) +; diff --git a/modules/openapi-generator/src/main/resources/php/.php_cs b/modules/openapi-generator/src/main/resources/php/.php_cs deleted file mode 100644 index 4fbe53ec5ff..00000000000 --- a/modules/openapi-generator/src/main/resources/php/.php_cs +++ /dev/null @@ -1,23 +0,0 @@ -setUsingCache(true) - ->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/modules/openapi-generator/src/main/resources/php/ApiException.mustache b/modules/openapi-generator/src/main/resources/php/ApiException.mustache index ade19b26538..0410c3ed4a2 100644 --- a/modules/openapi-generator/src/main/resources/php/ApiException.mustache +++ b/modules/openapi-generator/src/main/resources/php/ApiException.mustache @@ -30,7 +30,6 @@ use \Exception; */ class ApiException extends Exception { - /** * The HTTP body of the server response either as Json or string. * diff --git a/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache b/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache index aece2d69c0d..a2622e30ed7 100644 --- a/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache +++ b/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache @@ -30,7 +30,6 @@ use \Exception; */ class HeaderSelector { - /** * @param string[] $accept * @param string[] $contentTypes diff --git a/modules/openapi-generator/src/main/resources/php/composer.mustache b/modules/openapi-generator/src/main/resources/php/composer.mustache index e8c6f3bf10c..8b6d4e98696 100644 --- a/modules/openapi-generator/src/main/resources/php/composer.mustache +++ b/modules/openapi-generator/src/main/resources/php/composer.mustache @@ -33,7 +33,7 @@ }, "require-dev": { "phpunit/phpunit": "^8.0 || ^9.0", - "friendsofphp/php-cs-fixer": "^2.12" + "friendsofphp/php-cs-fixer": "^3.5" }, "autoload": { "psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" } diff --git a/modules/openapi-generator/src/main/resources/php/gitignore b/modules/openapi-generator/src/main/resources/php/gitignore index 92f7fdeff58..9f1681c2be8 100644 --- a/modules/openapi-generator/src/main/resources/php/gitignore +++ b/modules/openapi-generator/src/main/resources/php/gitignore @@ -9,6 +9,7 @@ composer.phar # php-cs-fixer cache .php_cs.cache +.php-cs-fixer.cache # PHPUnit cache .phpunit.result.cache diff --git a/modules/openapi-generator/src/main/resources/php/model_enum.mustache b/modules/openapi-generator/src/main/resources/php/model_enum.mustache index 88036f76ffc..77001f2e605 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -5,7 +5,7 @@ class {{classname}} */ {{#allowableValues}} {{#enumVars}} - const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; + public const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; {{/enumVars}} {{/allowableValues}} diff --git a/modules/openapi-generator/src/main/resources/php/model_generic.mustache b/modules/openapi-generator/src/main/resources/php/model_generic.mustache index 03eeb2c16f4..c1eeebee803 100644 --- a/modules/openapi-generator/src/main/resources/php/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_generic.mustache @@ -127,7 +127,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par {{#isEnum}} {{#allowableValues}} {{#enumVars}} - const {{enumName}}_{{{name}}} = {{{value}}}; + public const {{enumName}}_{{{name}}} = {{{value}}}; {{/enumVars}} {{/allowableValues}} {{/isEnum}} diff --git a/samples/client/petstore/php/OpenAPIClient-php/.gitignore b/samples/client/petstore/php/OpenAPIClient-php/.gitignore index 92f7fdeff58..9f1681c2be8 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/.gitignore +++ b/samples/client/petstore/php/OpenAPIClient-php/.gitignore @@ -9,6 +9,7 @@ composer.phar # php-cs-fixer cache .php_cs.cache +.php-cs-fixer.cache # PHPUnit cache .phpunit.result.cache diff --git a/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES b/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES index c41da42e1fc..789b9f71e58 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES +++ b/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES @@ -1,5 +1,5 @@ .gitignore -.php_cs +.php-cs-fixer.dist.php .travis.yml README.md composer.json diff --git a/samples/client/petstore/php/OpenAPIClient-php/.php-cs-fixer.dist.php b/samples/client/petstore/php/OpenAPIClient-php/.php-cs-fixer.dist.php new file mode 100644 index 00000000000..af9cf39fddb --- /dev/null +++ b/samples/client/petstore/php/OpenAPIClient-php/.php-cs-fixer.dist.php @@ -0,0 +1,29 @@ +in(__DIR__) + ->exclude('vendor') + ->exclude('test') + ->exclude('tests') +; + +$config = new PhpCsFixer\Config(); +return $config->setRules([ + '@PSR12' => 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, + 'no_leading_import_slash' => false, + ]) + ->setFinder($finder) +; diff --git a/samples/client/petstore/php/OpenAPIClient-php/composer.json b/samples/client/petstore/php/OpenAPIClient-php/composer.json index 2318059097f..b2250cf6722 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/composer.json +++ b/samples/client/petstore/php/OpenAPIClient-php/composer.json @@ -27,7 +27,7 @@ }, "require-dev": { "phpunit/phpunit": "^8.0 || ^9.0", - "friendsofphp/php-cs-fixer": "^2.12" + "friendsofphp/php-cs-fixer": "^3.5" }, "autoload": { "psr-4": { "OpenAPI\\Client\\" : "lib/" } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php index 40d5643fb6d..e12b7f2816c 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php @@ -39,7 +39,6 @@ use \Exception; */ class ApiException extends Exception { - /** * The HTTP body of the server response either as Json or string. * diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php b/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php index 3cbf7663b59..627222ab36c 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/HeaderSelector.php @@ -39,7 +39,6 @@ use \Exception; */ class HeaderSelector { - /** * @param string[] $accept * @param string[] $contentTypes diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php index 42fb38e00d3..a27207d92bd 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php @@ -167,10 +167,10 @@ class EnumArrays implements ModelInterface, ArrayAccess, \JsonSerializable return self::$openAPIModelName; } - const JUST_SYMBOL_GREATER_THAN_OR_EQUAL_TO = '>='; - const JUST_SYMBOL_DOLLAR = '$'; - const ARRAY_ENUM_FISH = 'fish'; - const ARRAY_ENUM_CRAB = 'crab'; + public const JUST_SYMBOL_GREATER_THAN_OR_EQUAL_TO = '>='; + public const JUST_SYMBOL_DOLLAR = '$'; + public const ARRAY_ENUM_FISH = 'fish'; + public const ARRAY_ENUM_CRAB = 'crab'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php index 8fe1b288072..8b9e1d319f8 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php @@ -42,11 +42,11 @@ class EnumClass /** * Possible values of this enum */ - const ABC = '_abc'; + public const ABC = '_abc'; - const EFG = '-efg'; + public const EFG = '-efg'; - const XYZ = '(xyz)'; + public const XYZ = '(xyz)'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php index 95616eb83ed..2adce4a529f 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php @@ -197,16 +197,16 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable return self::$openAPIModelName; } - const ENUM_STRING_UPPER = 'UPPER'; - const ENUM_STRING_LOWER = 'lower'; - const ENUM_STRING_EMPTY = ''; - const ENUM_STRING_REQUIRED_UPPER = 'UPPER'; - const ENUM_STRING_REQUIRED_LOWER = 'lower'; - const ENUM_STRING_REQUIRED_EMPTY = ''; - const ENUM_INTEGER_1 = 1; - const ENUM_INTEGER_MINUS_1 = -1; - const ENUM_NUMBER_1_DOT_1 = 1.1; - const ENUM_NUMBER_MINUS_1_DOT_2 = -1.2; + public const ENUM_STRING_UPPER = 'UPPER'; + public const ENUM_STRING_LOWER = 'lower'; + public const ENUM_STRING_EMPTY = ''; + public const ENUM_STRING_REQUIRED_UPPER = 'UPPER'; + public const ENUM_STRING_REQUIRED_LOWER = 'lower'; + public const ENUM_STRING_REQUIRED_EMPTY = ''; + public const ENUM_INTEGER_1 = 1; + public const ENUM_INTEGER_MINUS_1 = -1; + public const ENUM_NUMBER_1_DOT_1 = 1.1; + public const ENUM_NUMBER_MINUS_1_DOT_2 = -1.2; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/MapTest.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/MapTest.php index 209b437269e..ca11f082697 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/MapTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/MapTest.php @@ -177,8 +177,8 @@ class MapTest implements ModelInterface, ArrayAccess, \JsonSerializable return self::$openAPIModelName; } - const MAP_OF_ENUM_STRING_UPPER = 'UPPER'; - const MAP_OF_ENUM_STRING_LOWER = 'lower'; + public const MAP_OF_ENUM_STRING_UPPER = 'UPPER'; + public const MAP_OF_ENUM_STRING_LOWER = 'lower'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php index 0deed17c114..2318e4c4be1 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php @@ -187,9 +187,9 @@ class Order implements ModelInterface, ArrayAccess, \JsonSerializable return self::$openAPIModelName; } - const STATUS_PLACED = 'placed'; - const STATUS_APPROVED = 'approved'; - const STATUS_DELIVERED = 'delivered'; + public const STATUS_PLACED = 'placed'; + public const STATUS_APPROVED = 'approved'; + public const STATUS_DELIVERED = 'delivered'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php index b1b834319a8..6ee35cc3187 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php @@ -42,11 +42,11 @@ class OuterEnum /** * Possible values of this enum */ - const PLACED = 'placed'; + public const PLACED = 'placed'; - const APPROVED = 'approved'; + public const APPROVED = 'approved'; - const DELIVERED = 'delivered'; + public const DELIVERED = 'delivered'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php index 5d88f63550c..30dadd3c7ea 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php @@ -42,11 +42,11 @@ class OuterEnumDefaultValue /** * Possible values of this enum */ - const PLACED = 'placed'; + public const PLACED = 'placed'; - const APPROVED = 'approved'; + public const APPROVED = 'approved'; - const DELIVERED = 'delivered'; + public const DELIVERED = 'delivered'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php index 5bb6ee6f26e..0aa176d13f6 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php @@ -42,11 +42,11 @@ class OuterEnumInteger /** * Possible values of this enum */ - const NUMBER_0 = 0; + public const NUMBER_0 = 0; - const NUMBER_1 = 1; + public const NUMBER_1 = 1; - const NUMBER_2 = 2; + public const NUMBER_2 = 2; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php index f7ae2d6b29d..622e76d0aae 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php @@ -42,11 +42,11 @@ class OuterEnumIntegerDefaultValue /** * Possible values of this enum */ - const NUMBER_0 = 0; + public const NUMBER_0 = 0; - const NUMBER_1 = 1; + public const NUMBER_1 = 1; - const NUMBER_2 = 2; + public const NUMBER_2 = 2; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php index d648ac61aa4..50d5fabc0f0 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php @@ -187,9 +187,9 @@ class Pet implements ModelInterface, ArrayAccess, \JsonSerializable return self::$openAPIModelName; } - const STATUS_AVAILABLE = 'available'; - const STATUS_PENDING = 'pending'; - const STATUS_SOLD = 'sold'; + public const STATUS_AVAILABLE = 'available'; + public const STATUS_PENDING = 'pending'; + public const STATUS_SOLD = 'sold'; /** * Gets allowable values of the enum diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/SingleRefType.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/SingleRefType.php index ff92f27cd1d..398508a02ea 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/SingleRefType.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/SingleRefType.php @@ -42,9 +42,9 @@ class SingleRefType /** * Possible values of this enum */ - const ADMIN = 'admin'; + public const ADMIN = 'admin'; - const USER = 'user'; + public const USER = 'user'; /** * Gets allowable values of the enum