Repository navigation
Do not reorder arguments in ArgumentsNormalizer::reorderArgs() when named arguments follow an unpacked argument - #7110
Open
phpstan-bot wants to merge 1 commit into
Conversation
… named arguments follow an unpacked argument
- reorderArgs() placed an unpacked argument at its call position and then
dropped any named argument mapping to the same position, or filled the
parameters between the unpack and a later named argument with default
values even though the unpacked array might supply them. The number of
parameters an unpacked argument fills is unknown there, so return null
("cannot reorder") when a call mixes unpacking with named arguments.
Callers then fall back to the original arguments, which
ParametersAcceptorSelector::selectFromArgs() already maps correctly
(constant arrays expanded, string keys and named args by name).
- Mirrored the change in turbo-ext/src/ArgumentsNormalizer.cpp.
- Covers function, method and static method calls (conditional return
types), named args at a later position after the unpack, and dynamic
return type extensions (array_slice() previously returned array{}).
`new` was probed and already infers the right generic type.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change preserving argument order when unpacked and named arguments are combined.
No open issues 🎉
What changed in this PR
Fixes incorrect return-type inference for calls that combine unpacked arguments with named arguments by leaving their argument order unchanged.
Changes:
- Makes
reorderArgs()returnnullwhen both argument forms are present. - Applies the same behavior in the native C++ implementation.
- Adds regression coverage for function, method, static method, and
array_slice()calls.
| File | Description |
|---|---|
turbo-ext/src/ArgumentsNormalizer.cpp |
Mirrors the argument-reordering guard in the native implementation. |
tests/PHPStan/Analyser/nsrt/bug-15778.php |
Tests inferred return types for unpacked arguments followed by named arguments. |
src/Analyser/ArgumentsNormalizer.php |
Prevents unsafe reordering when named and unpacked arguments coexist. |
Review effort: Balanced
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
PHP 8.1+ allows named arguments after an unpacked argument (
label(...['locale' => 'en'], key: 'welcome')). PHPStan reordered these calls incorrectly, so conditional return types and dynamic return type extensions resolved against the wrong arguments. For example,label(...[], key: 'welcome')was inferred asnullinstead ofstring.Changes
src/Analyser/ArgumentsNormalizer.php:reorderArgs()returnsnull(cannot reorder) when a call has both named arguments and an unpacked argument.turbo-ext/src/ArgumentsNormalizer.cpp: the same change in the native mirror.Root cause
reorderArgs()placed the unpacked argument at its own index and then:label(...[], key: 'welcome')droppedkey), andsecond(...['x', 'y'], key: 'welcome')replaced$bwith its defaultnull).How many parameters an unpacked argument fills isn't known syntactically, so the argument list can't be safely reordered. When
reorderArgs()returnsnull, every caller falls back to the original call.ParametersAcceptorSelector::selectFromArgs()already handles the original arguments correctly: it expands constant arrays and matches string keys and named arguments by name.Paths affected, all through the shared
reorderArgs():newreorderFuncArguments()(they now skip these calls instead of checking the wrong arguments)newwas probed and already infers the right generic type.Test
tests/PHPStan/Analyser/nsrt/bug-15778.phpcovers:array_slice(...[[1, 2, 3]], offset: 1), which was wrongly inferred asarray{}Without the fix, the test fails on 9 assertions. The full test suite passes with and without the turbo extension loaded.
Fixes phpstan/phpstan#15778
🤖 Generated with Claude Code