Require array_search()'s haystack argument before reading it - #6239
Merged
staabm merged 1 commit intoAug 18, 2026
Conversation
The list-preserving `$list[array_search($needle, $list)]` heuristic accepted a
call with a single argument and then read the second one, so
`$list[array_search($list)] = 4;` aborted the analysis with
AssignHandler::isSameVariable(): Argument #2 ($b) must be of type
PhpParser\Node\Expr, null given
The call is malformed either way; with the argument count checked it is reported
as such instead of ending the run. A single unpacked argument counts as one
argument too and hit the same read.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Split out of #6226 as requested in #6226 (comment).
AssignHandler::shouldKeepList()recognizes$list[array_search($needle, $list)]as an offset that keeps an array a list. The branch accepted a call with one argument and then read the second one, so this aborts the analysis on current 2.2.x:The haystack is
array_search()'s second argument, so requiring two is what the branch already assumed. The call stays malformed - it is now reported asFunction array_search invoked with 1 parameter, 2-3 required.instead of ending the run.A single unpacked argument counts as one argument too, so
$list[array_search(...$args)] = 4;reached the same read and is covered as well.I checked the neighbouring heuristics for the same off-by-one: every other
getArgs()[N]in the file is guarded by a count of at leastN + 1, and a sweep oversrc/for an argument index accessed under a weaker count guard turned up no other case that can be reached (thecount()ones bail out on an empty argument list before getting there).tests/PHPStan/Analyser/nsrt/array-search-offset-argument-count.phpholds both cases; with the guard reverted the test errors with the TypeError above. Full suite, self-analysis and phpcs are green.