Skip to content

Commit 616eb38

Browse files
committed
PHP 8.0 | Squiz/IncrementDecrementUsage: allow for nullsafe object operator
Includes unit test. Note: there is quite a lot more which can do with improvement in this sniff, as the sniff is not code style independent, nor does it take all allowed syntaxes into account, such as array in/decrementing `$a[0]++` or nested property in/decrementing `--$obj->prop->nested` and more along those lines, but that's outside the scope of this PR.
1 parent 85f3521 commit 616eb38

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ protected function processIncDec($phpcsFile, $stackPtr)
7474
// start looking for other operators.
7575
if ($tokens[($stackPtr - 1)]['code'] === T_VARIABLE
7676
|| ($tokens[($stackPtr - 1)]['code'] === T_STRING
77-
&& $tokens[($stackPtr - 2)]['code'] === T_OBJECT_OPERATOR)
77+
&& ($tokens[($stackPtr - 2)]['code'] === T_OBJECT_OPERATOR
78+
|| $tokens[($stackPtr - 2)]['code'] === T_NULLSAFE_OBJECT_OPERATOR))
7879
) {
7980
$start = ($stackPtr + 1);
8081
} else {

src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ $var = (1 + $var);
3737
$expected[$i]['sort_order'] = ($i + 1);
3838
$expected[($i + 1)]['sort_order'] = ($i + 1);
3939

40-
$id = $id.($this->i++).$id;
40+
$id = $id.($obj->i++).$id;
41+
$id = $obj?->i++.$id;
42+
$id = $obj?->i++*10;

src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function getErrorList()
3535
27 => 1,
3636
29 => 1,
3737
31 => 1,
38+
41 => 1,
39+
42 => 1,
3840
];
3941

4042
}//end getErrorList()

0 commit comments

Comments
 (0)