PEAR/ObjectOperatorIndent: improve end of statement detection when getting the next object operator#1158
Merged
jrfnl merged 1 commit intoPHPCSStandards:masterfrom Jul 2, 2025
Conversation
…tting 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.
jrfnl
approved these changes
Jul 2, 2025
Member
jrfnl
left a comment
There was a problem hiding this comment.
@rodrigoprimo Good analysis and the fix makes sense to me. Thank you for taking this one on!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug where
PEAR.WhiteSpace.ObjectOperatorIndentwould generate a false positive when checking multiple chained object operators in a multidimensional array.The problem is that the code was using the
$localparameter offindNext()to limit the scope of the search when reassigning$nextto the next object operator, and$localonly 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 and 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
$localparameter to find the end of the statement, the end of the statement as defined byFile::findEndOfStatement()is now used.See #1154 for more details on the problem.
Suggested changelog entry
Fixed: false positive when
PEAR.WhiteSpaceObjectOperatorIndentchecks multiple chained method calls in a multidimensional arrayRelated issues/external references
Fixes #1154
Fixes squizlabs/PHP_CodeSniffer#3718
Types of changes
PR checklist