From e00262df46a73f3478bb0dc0e0d62663ef3f442e Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 11 Jun 2020 13:01:41 +0200 Subject: [PATCH] PhpFunctionFromParserNodeReflection - fix optionality of parameters with default value followed by variadic parmaeters --- .../PhpFunctionFromParserNodeReflection.php | 4 +-- .../Methods/OverridingMethodRuleTest.php | 6 ++++ tests/PHPStan/Rules/Methods/data/bug-3443.php | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-3443.php diff --git a/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php b/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php index 3c080b7642..a9194e6fe4 100644 --- a/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php +++ b/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php @@ -142,7 +142,7 @@ private function getParameters(): array /** @var \PhpParser\Node\Param $parameter */ foreach (array_reverse($this->functionLike->getParams()) as $parameter) { - if (!$isOptional || $parameter->default === null) { + if ($parameter->default === null && !$parameter->variadic) { $isOptional = false; } @@ -151,7 +151,7 @@ private function getParameters(): array } $parameters[] = new PhpParameterFromParserNodeReflection( $parameter->var->name, - $isOptional || $parameter->variadic, + $isOptional, $this->realParameterTypes[$parameter->var->name], $this->phpDocParameterTypes[$parameter->var->name] ?? null, $parameter->byRef diff --git a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php index d5cc0ddf0e..cdde0eaa67 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -330,4 +330,10 @@ public function testBug3403(int $phpVersion): void $this->analyse([__DIR__ . '/data/bug-3403.php'], []); } + public function testBug3443(): void + { + $this->phpVersionId = PHP_VERSION_ID; + $this->analyse([__DIR__ . '/data/bug-3443.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-3443.php b/tests/PHPStan/Rules/Methods/data/bug-3443.php new file mode 100644 index 0000000000..e99b892444 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-3443.php @@ -0,0 +1,33 @@ +