Skip to content

Commit 21956b2

Browse files
committed
Disallow string property fetch
While we're unlikely to manually write such things, there are custom rectors I've been writing that do so, and massively.
1 parent 1781135 commit 21956b2

9 files changed

+38
-23
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"require": {
2525
"php": "^7.2 || ^8.0",
2626
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7 || ^1.0.0",
27-
"slevomat/coding-standard": "^8.6.2",
27+
"slevomat/coding-standard": "^8.10.0",
2828
"squizlabs/php_codesniffer": "^3.7"
2929
},
3030
"config": {

lib/Doctrine/ruleset.xml

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@
136136
<rule ref="SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants"/>
137137
<!-- Forbid more than one constant declared per statement -->
138138
<rule ref="SlevomatCodingStandard.Classes.DisallowMultiConstantDefinition"/>
139+
<!-- Forbid string expression property fetch ($foo->{'bar'}) -->
140+
<rule ref="SlevomatCodingStandard.Classes.DisallowStringExpressionPropertyFetch"/>
139141
<!-- Forbid empty lines around type declarations -->
140142
<rule ref="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces">
141143
<properties>

tests/expected_report.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tests/input/semicolon_spacing.php 3 0
4040
tests/input/single-line-array-spacing.php 5 0
4141
tests/input/spread-operator.php 6 0
4242
tests/input/static-closures.php 1 0
43+
tests/input/string_property_fetch.php 1 0
4344
tests/input/strings.php 3 0
4445
tests/input/superfluous-naming.php 11 0
4546
tests/input/test-case.php 8 0
@@ -52,9 +53,9 @@ tests/input/use-ordering.php 1 0
5253
tests/input/useless-semicolon.php 2 0
5354
tests/input/UselessConditions.php 21 0
5455
----------------------------------------------------------------------
55-
A TOTAL OF 453 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
56+
A TOTAL OF 454 ERRORS AND 0 WARNINGS WERE FOUND IN 49 FILES
5657
----------------------------------------------------------------------
57-
PHPCBF CAN FIX 375 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58+
PHPCBF CAN FIX 376 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5859
----------------------------------------------------------------------
5960

6061

tests/fixed/string_property_fetch.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$a = new stdClass();
6+
$a->b = 'hi';

tests/input/string_property_fetch.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$a = new stdClass();
6+
$a->{'b'} = 'hi';

tests/php72-compatibility.patch

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ index d1e1fad..ea3b611 100644
4141
tests/input/semicolon_spacing.php 3 0
4242
tests/input/single-line-array-spacing.php 5 0
4343
tests/input/spread-operator.php 6 0
44-
@@ -44,17 +41,16 @@ tests/input/strings.php 1 0
44+
@@ -43,17 +42,16 @@ tests/input/strings.php 1 0
4545
tests/input/superfluous-naming.php 11 0
4646
tests/input/test-case.php 8 0
4747
tests/input/trailing_comma_on_array.php 1 0
@@ -55,11 +55,11 @@ index d1e1fad..ea3b611 100644
5555
-tests/input/UselessConditions.php 21 0
5656
+tests/input/UselessConditions.php 20 0
5757
----------------------------------------------------------------------
58-
-A TOTAL OF 453 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
59-
+A TOTAL OF 406 ERRORS AND 0 WARNINGS WERE FOUND IN 44 FILES
58+
-A TOTAL OF 454 ERRORS AND 0 WARNINGS WERE FOUND IN 49 FILES
59+
+A TOTAL OF 407 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
6060
----------------------------------------------------------------------
61-
-PHPCBF CAN FIX 375 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
62-
+PHPCBF CAN FIX 328 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
61+
-PHPCBF CAN FIX 376 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
62+
+PHPCBF CAN FIX 329 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6363
----------------------------------------------------------------------
6464

6565

tests/php73-compatibility.patch

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ index d1e1fad..9a78bc1 100644
4141
tests/input/semicolon_spacing.php 3 0
4242
tests/input/single-line-array-spacing.php 5 0
4343
tests/input/spread-operator.php 6 0
44-
@@ -44,17 +41,17 @@ tests/input/strings.php 1 0
44+
@@ -43,17 +42,17 @@ tests/input/strings.php 1 0
4545
tests/input/superfluous-naming.php 11 0
4646
tests/input/test-case.php 8 0
4747
tests/input/trailing_comma_on_array.php 1 0
@@ -56,11 +56,11 @@ index d1e1fad..9a78bc1 100644
5656
-tests/input/UselessConditions.php 21 0
5757
+tests/input/UselessConditions.php 20 0
5858
----------------------------------------------------------------------
59-
-A TOTAL OF 453 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
60-
+A TOTAL OF 408 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
59+
-A TOTAL OF 454 ERRORS AND 0 WARNINGS WERE FOUND IN 49 FILES
60+
+A TOTAL OF 409 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
6161
----------------------------------------------------------------------
62-
-PHPCBF CAN FIX 375 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
63-
+PHPCBF CAN FIX 330 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
62+
-PHPCBF CAN FIX 376 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
63+
+PHPCBF CAN FIX 331 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6464
----------------------------------------------------------------------
6565

6666

tests/php74-compatibility.patch

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ index d1e1fad..ed67841 100644
3939
tests/input/semicolon_spacing.php 3 0
4040
tests/input/single-line-array-spacing.php 5 0
4141
tests/input/spread-operator.php 6 0
42-
@@ -44,17 +41,17 @@ tests/input/strings.php 1 0
42+
@@ -43,17 +42,17 @@ tests/input/strings.php 1 0
4343
tests/input/superfluous-naming.php 11 0
4444
tests/input/test-case.php 8 0
4545
tests/input/trailing_comma_on_array.php 1 0
@@ -54,11 +54,11 @@ index d1e1fad..ed67841 100644
5454
-tests/input/UselessConditions.php 21 0
5555
+tests/input/UselessConditions.php 20 0
5656
----------------------------------------------------------------------
57-
-A TOTAL OF 453 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
58-
+A TOTAL OF 417 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
57+
-A TOTAL OF 454 ERRORS AND 0 WARNINGS WERE FOUND IN 49 FILES
58+
+A TOTAL OF 418 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
5959
----------------------------------------------------------------------
60-
-PHPCBF CAN FIX 375 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
61-
+PHPCBF CAN FIX 339 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
60+
-PHPCBF CAN FIX 376 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
61+
+PHPCBF CAN FIX 340 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6262
----------------------------------------------------------------------
6363

6464

tests/php80-compatibility.patch

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ index d1e1fad..71022c4 100644
2525
tests/input/return_type_on_closures.php 26 0
2626
tests/input/return_type_on_methods.php 22 0
2727
tests/input/semicolon_spacing.php 3 0
28-
@@ -52,9 +51,9 @@ tests/input/use-ordering.php 1 0
28+
@@ -51,9 +52,9 @@ tests/input/use-ordering.php 1 0
2929
tests/input/useless-semicolon.php 2 0
3030
tests/input/UselessConditions.php 21 0
3131
----------------------------------------------------------------------
32-
-A TOTAL OF 453 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
33-
+A TOTAL OF 447 ERRORS AND 0 WARNINGS WERE FOUND IN 47 FILES
32+
-A TOTAL OF 454 ERRORS AND 0 WARNINGS WERE FOUND IN 49 FILES
33+
+A TOTAL OF 448 ERRORS AND 0 WARNINGS WERE FOUND IN 48 FILES
3434
----------------------------------------------------------------------
35-
-PHPCBF CAN FIX 375 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
36-
+PHPCBF CAN FIX 369 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
35+
-PHPCBF CAN FIX 376 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
36+
+PHPCBF CAN FIX 370 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
3737
----------------------------------------------------------------------
3838

3939

0 commit comments

Comments
 (0)