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); }