Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Skip has named arg on AddMethodCallBasedStrictParamTypeRector #6137

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class SkipNamedArg
{
public function run(): void
{
$this->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];
}
}
18 changes: 6 additions & 12 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -31,25 +31,19 @@ public function __construct(
}

/**
* @param MethodCall[]|StaticCall[]|ArrayCallable[] $calls
* @param MethodCall[]|StaticCall[] $calls
* @return array<int, Type>
*/
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);
Expand Down