Skip to content

Commit

Permalink
Named arguments - fix optional arguments and variadics
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 4, 2021
1 parent f2ecdac commit 3e5621e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ResolvedFunctionVariant;
use PHPStan\Type\ErrorType;
Expand Down Expand Up @@ -409,8 +410,11 @@ private function processArguments(
$namedArgumentAlreadyOccurred = true;

$parametersCount = count($parameters);
$requiredParametersByName = array_filter($unusedParametersByName, static function (ParameterReflection $parameter): bool {
return !$parameter->isOptional();
});
if (
count($unusedParametersByName) !== 0
count($requiredParametersByName) !== 0
|| !$parametersAcceptor->isVariadic()
|| $parametersCount <= 0
|| $isBuiltin
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1903,4 +1903,17 @@ public function testBug3546(): void
$this->analyse([__DIR__ . '/data/bug-3546.php'], []);
}

public function testBug4800(): void
{
if (!self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires static reflection.');
}

$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->phpVersion = 80000;
$this->analyse([__DIR__ . '/data/bug-4800.php'], []);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4800.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.0

namespace Bug4800;

class HelloWorld
{
/**
* @param string|int ...$arguments
*/
public function a(string $bar = '', ...$arguments): string
{
return '';
}

public function b(): void
{
$this->a(bar: 'baz', foo: 'bar', c: 3);
$this->a(foo: 'bar', c: 3);
}
}

0 comments on commit 3e5621e

Please sign in to comment.