Repository navigation
Do not reorder arguments in ArgumentsNormalizer::reorderArgs() when named arguments follow an unpacked argument - #7110
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
staabm
left a comment
There was a problem hiding this comment.
this fix seems to regress all benchmarks.
can we make it faster?
|
I don't understand how this code impact the benchmark... |
… named arguments Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I made What caused the slowdown: the PR changed the first loop in Fix (commit a106d4a):
Verification:
|
|
Oh indeed, I understand the mistake... |
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