Skip to content

Commit ab80779

Browse files
authored
Merge pull request #327 from simPod/sniffie
feat: enable SlevomatCodingStandard.TypeHints.ClassConstantTypeHint
2 parents 67d8f07 + 7c1fef7 commit ab80779

12 files changed

+266
-29
lines changed

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ update-compatibility-patch-80:
2424
@git diff -- tests/expected_report.txt tests/fixed tests/input > .tmp-patch && mv .tmp-patch tests/php80-compatibility.patch && git apply -R tests/php80-compatibility.patch
2525
@git commit -m 'Update compatibility patch' tests/php80-compatibility.patch
2626

27+
update-compatibility-patch-81:
28+
@git apply tests/php81-compatibility.patch --allow-empty
29+
@printf "Please open your editor and apply your changes\n"
30+
@until [ "$${compatibility_resolved}" == "y" ]; do read -p "Have finished your changes (y|n)? " compatibility_resolved; done && compatibility_resolved=
31+
@git diff -- tests/expected_report.txt tests/fixed tests/input > .tmp-patch && mv .tmp-patch tests/php81-compatibility.patch && git apply -R tests/php81-compatibility.patch --allow-empty
32+
@git commit -m 'Update compatibility patch' tests/php81-compatibility.patch
33+
34+
update-compatibility-patch-82:
35+
@git apply tests/php82-compatibility.patch --allow-empty
36+
@printf "Please open your editor and apply your changes\n"
37+
@until [ "$${compatibility_resolved}" == "y" ]; do read -p "Have finished your changes (y|n)? " compatibility_resolved; done && compatibility_resolved=
38+
@git diff -- tests/expected_report.txt tests/fixed tests/input > .tmp-patch && mv .tmp-patch tests/php82-compatibility.patch && git apply -R tests/php82-compatibility.patch --allow-empty
39+
@git commit -m 'Update compatibility patch' tests/php82-compatibility.patch
40+
2741
vendor: composer.json
2842
composer update
2943
touch -c vendor

lib/Doctrine/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@
403403
<rule ref="SlevomatCodingStandard.PHP.ShortList"/>
404404
<!-- Forbid use of longhand cast operators -->
405405
<rule ref="SlevomatCodingStandard.PHP.TypeCast"/>
406+
<!-- Check for missing typehints in case they can be declared natively. Report useless @var annotation (or whole documentation comment) because the type of constant is always clear. -->
407+
<rule ref="SlevomatCodingStandard.TypeHints.ClassConstantTypeHint"/>
406408
<!-- Require presence of declare(strict_types=1) -->
407409
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
408410
<properties>

tests/expected_report.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ tests/input/class-references.php 10 2
1313
tests/input/ClassPropertySpacing.php 2 0
1414
tests/input/concatenation_spacing.php 49 0
1515
tests/input/constants-no-lsb.php 2 0
16-
tests/input/constants-var.php 6 0
16+
tests/input/constants-var.php 13 0
1717
tests/input/ControlStructures.php 28 0
1818
tests/input/doc-comment-spacing.php 11 0
1919
tests/input/duplicate-assignment-variable.php 1 0
2020
tests/input/EarlyReturn.php 7 0
21-
tests/input/example-class.php 47 0
21+
tests/input/example-class.php 48 0
2222
tests/input/ExampleBackedEnum.php 5 0
2323
tests/input/Exceptions.php 1 0
2424
tests/input/forbidden-comments.php 14 0
@@ -54,9 +54,9 @@ tests/input/use-ordering.php 1 0
5454
tests/input/useless-semicolon.php 2 0
5555
tests/input/UselessConditions.php 21 0
5656
----------------------------------------------------------------------
57-
A TOTAL OF 468 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
57+
A TOTAL OF 476 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
5858
----------------------------------------------------------------------
59-
PHPCBF CAN FIX 384 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59+
PHPCBF CAN FIX 391 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6060
----------------------------------------------------------------------
6161

6262

