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 1/3] 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); } From a106d4a8f41bf813e5c7dad535f7668783e96384 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Fri, 20 Nov 2026 10:04:08 +0000 Subject: [PATCH 2/3] Only look for unpacked arguments in `reorderArgs()` when the call has named arguments Co-Authored-By: Claude Opus 5.5 --- src/Analyser/ArgumentsNormalizer.php | 18 ++++++++---------- turbo-ext/src/ArgumentsNormalizer.cpp | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Analyser/ArgumentsNormalizer.php b/src/Analyser/ArgumentsNormalizer.php index ebbfb20f150..d9a5e84f168 100644 --- a/src/Analyser/ArgumentsNormalizer.php +++ b/src/Analyser/ArgumentsNormalizer.php @@ -325,24 +325,22 @@ 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; + + foreach ($callArgs as $arg) { + if ($arg->unpack) { + // 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; diff --git a/turbo-ext/src/ArgumentsNormalizer.cpp b/turbo-ext/src/ArgumentsNormalizer.cpp index b33b0bc5b0c..7e52c1e5f76 100644 --- a/turbo-ext/src/ArgumentsNormalizer.cpp +++ b/turbo-ext/src/ArgumentsNormalizer.cpp @@ -288,26 +288,25 @@ 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, arg, PT_LC("name"), nameHold); + zval *name = readProperty(pt_an_arg_name_site, entry.value().deref().raw(), PT_LC("name"), nameHold); if (UNEXPECTED(name == NULL)) return zv::Val(); if (Z_TYPE_P(name) != IS_NULL) { hasNamedArgs = true; + break; } + } + if (EXPECTED(!hasNamedArgs)) return arrayValues(callArgs); + + for (zv::ArrayEntry entry : zv::TableRef(args)) { zv::Val unpackHold; - zval *unpack = readProperty(pt_an_arg_unpack_site, arg, PT_LC("unpack"), unpackHold); + zval *unpack = readProperty(pt_an_arg_unpack_site, entry.value().deref().raw(), PT_LC("unpack"), unpackHold); if (UNEXPECTED(unpack == NULL)) return zv::Val(); - if (zend_is_true(unpack)) { - hasUnpackedArgs = true; - } + // 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 (zend_is_true(unpack)) return zv::Val::null(); } - 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); } From f1a8d9eb41f120eae84351a10e8881553a0721c7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 20 Nov 2026 11:26:40 +0100 Subject: [PATCH 3/3] accept tally regressions --- tests/bench/ArgumentsNormalizerBench.php | 2 +- tests/bench/FileCacheStorageBench.php | 2 +- tests/bench/NullsafeOperatorHelperBench.php | 4 ++-- tests/bench/OperatorTypeSpecifyingExtensionBench.php | 2 +- tests/bench/RuleBench.php | 2 +- tests/bench/TrinaryLogicBench.php | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/bench/ArgumentsNormalizerBench.php b/tests/bench/ArgumentsNormalizerBench.php index e823f4d9939..2c68ff7ae46 100644 --- a/tests/bench/ArgumentsNormalizerBench.php +++ b/tests/bench/ArgumentsNormalizerBench.php @@ -54,7 +54,7 @@ public function boot(bool $named): void */ #[DataProvider(method: 'calls')] #[ExpectedFor(dataSet: 'named', instructions: '24.75K', cycles: '99.67K', memory: '2.516KiB')] - #[ExpectedFor(dataSet: 'positional', instructions: '2.670K', cycles: '21.83K', memory: '0B')] + #[ExpectedFor(dataSet: 'positional', instructions: '2.729K', cycles: '22.86K', memory: '0B')] public function benchReorderFuncArguments(bool $named): ?FuncCall { if ($this->acceptor === null || $this->call === null) { diff --git a/tests/bench/FileCacheStorageBench.php b/tests/bench/FileCacheStorageBench.php index c7bb1089a5b..fb697296693 100644 --- a/tests/bench/FileCacheStorageBench.php +++ b/tests/bench/FileCacheStorageBench.php @@ -53,7 +53,7 @@ public function boot(int $entries): void * @throws DirectoryCreatorException */ #[DataProvider(method: 'entries')] - #[ExpectedFor(dataSet: 'entries-100', instructions: '234.2K', cycles: '550.6K', memory: '17.29KiB')] + #[ExpectedFor(dataSet: 'entries-100', instructions: '246.5K', cycles: '582.7K', memory: '17.29KiB')] #[ExpectedFor(dataSet: 'entries-10000', instructions: '18.84M', cycles: '29.57M', memory: '1.345MiB')] public function benchSave(int $entries): void { diff --git a/tests/bench/NullsafeOperatorHelperBench.php b/tests/bench/NullsafeOperatorHelperBench.php index 688cb47740d..e6cc90c0632 100644 --- a/tests/bench/NullsafeOperatorHelperBench.php +++ b/tests/bench/NullsafeOperatorHelperBench.php @@ -84,7 +84,7 @@ public function boot(int $depth, bool $nullsafe): void */ #[DataProvider(method: 'levelsWithoutNullsafe')] #[ExpectedFor(dataSet: 'levels-16', instructions: '74.18K', cycles: '124.6K', memory: '6.242KiB')] - #[ExpectedFor(dataSet: 'levels-64', instructions: '283.4K', cycles: '379.1K', memory: '23.87KiB')] + #[ExpectedFor(dataSet: 'levels-64', instructions: '289.2K', cycles: '373.1K', memory: '23.87KiB')] public function benchGetNullsafeShortcircuitedExprOfEveryLevel(int $depth, bool $nullsafe): int { $unchanged = 0; @@ -104,7 +104,7 @@ public function benchGetNullsafeShortcircuitedExprOfEveryLevel(int $depth, bool */ #[DataProvider(method: 'chains')] #[ExpectedFor(dataSet: 'innermost-nullsafe-64', instructions: '263.0K', cycles: '363.3K', memory: '9.609KiB')] - #[ExpectedFor(dataSet: 'without-nullsafe-64', instructions: '173.3K', cycles: '258.8K', memory: '23.87KiB')] + #[ExpectedFor(dataSet: 'without-nullsafe-64', instructions: '177.1K', cycles: '255.0K', memory: '23.87KiB')] public function benchGetNullsafeShortcircuitedExpr(int $depth, bool $nullsafe): Expr { if ($this->chain === null) { diff --git a/tests/bench/OperatorTypeSpecifyingExtensionBench.php b/tests/bench/OperatorTypeSpecifyingExtensionBench.php index 219d929808b..6c966818568 100644 --- a/tests/bench/OperatorTypeSpecifyingExtensionBench.php +++ b/tests/bench/OperatorTypeSpecifyingExtensionBench.php @@ -76,7 +76,7 @@ public function boot(string $extension, string $operands): void #[ExpectedFor(dataSet: 'bcmath-integers', instructions: '317.4K', cycles: '383.8K', memory: '0B')] #[ExpectedFor(dataSet: 'bcmath-objects', instructions: '665.4K', cycles: '781.7K', memory: '216B')] #[ExpectedFor(dataSet: 'gmp-integers', instructions: '302.1K', cycles: '367.6K', memory: '0B')] - #[ExpectedFor(dataSet: 'gmp-objects', instructions: '593.5K', cycles: '718.6K', memory: '216B')] + #[ExpectedFor(dataSet: 'gmp-objects', instructions: '606.5K', cycles: '721.9K', memory: '216B')] public function benchSpecify(string $extension, string $operands): int { if ($this->extension === null) { diff --git a/tests/bench/RuleBench.php b/tests/bench/RuleBench.php index 6f1e641a4ec..f71fd5913d3 100644 --- a/tests/bench/RuleBench.php +++ b/tests/bench/RuleBench.php @@ -138,7 +138,7 @@ public function boot(string $rule, string $code, ?int $phpVersion): void #[ExpectedFor(dataSet: 'function-conditional-return-type-without-conditional-types', instructions: '148.3K', cycles: '159.0K', memory: '376B')] #[ExpectedFor(dataSet: 'impossible-check-type-function-call', instructions: '6.250M', cycles: '8.182M', memory: '5.758KiB')] #[ExpectedFor(dataSet: 'instantiation-restricted-constructor', instructions: '3.628M', cycles: '5.050M', memory: '6.742KiB')] - #[ExpectedFor(dataSet: 'node-connecting-visitor-attributes-no-arguments', instructions: '38.75K', cycles: '40.87K', memory: '40B')] + #[ExpectedFor(dataSet: 'node-connecting-visitor-attributes-no-arguments', instructions: '39.53K', cycles: '41.61K', memory: '40B')] #[ExpectedFor(dataSet: 'old-php-parser4-class-outside-class', instructions: '20.96K', cycles: '22.23K', memory: '0B')] #[ExpectedFor(dataSet: 'parameter-castable-to-string-diff-assoc', instructions: '6.028M', cycles: '6.950M', memory: '3.172KiB')] #[ExpectedFor(dataSet: 'restricted-function-usage', instructions: '287.9K', cycles: '355.2K', memory: '848B')] diff --git a/tests/bench/TrinaryLogicBench.php b/tests/bench/TrinaryLogicBench.php index 0c561590c99..94d01c3b59c 100644 --- a/tests/bench/TrinaryLogicBench.php +++ b/tests/bench/TrinaryLogicBench.php @@ -61,7 +61,7 @@ public function boot(int $count): void } #[DataProvider(method: 'operands')] - #[ExpectedFor(dataSet: 'operands-1', instructions: '1.111M', cycles: '1.135M', memory: '0B')] + #[ExpectedFor(dataSet: 'operands-1', instructions: '1.134M', cycles: '1.154M', memory: '0B')] #[ExpectedFor(dataSet: 'operands-4', instructions: '2.427M', cycles: '2.450M', memory: '216B')] public function benchAnd(int $operands): TrinaryLogic { @@ -75,7 +75,7 @@ public function benchAnd(int $operands): TrinaryLogic } #[DataProvider(method: 'operands')] - #[ExpectedFor(dataSet: 'operands-1', instructions: '1.111M', cycles: '1.136M', memory: '0B')] + #[ExpectedFor(dataSet: 'operands-1', instructions: '1.134M', cycles: '1.155M', memory: '0B')] #[ExpectedFor(dataSet: 'operands-4', instructions: '2.427M', cycles: '2.451M', memory: '216B')] public function benchOr(int $operands): TrinaryLogic {