Fix #5290: "Cannot use [] for reading." false positives#5059
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #5290: "Cannot use [] for reading." false positives#5059phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Enter expression assign context for ArrayDimFetch with null dim when passed as a by-reference argument in NodeScopeResolver::processArgs() - Exit expression assign after processing to prevent scope leakage - Add regression test in tests/PHPStan/Rules/Arrays/data/bug-5290.php - Uncomment previously disabled test case for by-ref closure in existing test data Closes phpstan/phpstan#5290
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.
Summary
PHPStan incorrectly reported "Cannot use [] for reading." when
$array[]was passed as an argument to a function/method parameter declared as by-reference. This is a valid PHP pattern —$array[]in a by-reference context creates a new array element and passes the reference to it, rather than reading.Changes
src/Analyser/NodeScopeResolver.phpinprocessArgs(): when processing a by-reference argument that is anArrayDimFetchwith null dim ($array[]), enter expression assign context before processing and exit it afterward. This tells theOffsetAccessWithoutDimForReadingRulethat the expression is in a write context.tests/PHPStan/Rules/Arrays/data/bug-5290.phpcovering named functions, immediately invoked closures with by-ref params, and method calls.tests/PHPStan/Rules/Arrays/data/offset-access-without-dim-for-reading.php(line 18:(function (&$ref) {})($array[])which was marked as "Should work but doesn't").Root cause
The
OffsetAccessWithoutDimForReadingRulechecks whether anArrayDimFetchwith null dim is in an expression assign context via$scope->isInExpressionAssign(). However,NodeScopeResolver::processArgs()did not enter expression assign context when processing by-reference arguments, so$array[]passed to a&$paramwas treated as a read rather than a write.The fix is targeted to only affect
ArrayDimFetchnodes with null dim (the$array[]syntax), avoiding broader side effects on other rules likeDefinedVariableRuleorUnusedPrivatePropertyRulethat also checkisInExpressionAssign.Test
Added
tests/PHPStan/Rules/Arrays/data/bug-5290.phpwith three scenarios that all previously produced false positives:set($array[])— named function with&$valueparameter(function (&$ref) {})($array[])— immediately invoked closure with by-ref parameter$foo->bar($array[])— method call with&$valueparameterAll three expect no errors.
Fixes phpstan/phpstan#5290