From 0b13c2bd71fb738e4798ccbe8b23b34cffde4c96 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 1 Jul 2025 16:55:26 -0300 Subject: [PATCH] PEAR/ObjectOperatorIndent: improve end of statement detection when getting the next object operator This commit fixes a bug where `PEAR.WhiteSpace.ObjectOperatorIndent` would generate a false positive when checking multiple chained object operators in a multidimensional array. The problem is that the code was using the `$local` parameter of `findNext()` to limit the scope of the search when reassigning `$next` to the next object operator, and `$local` only considers a semicolon as the end of a statement. Different chained calls are not necessarily separated by a semicolon (as shown in the test added in this commit). This means that the sniff currently considers the object operators on the second and third chained method calls as part of the first chained method call. The sniff checks their indentation using the indentation of the first element on the first chained call as its base. To fix this problem, instead of relying on the `$local` parameter to find the end of the statement, the end of the statement as defined by `File::findEndOfStatement()` is now used. --- .../Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php | 5 +---- .../Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc | 11 +++++++++++ .../WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed | 11 +++++++++++ .../Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php b/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php index 9c64c328af..9b3c1df130 100644 --- a/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php +++ b/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php @@ -191,10 +191,7 @@ public function process(File $phpcsFile, $stackPtr) $next = $phpcsFile->findNext( $this->targets, ($next + 1), - null, - false, - null, - true + $end ); }//end while diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc index b1b09d9323..c5ced0a378 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc @@ -140,3 +140,14 @@ $someObject ->someOtherFunc(nameA: 23, nameB: 42) ->endSomething($value, name: $value) ->endEverything(); + +// Issue https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1154 +$array = [ + [ + $item->one()->two(), + ], + $item->one() + ->two(), + $item->one() + ->two(), +]; diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed index 5d5b77bef6..48419d9056 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed @@ -140,3 +140,14 @@ $someObject ->someOtherFunc(nameA: 23, nameB: 42) ->endSomething($value, name: $value) ->endEverything(); + +// Issue https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1154 +$array = [ + [ + $item->one()->two(), + ], + $item->one() + ->two(), + $item->one() + ->two(), +]; diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php index 9beb77feab..f8a1e9894a 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php +++ b/src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php @@ -56,6 +56,7 @@ public function getErrorList() 140 => 1, 141 => 1, 142 => 1, + 152 => 1, ]; }//end getErrorList()