From c48a8bfe74c6a781ac492dad6c2f64de3bc162fa Mon Sep 17 00:00:00 2001 From: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:00:08 +0000 Subject: [PATCH] Do not reorder arguments in `ArgumentsNormalizer::reorderArgs()` when 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. --- src/Analyser/ArgumentsNormalizer.php | 12 ++++- tests/PHPStan/Analyser/nsrt/bug-15778.php | 58 +++++++++++++++++++++++ turbo-ext/src/ArgumentsNormalizer.cpp | 14 +++++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-15778.php diff --git a/src/Analyser/ArgumentsNormalizer.php b/src/Analyser/ArgumentsNormalizer.php index e720fa68311..ebbfb20f150 100644 --- a/src/Analyser/ArgumentsNormalizer.php +++ b/src/Analyser/ArgumentsNormalizer.php @@ -325,15 +325,25 @@ public static function reorderArgs(ParametersAcceptor $parametersAcceptor, array } $hasNamedArgs = false; + $hasUnpackedArgs = false; foreach ($callArgs as $arg) { if ($arg->name !== null) { $hasNamedArgs = true; - break; } + if (!$arg->unpack) { + continue; + } + + $hasUnpackedArgs = true; } if (!$hasNamedArgs) { return array_values($callArgs); } + if ($hasUnpackedArgs) { + // the number of parameters an unpacked argument fills is unknown here, + // so the positions of the named arguments that follow it cannot be determined + return null; + } $hasVariadic = false; $argumentPositions = []; diff --git a/tests/PHPStan/Analyser/nsrt/bug-15778.php b/tests/PHPStan/Analyser/nsrt/bug-15778.php new file mode 100644 index 00000000000..0da9b34ab0f --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15778.php @@ -0,0 +1,58 @@ += 8.1 + +declare(strict_types=1); + +namespace Bug15778; + +use function PHPStan\Testing\assertType; + +/** @return ($key is null ? null : string) */ +function label(?string $key = null, ?string $locale = null): ?string +{ + return $key; +} + +/** @return ($b is null ? null : string) */ +function second(?string $a = null, ?string $b = null, ?string $key = null): ?string +{ + return $b; +} + +class Foo +{ + + /** @return ($key is null ? null : string) */ + public function label(?string $key = null, ?string $locale = null): ?string + { + return $key; + } + + /** @return ($key is null ? null : string) */ + public static function staticLabel(?string $key = null, ?string $locale = null): ?string + { + return $key; + } + + /** @return ($b is null ? null : string) */ + public function second(?string $a = null, ?string $b = null, ?string $key = null): ?string + { + return $b; + } + +} + +function doFoo(Foo $foo): void +{ + assertType('string', label(locale: 'en', key: 'welcome')); + assertType('string', label(...['locale' => 'en'], key: 'welcome')); + assertType('string', label(...[], key: 'welcome')); + assertType('string', second(...['x', 'y'], key: 'welcome')); + assertType('null', second(...['x'], key: 'welcome')); + assertType('array', array_slice(...[[1, 2, 3]], offset: 1)); + + assertType('string', $foo->label(...['locale' => 'en'], key: 'welcome')); + assertType('string', $foo->label(...[], key: 'welcome')); + assertType('string', $foo->second(...['x', 'y'], key: 'welcome')); + assertType('string', Foo::staticLabel(...['locale' => 'en'], key: 'welcome')); + assertType('string', Foo::staticLabel(...[], key: 'welcome')); +} diff --git a/turbo-ext/src/ArgumentsNormalizer.cpp b/turbo-ext/src/ArgumentsNormalizer.cpp index f13ec4931b2..b33b0bc5b0c 100644 --- a/turbo-ext/src/ArgumentsNormalizer.cpp +++ b/turbo-ext/src/ArgumentsNormalizer.cpp @@ -288,16 +288,26 @@ class ArgumentsNormalizer if (zend_hash_num_elements(args) == 0) return zv::Val(zv::Arr::empty()); bool hasNamedArgs = false; + bool hasUnpackedArgs = false; for (zv::ArrayEntry entry : zv::TableRef(args)) { + zval *arg = entry.value().deref().raw(); zv::Val nameHold; - zval *name = readProperty(pt_an_arg_name_site, entry.value().deref().raw(), PT_LC("name"), nameHold); + zval *name = readProperty(pt_an_arg_name_site, arg, PT_LC("name"), nameHold); if (UNEXPECTED(name == NULL)) return zv::Val(); if (Z_TYPE_P(name) != IS_NULL) { hasNamedArgs = true; - break; + } + zv::Val unpackHold; + zval *unpack = readProperty(pt_an_arg_unpack_site, arg, PT_LC("unpack"), unpackHold); + if (UNEXPECTED(unpack == NULL)) return zv::Val(); + if (zend_is_true(unpack)) { + hasUnpackedArgs = true; } } if (EXPECTED(!hasNamedArgs)) return arrayValues(callArgs); + // the number of parameters an unpacked argument fills is unknown here, + // so the positions of the named arguments that follow it cannot be determined + if (hasUnpackedArgs) return zv::Val::null(); return reorderNamedArgs(parametersAcceptor, args); }