diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/FixtureUnion/skip_named_arg.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/FixtureUnion/skip_named_arg.php.inc new file mode 100644 index 00000000000..059c754f628 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/FixtureUnion/skip_named_arg.php.inc @@ -0,0 +1,23 @@ +method(var1: new \DateTime(), var3: ['1']); + $this->method(var1: new \DateTime(), var2: true); + } + + private function method( + ?\DateTime $var1, + ?bool $var2 = false, + array $var3 = [], + ): void + { + return [$var1, $var2, $var2]; + } +} diff --git a/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php b/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php index 15fd2658941..af4ceb2801a 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php +++ b/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php @@ -7,6 +7,7 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Identifier; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\MixedType; use PHPStan\Type\NullType; @@ -15,7 +16,6 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeWithClassName; use PHPStan\Type\UnionType; -use Rector\NodeCollector\ValueObject\ArrayCallable; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; use Rector\NodeTypeResolver\TypeComparator\TypeComparator; @@ -31,7 +31,7 @@ public function __construct( } /** - * @param MethodCall[]|StaticCall[]|ArrayCallable[] $calls + * @param MethodCall[]|StaticCall[] $calls * @return array */ public function resolveStrictTypesFromCalls(array $calls): array @@ -39,17 +39,11 @@ public function resolveStrictTypesFromCalls(array $calls): array $staticTypesByArgumentPosition = []; foreach ($calls as $call) { - if (! $call instanceof StaticCall && ! $call instanceof MethodCall) { - continue; - } - foreach ($call->args as $position => $arg) { - if (! $arg instanceof Arg) { - continue; - } - - if ($arg->unpack) { - continue; + // there is first class callable usage, or argument unpack, or named arg + // simply returns array marks as unknown as can be anything and in any position + if (! $arg instanceof Arg || $arg->unpack || $arg->name instanceof Identifier) { + return []; } $staticTypesByArgumentPosition[$position][] = $this->resolveStrictArgValueType($arg);