Skip to content

Commit 0b13c2b

Browse files
committed
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.
1 parent 368817d commit 0b13c2b

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ public function process(File $phpcsFile, $stackPtr)
191191
$next = $phpcsFile->findNext(
192192
$this->targets,
193193
($next + 1),
194-
null,
195-
false,
196-
null,
197-
true
194+
$end
198195
);
199196
}//end while
200197

src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@ $someObject
140140
->someOtherFunc(nameA: 23, nameB: 42)
141141
->endSomething($value, name: $value)
142142
->endEverything();
143+
144+
// Issue https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1154
145+
$array = [
146+
[
147+
$item->one()->two(),
148+
],
149+
$item->one()
150+
->two(),
151+
$item->one()
152+
->two(),
153+
];

src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.inc.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@ $someObject
140140
->someOtherFunc(nameA: 23, nameB: 42)
141141
->endSomething($value, name: $value)
142142
->endEverything();
143+
144+
// Issue https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1154
145+
$array = [
146+
[
147+
$item->one()->two(),
148+
],
149+
$item->one()
150+
->two(),
151+
$item->one()
152+
->two(),
153+
];

src/Standards/PEAR/Tests/WhiteSpace/ObjectOperatorIndentUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function getErrorList()
5656
140 => 1,
5757
141 => 1,
5858
142 => 1,
59+
152 => 1,
5960
];
6061

6162
}//end getErrorList()

0 commit comments

Comments
 (0)