Skip to content

Do not reorder arguments in ArgumentsNormalizer::reorderArgs() when named arguments follow an unpacked argument - #7110

Open
phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-jfjrccv
Open

phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-jfjrccv

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

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 as null instead of string.

Changes

  • src/Analyser/ArgumentsNormalizer.php: reorderArgs() returns null (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:

  • skipped a named argument whose parameter position was the same index (label(...[], key: 'welcome') dropped key), and
  • filled any gap between the unpack and a later named argument with default values, even though the unpacked array may supply those parameters (second(...['x', 'y'], key: 'welcome') replaced $b with its default null).

How many parameters an unpacked argument fills isn't known syntactically, so the argument list can't be safely reordered. When reorderArgs() returns null, 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():

  • function calls
  • method calls
  • static calls
  • new
  • the function-argument rules that use reorderFuncArguments() (they now skip these calls instead of checking the wrong arguments)

new was probed and already infers the right generic type.

Test

tests/PHPStan/Analyser/nsrt/bug-15778.php covers:

  • the three calls from the issue
  • a named argument at a later position than the unpack, both when the unpack does and doesn't supply the parameter in between
  • method and static method variants
  • array_slice(...[[1, 2, 3]], offset: 1), which was wrongly inferred as array{}

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

… 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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() return null when 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong conditional return type when a named argument follows an unpacked array

2 participants