diff --git a/src/Analyser/ArgumentsNormalizer.php b/src/Analyser/ArgumentsNormalizer.php index e720fa68311..d9a5e84f168 100644 --- a/src/Analyser/ArgumentsNormalizer.php +++ b/src/Analyser/ArgumentsNormalizer.php @@ -335,6 +335,14 @@ public static function reorderArgs(ParametersAcceptor $parametersAcceptor, array return array_values($callArgs); } + 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; $argumentPositions = []; $signatureParameters = $parametersAcceptor->getParameters(); 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/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 { diff --git a/turbo-ext/src/ArgumentsNormalizer.cpp b/turbo-ext/src/ArgumentsNormalizer.cpp index f13ec4931b2..7e52c1e5f76 100644 --- a/turbo-ext/src/ArgumentsNormalizer.cpp +++ b/turbo-ext/src/ArgumentsNormalizer.cpp @@ -299,6 +299,15 @@ class ArgumentsNormalizer } if (EXPECTED(!hasNamedArgs)) return arrayValues(callArgs); + for (zv::ArrayEntry entry : zv::TableRef(args)) { + zv::Val unpackHold; + zval *unpack = readProperty(pt_an_arg_unpack_site, entry.value().deref().raw(), PT_LC("unpack"), unpackHold); + if (UNEXPECTED(unpack == NULL)) return zv::Val(); + // 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(); + } + return reorderNamedArgs(parametersAcceptor, args); }