Skip to content

Commit 9056d24

Browse files
authored
Merge pull request #144 from simPod/upgrade-slevomatcs
Upgrade slevomat/coding-standard to v6
2 parents ae0778a + c53c4bd commit 9056d24

12 files changed

+212
-19
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ install: travis_retry composer update --prefer-dist
2020

2121
script:
2222
- vendor/bin/phpcs
23-
- vendor/bin/phpcs $(find tests/input/* | sort) --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log
23+
- make test-report
2424

2525
stages:
2626
- Validate against schema
@@ -41,6 +41,8 @@ jobs:
4141
- xmllint --noout --schema vendor/squizlabs/php_codesniffer/phpcs.xsd lib/Doctrine/ruleset.xml
4242

4343
- stage: Apply fixes
44-
before_script:
45-
- cp -R tests/input/ tests/input2/
46-
script: vendor/bin/phpcbf tests/input2; [ $? -ne 2 ] && diff tests/input2 tests/fixed
44+
script: make test-fix
45+
46+
- stage: Apply fixes
47+
php: 7.4snapshot
48+
script: make test-fix

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.PHONY: test test-report test-fix update-compatibility-patch
2+
3+
PHP_74_OR_NEWER=`php -r "echo (int) version_compare(PHP_VERSION, '7.4', '>=');"`
4+
5+
test: test-report test-fix
6+
7+
test-report: vendor
8+
@if [ $(PHP_74_OR_NEWER) -eq 1 ]; then git apply tests/php-compatibility.patch; fi
9+
@vendor/bin/phpcs `find tests/input/* | sort` --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log; if [ $$? -ne 0 ] && [ $(PHP_74_OR_NEWER) -eq 1 ]; then git apply -R tests/php-compatibility.patch; exit 1; fi
10+
@if [ $(PHP_74_OR_NEWER) -eq 1 ]; then git apply -R tests/php-compatibility.patch; fi
11+
12+
test-fix: vendor
13+
@if [ $(PHP_74_OR_NEWER) -eq 1 ]; then git apply tests/php-compatibility.patch; fi
14+
@cp -R tests/input/ tests/input2/
15+
@vendor/bin/phpcbf tests/input2; diff tests/input2 tests/fixed; if [ $$? -ne 0 ]; then rm -rf tests/input2/ && if [ $(PHP_74_OR_NEWER) -eq 1 ]; then git apply -R tests/php-compatibility.patch; fi; exit 1; fi
16+
@rm -rf tests/input2/ && if [ $(PHP_74_OR_NEWER) -eq 1 ]; then git apply -R tests/php-compatibility.patch; fi
17+
18+
update-compatibility-patch:
19+
@git apply tests/php-compatibility.patch
20+
@echo -e "Please open your editor and apply your changes\n"
21+
@until [ "$${compatibility_resolved}" == "y" ]; do read -p "Have finished your changes (y|n)? " compatibility_resolved; done && compatibility_resolved=
22+
@git diff -- tests/expected_report.txt tests/fixed > .tmp-patch && mv .tmp-patch tests/php-compatibility.patch && git apply -R tests/php-compatibility.patch
23+
@git commit -m 'Update compatibility patch' tests/php-compatibility.patch
24+
25+
vendor: composer.json
26+
composer update

bin/test.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"require": {
2929
"php": "^7.2",
3030
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
31-
"slevomat/coding-standard": "dev-master#469368cc6d8fe83aceba259ef65f1c0a2f63055b",
32-
"squizlabs/php_codesniffer": "^3.5.0"
31+
"slevomat/coding-standard": "^6.0",
32+
"squizlabs/php_codesniffer": "^3.5.3"
3333
},
3434
"config": {
3535
"sort-packages": true

docs/en/reference/index.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,22 @@ Testing
127127
=======
128128

129129
If you are contributing to the Doctrine Coding Standard and want to test your contribution, you just
130-
need to execute PHP_CodeSniffer with the tests folder and ensure it matches the expected report:
130+
need to execute the ``test`` GNU make target:
131131

132132
.. code-block:: bash
133133
134-
$ ./vendor/bin/phpcs tests/input --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log
134+
$ make test
135+
136+
Sometimes you are enabling a sniff that enforces some features for a specific PHP version, that will
137+
probably cause our build pipeline to fail when running on such versions.
138+
139+
We have created the ``update-compatibility-patch`` GNU make target to help you with that:
140+
141+
.. code-block:: bash
142+
143+
$ make update-compatibility-patch
144+
145+
That target will apply the latest version of our compatibility patch and ask you to adjust the code in ``tests/fixed`` and
146+
``tests/expected_report.txt`` using the editor of your preference.
147+
Once you are done, please type "y" to resume the command, which will update the compatibility patch and create a commit
148+
with your changes for you to push to your remote branch.

lib/Doctrine/ruleset.xml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@
206206
<!-- Forbid assignments in conditions -->
207207
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
208208
<!-- Require consistent spacing for block structures -->
209-
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing"/>
209+
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing">
210+
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeControlStructure" />
211+
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeFirstControlStructure" />
212+
</rule>
210213
<!-- Forbid fancy yoda conditions -->
211214
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
212215
<!-- Require usage of early exit -->
@@ -334,17 +337,51 @@
334337
<!-- Require types to be written as natively if possible;
335338
require iterable types to specify phpDoc with their content;
336339
forbid useless/duplicated information in phpDoc -->
337-
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration">
340+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
341+
<properties>
342+
<property name="traversableTypeHints" type="array">
343+
<element value="Traversable"/>
344+
<element value="Iterator"/>
345+
<element value="IteratorAggregate"/>
346+
<element value="Doctrine\Common\Collections\Collection"/>
347+
</property>
348+
</properties>
349+
</rule>
350+
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
338351
<properties>
339-
<property name="allAnnotationsAreUseful" value="true"/>
340-
<property name="enableEachParameterAndReturnInspection" value="true"/>
341352
<property name="traversableTypeHints" type="array">
353+
<element value="Traversable"/>
354+
<element value="Iterator"/>
355+
<element value="IteratorAggregate"/>
356+
<element value="Doctrine\Common\Collections\Collection"/>
357+
</property>
358+
</properties>
359+
</rule>
360+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
361+
<properties>
362+
<property name="traversableTypeHints" type="array">
363+
<element value="Traversable"/>
364+
<element value="Iterator"/>
365+
<element value="IteratorAggregate"/>
342366
<element value="Doctrine\Common\Collections\Collection"/>
343367
</property>
344368
</properties>
345369
</rule>
346370
<!-- Forbid useless @var for constants -->
347371
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
372+
<!-- Forbid useless phpDocs for functions -->
373+
<rule ref="SlevomatCodingStandard.Commenting.UselessFunctionDocComment">
374+
<properties>
375+
<property name="traversableTypeHints" type="array">
376+
<element value="Traversable"/>
377+
<element value="Iterator"/>
378+
<element value="IteratorAggregate"/>
379+
<element value="Doctrine\Common\Collections\Collection"/>
380+
</property>
381+
</properties>
382+
</rule>
383+
<!-- Forbid useless inherit doc comment -->
384+
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment"/>
348385
<!-- Forbid duplicated variables assignments -->
349386
<rule ref="SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable"/>
350387
<!-- Forbid useless variables -->

tests/expected_report.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ tests/input/constants-var.php 4 0
1212
tests/input/doc-comment-spacing.php 10 0
1313
tests/input/duplicate-assignment-variable.php 1 0
1414
tests/input/EarlyReturn.php 6 0
15-
tests/input/example-class.php 48 0
15+
tests/input/example-class.php 45 0
1616
tests/input/forbidden-comments.php 8 0
1717
tests/input/forbidden-functions.php 6 0
1818
tests/input/inline_type_hint_assertions.php 7 0
@@ -32,14 +32,15 @@ tests/input/superfluous-naming.php 11 0
3232
tests/input/test-case.php 8 0
3333
tests/input/trailing_comma_on_array.php 1 0
3434
tests/input/traits-uses.php 11 0
35+
tests/input/type-hints.php 4 0
3536
tests/input/UnusedVariables.php 1 0
3637
tests/input/use-ordering.php 1 0
3738
tests/input/useless-semicolon.php 2 0
3839
tests/input/UselessConditions.php 20 0
3940
----------------------------------------------------------------------
40-
A TOTAL OF 289 ERRORS AND 0 WARNINGS WERE FOUND IN 33 FILES
41+
A TOTAL OF 290 ERRORS AND 0 WARNINGS WERE FOUND IN 34 FILES
4142
----------------------------------------------------------------------
42-
PHPCBF CAN FIX 232 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
43+
PHPCBF CAN FIX 229 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
4344
----------------------------------------------------------------------
4445

4546

tests/fixed/example-class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public function throwWhenInvalid() : void
127127
public function trySwitchSpace() : void
128128
{
129129
try {
130+
$var = 1;
130131
switch (self::VERSION) {
131132
default:
132133
}

tests/fixed/type-hints.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeHints;
6+
7+
use Iterator;
8+
use Traversable;
9+
10+
class TraversableTypeHints
11+
{
12+
/** @var Traversable */
13+
private $parameter;
14+
15+
/**
16+
* @param Iterator $iterator
17+
*
18+
* @return Traversable
19+
*/
20+
public function get(Iterator $iterator) : Traversable
21+
{
22+
return $this->parameter;
23+
}
24+
}

