From 5400a7e445bfa7248fbaf7cfc9b1cc076d4cb402 Mon Sep 17 00:00:00 2001 From: Yuriy Belenko Date: Sat, 29 Dec 2018 11:58:15 +0500 Subject: [PATCH] [Slim] Add PHP CodeSniffer config template (#1764) * [Slim] Add PHP_CodeSniffer config template * [Slim] Update doc * [Slim] Add local configs to gitignore PHPUnit and PHP_CodeSniffer provides the same developing pattern when user overrides global config with local one. In local config he can set environment variables. Official doc recommends to not commit local config files. * [Slim] Remove phpcsStandard CLI option Now user have full freedom to override config file. This option is not used and not necessary anymore. * [Slim] Refresh samples --- docs/generators/php-slim.md | 3 - .../languages/PhpSlimServerCodegen.java | 21 +------ .../main/resources/php-slim-server/.gitignore | 8 ++- .../resources/php-slim-server/README.mustache | 55 +++++++++++++++---- .../php-slim-server/composer.mustache | 2 +- .../php-slim-server/phpcs.xml.mustache | 31 +++++++++++ .../options/PhpSlimServerOptionsProvider.java | 3 - .../slim/PhpSlimServerOptionsTest.java | 2 - .../php-slim/.gitignore | 8 ++- .../petstore-security-test/php-slim/README.md | 55 +++++++++++++++---- .../php-slim/composer.json | 2 +- .../php-slim/phpcs.xml.dist | 31 +++++++++++ samples/server/petstore/php-slim/.gitignore | 8 ++- samples/server/petstore/php-slim/README.md | 55 +++++++++++++++---- .../server/petstore/php-slim/composer.json | 2 +- .../server/petstore/php-slim/phpcs.xml.dist | 31 +++++++++++ 16 files changed, 250 insertions(+), 67 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/php-slim-server/phpcs.xml.mustache create mode 100644 samples/server/petstore-security-test/php-slim/phpcs.xml.dist create mode 100644 samples/server/petstore/php-slim/phpcs.xml.dist diff --git a/docs/generators/php-slim.md b/docs/generators/php-slim.md index 629056bb928..ee452b57fa2 100644 --- a/docs/generators/php-slim.md +++ b/docs/generators/php-slim.md @@ -40,7 +40,4 @@ CONFIG OPTIONS for php-slim artifactVersion The version to use in the composer package version field. e.g. 1.2.3 - phpcsStandard - PHP CodeSniffer option. Accepts name or path of the coding standard to use. (Default: PSR12) - Back to the [generators list](README.md) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java index 3ff9aad4eaa..d8480e4cc9e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java @@ -35,12 +35,10 @@ import java.util.Map; public class PhpSlimServerCodegen extends AbstractPhpCodegen { private static final Logger LOGGER = LoggerFactory.getLogger(PhpSlimServerCodegen.class); - public static final String PHPCS_STANDARD = "phpcsStandard"; public static final String USER_CLASSNAME_KEY = "userClassname"; protected String groupId = "org.openapitools"; protected String artifactId = "openapi-server"; - protected String phpcsStandard = "PSR12"; public PhpSlimServerCodegen() { super(); @@ -74,9 +72,6 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen { break; } } - - cliOptions.add(new CliOption(PHPCS_STANDARD, "PHP CodeSniffer option. Accepts name or path of the coding standard to use.") - .defaultValue("PSR12")); } @Override @@ -116,18 +111,13 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen { public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey(PHPCS_STANDARD)) { - this.setPhpcsStandard((String) additionalProperties.get(PHPCS_STANDARD)); - } else { - additionalProperties.put(PHPCS_STANDARD, phpcsStandard); - } - supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json")); supportingFiles.add(new SupportingFile("index.mustache", "", "index.php")); supportingFiles.add(new SupportingFile(".htaccess", "", ".htaccess")); supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php")); supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist")); + supportingFiles.add(new SupportingFile("phpcs.xml.mustache", "", "phpcs.xml.dist")); } @Override @@ -161,15 +151,6 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen { return objs; } - /** - * Sets PHP CodeSniffer <standard> option. Accepts name or path of the coding standard to use. - * - * @param phpcsStandard standard option value - */ - public void setPhpcsStandard(String phpcsStandard) { - this.phpcsStandard = phpcsStandard; - } - @Override public String toApiName(String name) { if (name.length() == 0) { diff --git a/modules/openapi-generator/src/main/resources/php-slim-server/.gitignore b/modules/openapi-generator/src/main/resources/php-slim-server/.gitignore index cd37c4a9b99..db204792ea3 100644 --- a/modules/openapi-generator/src/main/resources/php-slim-server/.gitignore +++ b/modules/openapi-generator/src/main/resources/php-slim-server/.gitignore @@ -8,4 +8,10 @@ composer.phar # composer.lock # phplint tool creates cache file which is not necessary in a codebase -/.phplint-cache \ No newline at end of file +/.phplint-cache + +# Do not commit local PHPUnit config +/phpunit.xml + +# Do not commit local PHP_CodeSniffer config +/phpcs.xml \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/php-slim-server/README.mustache b/modules/openapi-generator/src/main/resources/php-slim-server/README.mustache index 389484693fa..1620492e5e2 100644 --- a/modules/openapi-generator/src/main/resources/php-slim-server/README.mustache +++ b/modules/openapi-generator/src/main/resources/php-slim-server/README.mustache @@ -28,21 +28,54 @@ $ php -S localhost:8888 -t php-slim-server > It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. > It is not intended to be a full-featured web server. It should not be used on a public network. -## Run tests +## Tests -This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing and PHP Codesniffer to check source code against user defined coding standard(`phpcsStandard` generator config option). +### PHPUnit + +This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing. [Test folder]({{testBasePath}}) contains templates which you can fill with real test assertions. How to write tests read at [PHPUnit Manual - Chapter 2. Writing Tests for PHPUnit](https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html). -How to configure PHP CodeSniffer read at [PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). -There is [phplint](https://github.com/overtrue/phplint) tool to check php syntax automatically. -Command | Tool | Target ----- | ---- | ---- -`$ composer test` | PHPUnit | All tests -`$ composer run test-apis` | PHPUnit | Apis tests -`$ composer run test-models` | PHPUnit | Models tests -`$ composer run phpcs` | PHP CodeSniffer | All files -`$ composer run phplint` | phplint | All files +#### Run + +Command | Target +---- | ---- +`$ composer test` | All tests +`$ composer test-apis` | Apis tests +`$ composer test-models` | Models tests + +#### Config + +Package contains fully functional config `./phpunit.xml.dist` file. Create `./phpunit.xml` in root folder to override it. + +Quote from [3. The Command-Line Test Runner — PHPUnit 7.4 Manual](https://phpunit.readthedocs.io/en/7.4/textui.html#command-line-options): + +> If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file. + +### PHP CodeSniffer + +[PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). This tool helps to follow coding style and avoid common PHP coding mistakes. + +#### Run + +```bash +$ composer phpcs +``` + +#### Config + +Package contains fully functional config `./phpcs.xml.dist` file. It checks source code against PSR-1 and PSR-2 coding standards. +Create `./phpcs.xml` in root folder to override it. More info at [Using a Default Configuration File](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file) + +### PHPLint + +[PHPLint Documentation](https://github.com/overtrue/phplint). Checks PHP syntax only. + +#### Run + +```bash +$ composer phplint +``` ## Show errors diff --git a/modules/openapi-generator/src/main/resources/php-slim-server/composer.mustache b/modules/openapi-generator/src/main/resources/php-slim-server/composer.mustache index 6f23f467e29..8dfeed44a84 100644 --- a/modules/openapi-generator/src/main/resources/php-slim-server/composer.mustache +++ b/modules/openapi-generator/src/main/resources/php-slim-server/composer.mustache @@ -26,7 +26,7 @@ ], "test-apis": "phpunit --testsuite Apis", "test-models": "phpunit --testsuite Models", - "phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard={{phpcsStandard}}", + "phpcs": "phpcs", "phplint": "phplint ./ --exclude=vendor" } } diff --git a/modules/openapi-generator/src/main/resources/php-slim-server/phpcs.xml.mustache b/modules/openapi-generator/src/main/resources/php-slim-server/phpcs.xml.mustache new file mode 100644 index 00000000000..024e5f3371f --- /dev/null +++ b/modules/openapi-generator/src/main/resources/php-slim-server/phpcs.xml.mustache @@ -0,0 +1,31 @@ + + + PHP_CodeSniffer config for {{appName}} + + + ./ + + + ./vendor + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpSlimServerOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpSlimServerOptionsProvider.java index 39186e7b051..c170e2ff577 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpSlimServerOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpSlimServerOptionsProvider.java @@ -19,7 +19,6 @@ package org.openapitools.codegen.options; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.languages.AbstractPhpCodegen; -import org.openapitools.codegen.languages.PhpSlimServerCodegen; import com.google.common.collect.ImmutableMap; @@ -39,7 +38,6 @@ public class PhpSlimServerOptionsProvider implements OptionsProvider { public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; - public static final String PHPCS_STANDARD_VALUE = "PSR12"; @Override public String getLanguage() { @@ -62,7 +60,6 @@ public class PhpSlimServerOptionsProvider implements OptionsProvider { .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) - .put(PhpSlimServerCodegen.PHPCS_STANDARD, PHPCS_STANDARD_VALUE) .build(); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/slim/PhpSlimServerOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/slim/PhpSlimServerOptionsTest.java index 57a87adaf3f..25cd43077aa 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/slim/PhpSlimServerOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/slim/PhpSlimServerOptionsTest.java @@ -63,8 +63,6 @@ public class PhpSlimServerOptionsTest extends AbstractOptionsTest { times = 1; clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(PhpSlimServerOptionsProvider.SORT_PARAMS_VALUE)); times = 1; - clientCodegen.setPhpcsStandard(PhpSlimServerOptionsProvider.PHPCS_STANDARD_VALUE); - times = 1; }}; } } diff --git a/samples/server/petstore-security-test/php-slim/.gitignore b/samples/server/petstore-security-test/php-slim/.gitignore index cd37c4a9b99..db204792ea3 100644 --- a/samples/server/petstore-security-test/php-slim/.gitignore +++ b/samples/server/petstore-security-test/php-slim/.gitignore @@ -8,4 +8,10 @@ composer.phar # composer.lock # phplint tool creates cache file which is not necessary in a codebase -/.phplint-cache \ No newline at end of file +/.phplint-cache + +# Do not commit local PHPUnit config +/phpunit.xml + +# Do not commit local PHP_CodeSniffer config +/phpcs.xml \ No newline at end of file diff --git a/samples/server/petstore-security-test/php-slim/README.md b/samples/server/petstore-security-test/php-slim/README.md index 31a73e777ea..b1820697b66 100644 --- a/samples/server/petstore-security-test/php-slim/README.md +++ b/samples/server/petstore-security-test/php-slim/README.md @@ -28,21 +28,54 @@ $ php -S localhost:8888 -t php-slim-server > It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. > It is not intended to be a full-featured web server. It should not be used on a public network. -## Run tests +## Tests -This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing and PHP Codesniffer to check source code against user defined coding standard(`phpcsStandard` generator config option). +### PHPUnit + +This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing. [Test folder](test) contains templates which you can fill with real test assertions. How to write tests read at [PHPUnit Manual - Chapter 2. Writing Tests for PHPUnit](https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html). -How to configure PHP CodeSniffer read at [PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). -There is [phplint](https://github.com/overtrue/phplint) tool to check php syntax automatically. -Command | Tool | Target ----- | ---- | ---- -`$ composer test` | PHPUnit | All tests -`$ composer run test-apis` | PHPUnit | Apis tests -`$ composer run test-models` | PHPUnit | Models tests -`$ composer run phpcs` | PHP CodeSniffer | All files -`$ composer run phplint` | phplint | All files +#### Run + +Command | Target +---- | ---- +`$ composer test` | All tests +`$ composer test-apis` | Apis tests +`$ composer test-models` | Models tests + +#### Config + +Package contains fully functional config `./phpunit.xml.dist` file. Create `./phpunit.xml` in root folder to override it. + +Quote from [3. The Command-Line Test Runner — PHPUnit 7.4 Manual](https://phpunit.readthedocs.io/en/7.4/textui.html#command-line-options): + +> If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file. + +### PHP CodeSniffer + +[PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). This tool helps to follow coding style and avoid common PHP coding mistakes. + +#### Run + +```bash +$ composer phpcs +``` + +#### Config + +Package contains fully functional config `./phpcs.xml.dist` file. It checks source code against PSR-1 and PSR-2 coding standards. +Create `./phpcs.xml` in root folder to override it. More info at [Using a Default Configuration File](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file) + +### PHPLint + +[PHPLint Documentation](https://github.com/overtrue/phplint). Checks PHP syntax only. + +#### Run + +```bash +$ composer phplint +``` ## Show errors diff --git a/samples/server/petstore-security-test/php-slim/composer.json b/samples/server/petstore-security-test/php-slim/composer.json index 5b4e586c7c8..9c1a1d936ca 100644 --- a/samples/server/petstore-security-test/php-slim/composer.json +++ b/samples/server/petstore-security-test/php-slim/composer.json @@ -26,7 +26,7 @@ ], "test-apis": "phpunit --testsuite Apis", "test-models": "phpunit --testsuite Models", - "phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard=PSR12", + "phpcs": "phpcs", "phplint": "phplint ./ --exclude=vendor" } } diff --git a/samples/server/petstore-security-test/php-slim/phpcs.xml.dist b/samples/server/petstore-security-test/php-slim/phpcs.xml.dist new file mode 100644 index 00000000000..744c14c40a4 --- /dev/null +++ b/samples/server/petstore-security-test/php-slim/phpcs.xml.dist @@ -0,0 +1,31 @@ + + + PHP_CodeSniffer config for OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r + + + ./ + + + ./vendor + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/server/petstore/php-slim/.gitignore b/samples/server/petstore/php-slim/.gitignore index cd37c4a9b99..db204792ea3 100644 --- a/samples/server/petstore/php-slim/.gitignore +++ b/samples/server/petstore/php-slim/.gitignore @@ -8,4 +8,10 @@ composer.phar # composer.lock # phplint tool creates cache file which is not necessary in a codebase -/.phplint-cache \ No newline at end of file +/.phplint-cache + +# Do not commit local PHPUnit config +/phpunit.xml + +# Do not commit local PHP_CodeSniffer config +/phpcs.xml \ No newline at end of file diff --git a/samples/server/petstore/php-slim/README.md b/samples/server/petstore/php-slim/README.md index 00e100cca4f..6930213b7d4 100644 --- a/samples/server/petstore/php-slim/README.md +++ b/samples/server/petstore/php-slim/README.md @@ -28,21 +28,54 @@ $ php -S localhost:8888 -t php-slim-server > It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. > It is not intended to be a full-featured web server. It should not be used on a public network. -## Run tests +## Tests -This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing and PHP Codesniffer to check source code against user defined coding standard(`phpcsStandard` generator config option). +### PHPUnit + +This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing. [Test folder](test) contains templates which you can fill with real test assertions. How to write tests read at [PHPUnit Manual - Chapter 2. Writing Tests for PHPUnit](https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html). -How to configure PHP CodeSniffer read at [PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). -There is [phplint](https://github.com/overtrue/phplint) tool to check php syntax automatically. -Command | Tool | Target ----- | ---- | ---- -`$ composer test` | PHPUnit | All tests -`$ composer run test-apis` | PHPUnit | Apis tests -`$ composer run test-models` | PHPUnit | Models tests -`$ composer run phpcs` | PHP CodeSniffer | All files -`$ composer run phplint` | phplint | All files +#### Run + +Command | Target +---- | ---- +`$ composer test` | All tests +`$ composer test-apis` | Apis tests +`$ composer test-models` | Models tests + +#### Config + +Package contains fully functional config `./phpunit.xml.dist` file. Create `./phpunit.xml` in root folder to override it. + +Quote from [3. The Command-Line Test Runner — PHPUnit 7.4 Manual](https://phpunit.readthedocs.io/en/7.4/textui.html#command-line-options): + +> If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file. + +### PHP CodeSniffer + +[PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). This tool helps to follow coding style and avoid common PHP coding mistakes. + +#### Run + +```bash +$ composer phpcs +``` + +#### Config + +Package contains fully functional config `./phpcs.xml.dist` file. It checks source code against PSR-1 and PSR-2 coding standards. +Create `./phpcs.xml` in root folder to override it. More info at [Using a Default Configuration File](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file) + +### PHPLint + +[PHPLint Documentation](https://github.com/overtrue/phplint). Checks PHP syntax only. + +#### Run + +```bash +$ composer phplint +``` ## Show errors diff --git a/samples/server/petstore/php-slim/composer.json b/samples/server/petstore/php-slim/composer.json index 5b4e586c7c8..9c1a1d936ca 100644 --- a/samples/server/petstore/php-slim/composer.json +++ b/samples/server/petstore/php-slim/composer.json @@ -26,7 +26,7 @@ ], "test-apis": "phpunit --testsuite Apis", "test-models": "phpunit --testsuite Models", - "phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard=PSR12", + "phpcs": "phpcs", "phplint": "phplint ./ --exclude=vendor" } } diff --git a/samples/server/petstore/php-slim/phpcs.xml.dist b/samples/server/petstore/php-slim/phpcs.xml.dist new file mode 100644 index 00000000000..69f4ddac71c --- /dev/null +++ b/samples/server/petstore/php-slim/phpcs.xml.dist @@ -0,0 +1,31 @@ + + + PHP_CodeSniffer config for OpenAPI Petstore + + + ./ + + + ./vendor + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file