tests/fixed/ControlStructures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ControlStructures
1313
{
14-
private const VERSION = PHP_VERSION;
14+
private const int VERSION = PHP_VERSION;
1515

1616
/** @return iterable<int> */
1717
public function varAndIfNoSpaceBetween(): iterable

tests/fixed/constants-no-lsb.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Test;
66

7+
// phpcs:disable SlevomatCodingStandard.TypeHints.ClassConstantTypeHint
8+
79
class ConstantLsb
810
{
911
public const A = 123;

tests/fixed/constants-var.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313

1414
class Bar
1515
{
16-
public const BAZ = 456;
16+
public const int BAZ = 456;
1717

18-
protected const PROPERTY_1 = '1';
19-
protected const PROPERTY_2 = '2';
18+
protected const string PROPERTY_1 = '1';
19+
protected const string PROPERTY_2 = '2';
2020
}
2121

2222
class Spacing
2323
{
24-
public const FOO = 'bar';
25-
public const BAR = 'bar';
24+
public const string FOO = 'bar';
25+
public const string BAR = 'bar';
2626

27-
public const BAZ = 'baz';
27+
public const string BAZ = 'baz';
2828

2929
/** Brevis, primus coordinataes foris promissio de varius, barbatus heuretes. */
30-
private const BAM = 1234;
30+
private const int BAM = 1234;
3131
}

tests/input/ControlStructures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ControlStructures
1313
{
14-
private const VERSION = PHP_VERSION;
14+
private const int VERSION = PHP_VERSION;
1515

1616
/** @return iterable<int> */
1717
public function varAndIfNoSpaceBetween(): iterable

tests/input/constants-no-lsb.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Test;
66

7+
// phpcs:disable SlevomatCodingStandard.TypeHints.ClassConstantTypeHint
8+
79
class ConstantLsb
810
{
911
public const A = 123;

tests/php74-compatibility.patch

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 087995d..5284a56 100644
2+
index 8547171..f01ece0 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -17,26 +17,23 @@ tests/input/constants-var.php 6 0
5+
@@ -13,30 +13,27 @@ tests/input/class-references.php 10 2
6+
tests/input/ClassPropertySpacing.php 2 0
7+
tests/input/concatenation_spacing.php 49 0
8+
tests/input/constants-no-lsb.php 2 0
9+
-tests/input/constants-var.php 13 0
10+
+tests/input/constants-var.php 7 0
611
tests/input/ControlStructures.php 28 0
712
tests/input/doc-comment-spacing.php 11 0
813
tests/input/duplicate-assignment-variable.php 1 0
914
-tests/input/EarlyReturn.php 7 0
10-
-tests/input/example-class.php 47 0
15+
-tests/input/example-class.php 48 0
1116
-tests/input/ExampleBackedEnum.php 5 0
1217
-tests/input/Exceptions.php 1 0
1318
+tests/input/EarlyReturn.php 6 0
@@ -50,18 +55,27 @@ index 087995d..5284a56 100644
5055
-tests/input/UselessConditions.php 21 0
5156
+tests/input/UselessConditions.php 20 0
5257
----------------------------------------------------------------------
53-
-A TOTAL OF 468 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
54-
+A TOTAL OF 428 ERRORS AND 2 WARNINGS WERE FOUND IN 47 FILES
58+
-A TOTAL OF 476 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
59+
+A TOTAL OF 429 ERRORS AND 2 WARNINGS WERE FOUND IN 47 FILES
5560
----------------------------------------------------------------------
56-
-PHPCBF CAN FIX 384 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57-
+PHPCBF CAN FIX 344 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
61+
-PHPCBF CAN FIX 391 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
62+
+PHPCBF CAN FIX 345 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5863
----------------------------------------------------------------------
5964

6065

6166
diff --git a/tests/fixed/ControlStructures.php b/tests/fixed/ControlStructures.php
62-
index f8f7f65..a653086 100644
67+
index 480e419..a653086 100644
6368
--- a/tests/fixed/ControlStructures.php
6469
+++ b/tests/fixed/ControlStructures.php
70+
@@ -11,7 +11,7 @@ use const PHP_VERSION;
71+
72+
class ControlStructures
73+
{
74+
- private const int VERSION = PHP_VERSION;
75+
+ private const VERSION = PHP_VERSION;
76+
77+
/** @return iterable<int> */
78+
public function varAndIfNoSpaceBetween(): iterable
6579
@@ -104,7 +104,7 @@ class ControlStructures
6680

6781
try {
@@ -192,6 +206,37 @@ index 71e0cfb..2151b17 100644
192206
{
193207
if (! $this->isAdmin) {
194208
return null;
209+
diff --git a/tests/fixed/constants-var.php b/tests/fixed/constants-var.php
210+
index f10c235..d4268cb 100644
211+
--- a/tests/fixed/constants-var.php
212+
+++ b/tests/fixed/constants-var.php
213+
@@ -13,19 +13,19 @@ const BAR_1 = 2;
214+
215+
class Bar
216+
{
217+
- public const int BAZ = 456;
218+
+ public const BAZ = 456;
219+
220+
- protected const string PROPERTY_1 = '1';
221+
- protected const string PROPERTY_2 = '2';
222+
+ protected const PROPERTY_1 = '1';
223+
+ protected const PROPERTY_2 = '2';
224+
}
225+
226+
class Spacing
227+
{
228+
- public const string FOO = 'bar';
229+
- public const string BAR = 'bar';
230+
+ public const FOO = 'bar';
231+
+ public const BAR = 'bar';
232+
233+
- public const string BAZ = 'baz';
234+
+ public const BAZ = 'baz';
235+
236+
/** Brevis, primus coordinataes foris promissio de varius, barbatus heuretes. */
237+
- private const int BAM = 1234;
238+
+ private const BAM = 1234;
239+
}
195240
diff --git a/tests/fixed/example-class.php b/tests/fixed/example-class.php
196241
index 4988dab..c31e53f 100644
197242
--- a/tests/fixed/example-class.php
@@ -349,9 +394,18 @@ index 5e26ed8..bfa6d4f 100644
349394
+ private $x = 1;
350395
}
351396
diff --git a/tests/input/ControlStructures.php b/tests/input/ControlStructures.php
352-
index 73944e3..a0e0b2e 100644
397+
index 7c20d0e..a0e0b2e 100644
353398
--- a/tests/input/ControlStructures.php
354399
+++ b/tests/input/ControlStructures.php
400+
@@ -11,7 +11,7 @@ use const PHP_VERSION;
401+
402+
class ControlStructures
403+
{
404+
- private const int VERSION = PHP_VERSION;
405+
+ private const VERSION = PHP_VERSION;
406+
407+
/** @return iterable<int> */
408+
public function varAndIfNoSpaceBetween(): iterable
355409
@@ -93,7 +93,7 @@ class ControlStructures
356410
}
357411
try {

tests/php80-compatibility.patch

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 087995d..f99aa86 100644
2+
index 9b19b9e..760ee4a 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -19,7 +19,6 @@ tests/input/doc-comment-spacing.php 11 0
5+
@@ -13,13 +13,12 @@ tests/input/class-references.php 10 2
6+
tests/input/ClassPropertySpacing.php 2 0
7+
tests/input/concatenation_spacing.php 49 0
8+
tests/input/constants-no-lsb.php 2 0
9+
-tests/input/constants-var.php 13 0
10+
+tests/input/constants-var.php 7 0
11+
tests/input/ControlStructures.php 28 0
12+
tests/input/doc-comment-spacing.php 11 0
613
tests/input/duplicate-assignment-variable.php 1 0
714
tests/input/EarlyReturn.php 7 0
8-
tests/input/example-class.php 47 0
15+
-tests/input/example-class.php 48 0
916
-tests/input/ExampleBackedEnum.php 5 0
17+
+tests/input/example-class.php 47 0
1018
tests/input/Exceptions.php 1 0
1119
tests/input/forbidden-comments.php 14 0
1220
tests/input/forbidden-functions.php 6 0
@@ -23,11 +31,11 @@ index 087995d..f99aa86 100644
2331
tests/input/useless-semicolon.php 2 0
2432
tests/input/UselessConditions.php 21 0
2533
----------------------------------------------------------------------
26-
-A TOTAL OF 468 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
27-
+A TOTAL OF 458 ERRORS AND 2 WARNINGS WERE FOUND IN 49 FILES
34+
-A TOTAL OF 476 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
35+
+A TOTAL OF 459 ERRORS AND 2 WARNINGS WERE FOUND IN 49 FILES
2836
----------------------------------------------------------------------
29-
-PHPCBF CAN FIX 384 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
30-
+PHPCBF CAN FIX 374 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
37+
-PHPCBF CAN FIX 391 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
38+
+PHPCBF CAN FIX 375 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
3139
----------------------------------------------------------------------
3240

3341

@@ -63,6 +71,37 @@ index 9703897..7821d5c 100644
6371
) {
6472
}
6573
}
74+
diff --git a/tests/fixed/constants-var.php b/tests/fixed/constants-var.php
75+
index f10c235..d4268cb 100644
76+
--- a/tests/fixed/constants-var.php
77+
+++ b/tests/fixed/constants-var.php
78+
@@ -13,19 +13,19 @@ const BAR_1 = 2;
79+
80+
class Bar
81+
{
82+
- public const int BAZ = 456;
83+
+ public const BAZ = 456;
84+
85+
- protected const string PROPERTY_1 = '1';
86+
- protected const string PROPERTY_2 = '2';
87+
+ protected const PROPERTY_1 = '1';
88+
+ protected const PROPERTY_2 = '2';
89+
}
90+
91+
class Spacing
92+
{
93+
- public const string FOO = 'bar';
94+
- public const string BAR = 'bar';
95+
+ public const FOO = 'bar';
96+
+ public const BAR = 'bar';
97+
98+
- public const string BAZ = 'baz';
99+
+ public const BAZ = 'baz';
100+
101+
/** Brevis, primus coordinataes foris promissio de varius, barbatus heuretes. */
102+
- private const int BAM = 1234;
103+
+ private const BAM = 1234;
104+
}
66105
diff --git a/tests/input/ExampleBackedEnum.php b/tests/input/ExampleBackedEnum.php
67106
index 3b09f47..fe54eb9 100644
68107
--- a/tests/input/ExampleBackedEnum.php

tests/php81-compatibility.patch

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2+
index 8547171..a7a42fa 100644
3+
--- a/tests/expected_report.txt
4+
+++ b/tests/expected_report.txt
5+
@@ -13,12 +13,12 @@ tests/input/class-references.php 10 2
6+
tests/input/ClassPropertySpacing.php 2 0
7+
tests/input/concatenation_spacing.php 49 0
8+
tests/input/constants-no-lsb.php 2 0
9+
-tests/input/constants-var.php 13 0
10+
+tests/input/constants-var.php 7 0
11+
tests/input/ControlStructures.php 28 0
12+
tests/input/doc-comment-spacing.php 11 0
13+
tests/input/duplicate-assignment-variable.php 1 0
14+
tests/input/EarlyReturn.php 7 0
15+
-tests/input/example-class.php 48 0
16+
+tests/input/example-class.php 47 0
17+
tests/input/ExampleBackedEnum.php 5 0
18+
tests/input/Exceptions.php 1 0
19+
tests/input/forbidden-comments.php 14 0
20+
@@ -54,9 +54,9 @@ tests/input/use-ordering.php 1 0
21+
tests/input/useless-semicolon.php 2 0
22+
tests/input/UselessConditions.php 21 0
23+
----------------------------------------------------------------------
24+
-A TOTAL OF 476 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
25+
+A TOTAL OF 469 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
26+
----------------------------------------------------------------------
27+
-PHPCBF CAN FIX 391 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
28+
+PHPCBF CAN FIX 385 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29+
----------------------------------------------------------------------
30+
31+
32+
diff --git a/tests/fixed/constants-var.php b/tests/fixed/constants-var.php
33+
index f10c235..d4268cb 100644
34+
--- a/tests/fixed/constants-var.php
35+
+++ b/tests/fixed/constants-var.php
36+
@@ -13,19 +13,19 @@ const BAR_1 = 2;
37+
38+
class Bar
39+
{
40+
- public const int BAZ = 456;
41+
+ public const BAZ = 456;
42+
43+
- protected const string PROPERTY_1 = '1';
44+
- protected const string PROPERTY_2 = '2';
45+
+ protected const PROPERTY_1 = '1';
46+
+ protected const PROPERTY_2 = '2';
47+
}
48+
49+
class Spacing
50+
{
51+
- public const string FOO = 'bar';
52+
- public const string BAR = 'bar';
53+
+ public const FOO = 'bar';
54+
+ public const BAR = 'bar';
55+
56+
- public const string BAZ = 'baz';
57+
+ public const BAZ = 'baz';
58+
59+
/** Brevis, primus coordinataes foris promissio de varius, barbatus heuretes. */
60+
- private const int BAM = 1234;
61+
+ private const BAM = 1234;
62+
}

0 commit comments

Comments
 (0)