From 616eb38061602fea3d33257a5aa3a1af55076ced Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 1 Aug 2020 01:53:37 +0200 Subject: [PATCH] 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. --- .../Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php | 3 ++- .../Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc | 4 +++- .../Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php b/src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php index c3a8b3915e..cb3778453a 100644 --- a/src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php +++ b/src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php @@ -74,7 +74,8 @@ protected function processIncDec($phpcsFile, $stackPtr) // start looking for other operators. if ($tokens[($stackPtr - 1)]['code'] === T_VARIABLE || ($tokens[($stackPtr - 1)]['code'] === T_STRING - && $tokens[($stackPtr - 2)]['code'] === T_OBJECT_OPERATOR) + && ($tokens[($stackPtr - 2)]['code'] === T_OBJECT_OPERATOR + || $tokens[($stackPtr - 2)]['code'] === T_NULLSAFE_OBJECT_OPERATOR)) ) { $start = ($stackPtr + 1); } else { diff --git a/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc b/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc index e8d3beb94c..a4f82d1a8c 100644 --- a/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc +++ b/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc @@ -37,4 +37,6 @@ $var = (1 + $var); $expected[$i]['sort_order'] = ($i + 1); $expected[($i + 1)]['sort_order'] = ($i + 1); -$id = $id.($this->i++).$id; +$id = $id.($obj->i++).$id; +$id = $obj?->i++.$id; +$id = $obj?->i++*10; diff --git a/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php b/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php index 768911b508..3846905da0 100644 --- a/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php +++ b/src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php @@ -35,6 +35,8 @@ public function getErrorList() 27 => 1, 29 => 1, 31 => 1, + 41 => 1, + 42 => 1, ]; }//end getErrorList()