Skip to content

Commit d255357

Browse files
ybelenkoackintosh
authored andcommitted
[Slim] Add PHP CodeSniffer config template (OpenAPITools#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
1 parent 30d9e01 commit d255357

File tree

16 files changed

+253
-70
lines changed

16 files changed

+253
-70
lines changed

docs/generators/php-slim.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,4 @@ CONFIG OPTIONS for php-slim
4040
artifactVersion
4141
The version to use in the composer package version field. e.g. 1.2.3
4242

43-
phpcsStandard
44-
PHP CodeSniffer <standard> option. Accepts name or path of the coding standard to use. (Default: PSR12)
45-
4643
Back to the [generators list](README.md)

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@
3535
public class PhpSlimServerCodegen extends AbstractPhpCodegen {
3636
private static final Logger LOGGER = LoggerFactory.getLogger(PhpSlimServerCodegen.class);
3737

38-
public static final String PHPCS_STANDARD = "phpcsStandard";
3938
public static final String USER_CLASSNAME_KEY = "userClassname";
4039

4140
protected String groupId = "org.openapitools";
4241
protected String artifactId = "openapi-server";
43-
protected String phpcsStandard = "PSR12";
4442

4543
public PhpSlimServerCodegen() {
4644
super();
@@ -74,9 +72,6 @@ public PhpSlimServerCodegen() {
7472
break;
7573
}
7674
}
77-
78-
cliOptions.add(new CliOption(PHPCS_STANDARD, "PHP CodeSniffer <standard> option. Accepts name or path of the coding standard to use.")
79-
.defaultValue("PSR12"));
8075
}
8176

8277
@Override
@@ -116,18 +111,13 @@ public String modelFileFolder() {
116111
public void processOpts() {
117112
super.processOpts();
118113

119-
if (additionalProperties.containsKey(PHPCS_STANDARD)) {
120-
this.setPhpcsStandard((String) additionalProperties.get(PHPCS_STANDARD));
121-
} else {
122-
additionalProperties.put(PHPCS_STANDARD, phpcsStandard);
123-
}
124-
125114
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
126115
supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json"));
127116
supportingFiles.add(new SupportingFile("index.mustache", "", "index.php"));
128117
supportingFiles.add(new SupportingFile(".htaccess", "", ".htaccess"));
129118
supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php"));
130119
supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist"));
120+
supportingFiles.add(new SupportingFile("phpcs.xml.mustache", "", "phpcs.xml.dist"));
131121
}
132122

133123
@Override
@@ -161,15 +151,6 @@ public int compare(CodegenOperation one, CodegenOperation another) {
161151
return objs;
162152
}
163153

164-
/**
165-
* Sets PHP CodeSniffer &lt;standard&gt; option. Accepts name or path of the coding standard to use.
166-
*
167-
* @param phpcsStandard standard option value
168-
*/
169-
public void setPhpcsStandard(String phpcsStandard) {
170-
this.phpcsStandard = phpcsStandard;
171-
}
172-
173154
@Override
174155
public String toApiName(String name) {
175156
if (name.length() == 0) {

modules/openapi-generator/src/main/resources/php-slim-server/.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ composer.phar
88
# composer.lock
99

1010
# phplint tool creates cache file which is not necessary in a codebase
11-
/.phplint-cache
11+
/.phplint-cache
12+
13+
# Do not commit local PHPUnit config
14+
/phpunit.xml
15+
16+
# Do not commit local PHP_CodeSniffer config
17+
/phpcs.xml

modules/openapi-generator/src/main/resources/php-slim-server/README.mustache

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,54 @@ $ php -S localhost:8888 -t php-slim-server
2828
> It may also be useful for testing purposes or for application demonstrations that are run in controlled environments.
2929
> It is not intended to be a full-featured web server. It should not be used on a public network.
3030

31-
## Run tests
31+
## Tests
3232

33-
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).
33+
### PHPUnit
34+
35+
This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing.
3436
[Test folder]({{testBasePath}}) contains templates which you can fill with real test assertions.
3537
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).
36-
How to configure PHP CodeSniffer read at [PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki).
37-
There is [phplint](https://github.com/overtrue/phplint) tool to check php syntax automatically.
38-
39-
Command | Tool | Target
40-
---- | ---- | ----
41-
`$ composer test` | PHPUnit | All tests
42-
`$ composer run test-apis` | PHPUnit | Apis tests
43-
`$ composer run test-models` | PHPUnit | Models tests
44-
`$ composer run phpcs` | PHP CodeSniffer | All files
45-
`$ composer run phplint` | phplint | All files
38+
39+
#### Run
40+
41+
Command | Target
42+
---- | ----
43+
`$ composer test` | All tests
44+
`$ composer test-apis` | Apis tests
45+
`$ composer test-models` | Models tests
46+
47+
#### Config
48+
49+
Package contains fully functional config `./phpunit.xml.dist` file. Create `./phpunit.xml` in root folder to override it.
50+
51+
Quote from [3. The Command-Line Test Runner — PHPUnit 7.4 Manual](https://phpunit.readthedocs.io/en/7.4/textui.html#command-line-options):
52+
53+
> 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.
54+
55+
### PHP CodeSniffer
56+
57+
[PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). This tool helps to follow coding style and avoid common PHP coding mistakes.
58+
59+
#### Run
60+
61+
```bash
62+
$ composer phpcs
63+
```
64+
65+
#### Config
66+
67+
Package contains fully functional config `./phpcs.xml.dist` file. It checks source code against PSR-1 and PSR-2 coding standards.
68+
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)
69+
70+
### PHPLint
71+
72+
[PHPLint Documentation](https://github.com/overtrue/phplint). Checks PHP syntax only.
73+
74+
#### Run
75+
76+
```bash
77+
$ composer phplint
78+
```
4679

4780
## Show errors
4881

modules/openapi-generator/src/main/resources/php-slim-server/composer.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"test-apis": "phpunit --testsuite Apis",
2828
"test-models": "phpunit --testsuite Models",
29-
"phpcs": "phpcs ./ --ignore=vendor --warning-severity=0 --standard={{phpcsStandard}}",
29+
"phpcs": "phpcs",
3030
"phplint": "phplint ./ --exclude=vendor"
3131
}
3232
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="{{appName}} Config" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>PHP_CodeSniffer config for {{appName}}</description>
4+
5+
<!-- Path to inspected files -->
6+
<file>./</file>
7+
8+
<!-- Don't need to inspect installed packages -->
9+
<exclude-pattern>./vendor</exclude-pattern>
10+
11+
<!-- <basepath> A path to strip from the front of file paths inside reports -->
12+
<arg name="basepath" value="."/>
13+
14+
<!-- colors Use colors in output -->
15+
<arg name="colors"/>
16+
17+
<!-- Do not print warnings -->
18+
<!-- <arg name="warning-severity" value="0"/> -->
19+
20+
<!-- -p Show progress of the run -->
21+
<!-- -s Show sniff codes in all reports -->
22+
<arg value="ps"/>
23+
24+
<!-- Include the whole PSR12 standard -->
25+
<rule ref="PSR12">
26+
<!-- There is no way to wrap generated comments, just disable that rule for now -->
27+
<exclude name="Generic.Files.LineLength.TooLong" />
28+
<!-- Codegen generates variables with underscore on purpose -->
29+
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore" />
30+
</rule>
31+
</ruleset>

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/PhpSlimServerOptionsProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.openapitools.codegen.CodegenConstants;
2121
import org.openapitools.codegen.languages.AbstractPhpCodegen;
22-
import org.openapitools.codegen.languages.PhpSlimServerCodegen;
2322

2423
import com.google.common.collect.ImmutableMap;
2524

@@ -39,7 +38,6 @@ public class PhpSlimServerOptionsProvider implements OptionsProvider {
3938
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
4039
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
4140
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
42-
public static final String PHPCS_STANDARD_VALUE = "PSR12";
4341

4442
@Override
4543
public String getLanguage() {
@@ -62,7 +60,6 @@ public Map<String, String> createOptions() {
6260
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
6361
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
6462
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
65-
.put(PhpSlimServerCodegen.PHPCS_STANDARD, PHPCS_STANDARD_VALUE)
6663
.build();
6764
}
6865

modules/openapi-generator/src/test/java/org/openapitools/codegen/slim/PhpSlimServerOptionsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ protected void setExpectations() {
6363
times = 1;
6464
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(PhpSlimServerOptionsProvider.SORT_PARAMS_VALUE));
6565
times = 1;
66-
clientCodegen.setPhpcsStandard(PhpSlimServerOptionsProvider.PHPCS_STANDARD_VALUE);
67-
times = 1;
6866
}};
6967
}
7068
}

samples/server/petstore-security-test/php-slim/.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ composer.phar
88
# composer.lock
99

1010
# phplint tool creates cache file which is not necessary in a codebase
11-
/.phplint-cache
11+
/.phplint-cache
12+
13+
# Do not commit local PHPUnit config
14+
/phpunit.xml
15+
16+
# Do not commit local PHP_CodeSniffer config
17+
/phpcs.xml

samples/server/petstore-security-test/php-slim/README.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,54 @@ $ php -S localhost:8888 -t php-slim-server
2828
> It may also be useful for testing purposes or for application demonstrations that are run in controlled environments.
2929
> It is not intended to be a full-featured web server. It should not be used on a public network.
3030
31-
## Run tests
31+
## Tests
3232

33-
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).
33+
### PHPUnit
34+
35+
This package uses PHPUnit 6 or 7(depends from your PHP version) for unit testing.
3436
[Test folder](test) contains templates which you can fill with real test assertions.
3537
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).
36-
How to configure PHP CodeSniffer read at [PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki).
37-
There is [phplint](https://github.com/overtrue/phplint) tool to check php syntax automatically.
38-
39-
Command | Tool | Target
40-
---- | ---- | ----
41-
`$ composer test` | PHPUnit | All tests
42-
`$ composer run test-apis` | PHPUnit | Apis tests
43-
`$ composer run test-models` | PHPUnit | Models tests
44-
`$ composer run phpcs` | PHP CodeSniffer | All files
45-
`$ composer run phplint` | phplint | All files
38+
39+
#### Run
40+
41+
Command | Target
42+
---- | ----
43+
`$ composer test` | All tests
44+
`$ composer test-apis` | Apis tests
45+
`$ composer test-models` | Models tests
46+
47+
#### Config
48+
49+
Package contains fully functional config `./phpunit.xml.dist` file. Create `./phpunit.xml` in root folder to override it.
50+
51+
Quote from [3. The Command-Line Test Runner — PHPUnit 7.4 Manual](https://phpunit.readthedocs.io/en/7.4/textui.html#command-line-options):
52+
53+
> 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.
54+
55+
### PHP CodeSniffer
56+
57+
[PHP CodeSniffer Documentation](https://github.com/squizlabs/PHP_CodeSniffer/wiki). This tool helps to follow coding style and avoid common PHP coding mistakes.
58+
59+
#### Run
60+
61+
```bash
62+
$ composer phpcs
63+
```
64+
65+
#### Config
66+
67+
Package contains fully functional config `./phpcs.xml.dist` file. It checks source code against PSR-1 and PSR-2 coding standards.
68+
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)
69+
70+
### PHPLint
71+
72+
[PHPLint Documentation](https://github.com/overtrue/phplint). Checks PHP syntax only.
73+
74+
#### Run
75+
76+
```bash
77+
$ composer phplint
78+
```
4679

4780
## Show errors
4881

0 commit comments

Comments
 (0)