tests/input/example-class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function throwWhenInvalid() : void
122122
public function trySwitchSpace() : void
123123
{
124124
try {
125+
$var = 1;
125126
switch (self::VERSION) {
126127
default:
127128
}

tests/input/type-hints.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeHints;
6+
7+
use Iterator;
8+
use Traversable;
9+
10+
class TraversableTypeHints
11+
{
12+
/** @var Traversable */
13+
private $parameter;
14+
15+
/**
16+
* @param Iterator $iterator
17+
*
18+
* @return Traversable
19+
*/
20+
public function get(Iterator $iterator) : Traversable
21+
{
22+
return $this->parameter;
23+
}
24+
}

tests/php-compatibility.patch

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2+
index 855edfd..5653a39 100644
3+
--- a/tests/expected_report.txt
4+
+++ b/tests/expected_report.txt
5+
@@ -12,7 +12,7 @@ tests/input/constants-var.php 4 0
6+
tests/input/doc-comment-spacing.php 10 0
7+
tests/input/duplicate-assignment-variable.php 1 0
8+
tests/input/EarlyReturn.php 6 0
9+
-tests/input/example-class.php 45 0
10+
+tests/input/example-class.php 48 0
11+
tests/input/forbidden-comments.php 8 0
12+
tests/input/forbidden-functions.php 6 0
13+
tests/input/inline_type_hint_assertions.php 7 0
14+
@@ -32,15 +32,15 @@ tests/input/superfluous-naming.php 11 0
15+
tests/input/test-case.php 8 0
16+
tests/input/trailing_comma_on_array.php 1 0
17+
tests/input/traits-uses.php 11 0
18+
-tests/input/type-hints.php 4 0
19+
+tests/input/type-hints.php 5 0
20+
tests/input/UnusedVariables.php 1 0
21+
tests/input/use-ordering.php 1 0
22+
tests/input/useless-semicolon.php 2 0
23+
tests/input/UselessConditions.php 20 0
24+
----------------------------------------------------------------------
25+
-A TOTAL OF 290 ERRORS AND 0 WARNINGS WERE FOUND IN 34 FILES
26+
+A TOTAL OF 294 ERRORS AND 0 WARNINGS WERE FOUND IN 34 FILES
27+
----------------------------------------------------------------------
28+
-PHPCBF CAN FIX 229 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29+
+PHPCBF CAN FIX 233 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
30+
----------------------------------------------------------------------
31+
32+
33+
diff --git a/tests/fixed/example-class.php b/tests/fixed/example-class.php
34+
index 195d963..a2ee5c8 100644
35+
--- a/tests/fixed/example-class.php
36+
+++ b/tests/fixed/example-class.php
37+
@@ -24,14 +24,12 @@ class Example implements IteratorAggregate
38+
{
39+
private const VERSION = PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;
40+
41+
- /** @var int|null */
42+
- private $foo;
43+
+ private ?int $foo = null;
44+
45+
/** @var string[] */
46+
- private $bar;
47+
+ private array $bar;
48+
49+
- /** @var bool */
50+
- private $baz;
51+
+ private bool $baz;
52+
53+
/** @var ControlStructureSniff|int|string|null */
54+
private $baxBax;
55+
diff --git a/tests/fixed/type-hints.php b/tests/fixed/type-hints.php
56+
index a1b1827..fb7d406 100644
57+
--- a/tests/fixed/type-hints.php
58+
+++ b/tests/fixed/type-hints.php
59+
@@ -10,7 +10,7 @@ use Traversable;
60+
class TraversableTypeHints
61+
{
62+
/** @var Traversable */
63+
- private $parameter;
64+
+ private Traversable $parameter;
65+
66+
/**
67+
* @param Iterator $iterator

0 commit comments

Comments
 (0)