[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
This commit is contained in:
Yuriy Belenko 2018-12-29 11:58:15 +05:00 committed by Akihito Nakano
parent 1db105b6f9
commit 5400a7e445
16 changed files with 250 additions and 67 deletions

View File

@ -40,7 +40,4 @@ CONFIG OPTIONS for php-slim
artifactVersion artifactVersion
The version to use in the composer package version field. e.g. 1.2.3 The version to use in the composer package version field. e.g. 1.2.3
phpcsStandard
PHP CodeSniffer <standard> option. Accepts name or path of the coding standard to use. (Default: PSR12)
Back to the [generators list](README.md) Back to the [generators list](README.md)

View File

@ -35,12 +35,10 @@ import java.util.Map;
public class PhpSlimServerCodegen extends AbstractPhpCodegen { public class PhpSlimServerCodegen extends AbstractPhpCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(PhpSlimServerCodegen.class); private static final Logger LOGGER = LoggerFactory.getLogger(PhpSlimServerCodegen.class);
public static final String PHPCS_STANDARD = "phpcsStandard";
public static final String USER_CLASSNAME_KEY = "userClassname"; public static final String USER_CLASSNAME_KEY = "userClassname";
protected String groupId = "org.openapitools"; protected String groupId = "org.openapitools";
protected String artifactId = "openapi-server"; protected String artifactId = "openapi-server";
protected String phpcsStandard = "PSR12";
public PhpSlimServerCodegen() { public PhpSlimServerCodegen() {
super(); super();
@ -74,9 +72,6 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
break; break;
} }
} }
cliOptions.add(new CliOption(PHPCS_STANDARD, "PHP CodeSniffer <standard> option. Accepts name or path of the coding standard to use.")
.defaultValue("PSR12"));
} }
@Override @Override
@ -116,18 +111,13 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
public void processOpts() { public void processOpts() {
super.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("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json")); supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json"));
supportingFiles.add(new SupportingFile("index.mustache", "", "index.php")); supportingFiles.add(new SupportingFile("index.mustache", "", "index.php"));
supportingFiles.add(new SupportingFile(".htaccess", "", ".htaccess")); supportingFiles.add(new SupportingFile(".htaccess", "", ".htaccess"));
supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php")); 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("phpunit.xml.mustache", "", "phpunit.xml.dist"));
supportingFiles.add(new SupportingFile("phpcs.xml.mustache", "", "phpcs.xml.dist"));
} }
@Override @Override
@ -161,15 +151,6 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
return objs; return objs;
} }
/**
* Sets PHP CodeSniffer &lt;standard&gt; 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 @Override
public String toApiName(String name) { public String toApiName(String name) {
if (name.length() == 0) { if (name.length() == 0) {

View File

@ -9,3 +9,9 @@ composer.phar
# phplint tool creates cache file which is not necessary in a codebase # phplint tool creates cache file which is not necessary in a codebase
/.phplint-cache /.phplint-cache
# Do not commit local PHPUnit config
/phpunit.xml
# Do not commit local PHP_CodeSniffer config
/phpcs.xml

View File

@ -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 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. > 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. [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 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 #### Run
---- | ---- | ----
`$ composer test` | PHPUnit | All tests Command | Target
`$ composer run test-apis` | PHPUnit | Apis tests ---- | ----
`$ composer run test-models` | PHPUnit | Models tests `$ composer test` | All tests
`$ composer run phpcs` | PHP CodeSniffer | All files `$ composer test-apis` | Apis tests
`$ composer run phplint` | phplint | All files `$ 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 ## Show errors

View File

@ -26,7 +26,7 @@
], ],
"test-apis": "phpunit --testsuite Apis", "test-apis": "phpunit --testsuite Apis",
"test-models": "phpunit --testsuite Models", "test-models": "phpunit --testsuite Models",
"phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard={{phpcsStandard}}", "phpcs": "phpcs",
"phplint": "phplint ./ --exclude=vendor" "phplint": "phplint ./ --exclude=vendor"
} }
} }

View File

@ -0,0 +1,31 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="{{appName}} Config" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>PHP_CodeSniffer config for {{appName}}</description>
<!-- Path to inspected files -->
<file>./</file>
<!-- Don't need to inspect installed packages -->
<exclude-pattern>./vendor</exclude-pattern>
<!-- <basepath> A path to strip from the front of file paths inside reports -->
<arg name="basepath" value="."/>
<!-- colors Use colors in output -->
<arg name="colors"/>
<!-- Do not print warnings -->
<!-- <arg name="warning-severity" value="0"/> -->
<!-- -p Show progress of the run -->
<!-- -s Show sniff codes in all reports -->
<arg value="ps"/>
<!-- Include the whole PSR12 standard -->
<rule ref="PSR12">
<!-- There is no way to wrap generated comments, just disable that rule for now -->
<exclude name="Generic.Files.LineLength.TooLong" />
<!-- Codegen generates variables with underscore on purpose -->
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore" />
</rule>
</ruleset>

View File

@ -19,7 +19,6 @@ package org.openapitools.codegen.options;
import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.AbstractPhpCodegen; import org.openapitools.codegen.languages.AbstractPhpCodegen;
import org.openapitools.codegen.languages.PhpSlimServerCodegen;
import com.google.common.collect.ImmutableMap; 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 ENSURE_UNIQUE_PARAMS_VALUE = "true";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
public static final String PHPCS_STANDARD_VALUE = "PSR12";
@Override @Override
public String getLanguage() { public String getLanguage() {
@ -62,7 +60,6 @@ public class PhpSlimServerOptionsProvider implements OptionsProvider {
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(PhpSlimServerCodegen.PHPCS_STANDARD, PHPCS_STANDARD_VALUE)
.build(); .build();
} }

View File

@ -63,8 +63,6 @@ public class PhpSlimServerOptionsTest extends AbstractOptionsTest {
times = 1; times = 1;
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(PhpSlimServerOptionsProvider.SORT_PARAMS_VALUE)); clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(PhpSlimServerOptionsProvider.SORT_PARAMS_VALUE));
times = 1; times = 1;
clientCodegen.setPhpcsStandard(PhpSlimServerOptionsProvider.PHPCS_STANDARD_VALUE);
times = 1;
}}; }};
} }
} }

View File

@ -9,3 +9,9 @@ composer.phar
# phplint tool creates cache file which is not necessary in a codebase # phplint tool creates cache file which is not necessary in a codebase
/.phplint-cache /.phplint-cache
# Do not commit local PHPUnit config
/phpunit.xml
# Do not commit local PHP_CodeSniffer config
/phpcs.xml

View File

@ -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 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. > 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. [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 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 #### Run
---- | ---- | ----
`$ composer test` | PHPUnit | All tests Command | Target
`$ composer run test-apis` | PHPUnit | Apis tests ---- | ----
`$ composer run test-models` | PHPUnit | Models tests `$ composer test` | All tests
`$ composer run phpcs` | PHP CodeSniffer | All files `$ composer test-apis` | Apis tests
`$ composer run phplint` | phplint | All files `$ 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 ## Show errors

View File

@ -26,7 +26,7 @@
], ],
"test-apis": "phpunit --testsuite Apis", "test-apis": "phpunit --testsuite Apis",
"test-models": "phpunit --testsuite Models", "test-models": "phpunit --testsuite Models",
"phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard=PSR12", "phpcs": "phpcs",
"phplint": "phplint ./ --exclude=vendor" "phplint": "phplint ./ --exclude=vendor"
} }
} }

View File

@ -0,0 +1,31 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="OpenAPI Petstore *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r Config" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>PHP_CodeSniffer config for OpenAPI Petstore *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r</description>
<!-- Path to inspected files -->
<file>./</file>
<!-- Don't need to inspect installed packages -->
<exclude-pattern>./vendor</exclude-pattern>
<!-- <basepath> A path to strip from the front of file paths inside reports -->
<arg name="basepath" value="."/>
<!-- colors Use colors in output -->
<arg name="colors"/>
<!-- Do not print warnings -->
<!-- <arg name="warning-severity" value="0"/> -->
<!-- -p Show progress of the run -->
<!-- -s Show sniff codes in all reports -->
<arg value="ps"/>
<!-- Include the whole PSR12 standard -->
<rule ref="PSR12">
<!-- There is no way to wrap generated comments, just disable that rule for now -->
<exclude name="Generic.Files.LineLength.TooLong" />
<!-- Codegen generates variables with underscore on purpose -->
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore" />
</rule>
</ruleset>

View File

@ -9,3 +9,9 @@ composer.phar
# phplint tool creates cache file which is not necessary in a codebase # phplint tool creates cache file which is not necessary in a codebase
/.phplint-cache /.phplint-cache
# Do not commit local PHPUnit config
/phpunit.xml
# Do not commit local PHP_CodeSniffer config
/phpcs.xml

View File

@ -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 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. > 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. [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 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 #### Run
---- | ---- | ----
`$ composer test` | PHPUnit | All tests Command | Target
`$ composer run test-apis` | PHPUnit | Apis tests ---- | ----
`$ composer run test-models` | PHPUnit | Models tests `$ composer test` | All tests
`$ composer run phpcs` | PHP CodeSniffer | All files `$ composer test-apis` | Apis tests
`$ composer run phplint` | phplint | All files `$ 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 ## Show errors

View File

@ -26,7 +26,7 @@
], ],
"test-apis": "phpunit --testsuite Apis", "test-apis": "phpunit --testsuite Apis",
"test-models": "phpunit --testsuite Models", "test-models": "phpunit --testsuite Models",
"phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard=PSR12", "phpcs": "phpcs",
"phplint": "phplint ./ --exclude=vendor" "phplint": "phplint ./ --exclude=vendor"
} }
} }

View File

@ -0,0 +1,31 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="OpenAPI Petstore Config" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>PHP_CodeSniffer config for OpenAPI Petstore</description>
<!-- Path to inspected files -->
<file>./</file>
<!-- Don't need to inspect installed packages -->
<exclude-pattern>./vendor</exclude-pattern>
<!-- <basepath> A path to strip from the front of file paths inside reports -->
<arg name="basepath" value="."/>
<!-- colors Use colors in output -->
<arg name="colors"/>
<!-- Do not print warnings -->
<!-- <arg name="warning-severity" value="0"/> -->
<!-- -p Show progress of the run -->
<!-- -s Show sniff codes in all reports -->
<arg value="ps"/>
<!-- Include the whole PSR12 standard -->
<rule ref="PSR12">
<!-- There is no way to wrap generated comments, just disable that rule for now -->
<exclude name="Generic.Files.LineLength.TooLong" />
<!-- Codegen generates variables with underscore on purpose -->
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore" />
</rule>
</ruleset>