Skip to content

Commit

Permalink
[TypeDeclaration] Skip has named arg on AddMethodCallBasedStrictParam…
Browse files Browse the repository at this point in the history
…TypeRector (#6137)

* [TypeDeclaration] Skip has named arg on AddMethodCallBasedStrictParamTypeRector

* clean up

* clean up

* future comment

* clean up

* clean up doc
  • Loading branch information
samsonasik committed Jul 11, 2024
1 parent 5302c40 commit 18846cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
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

0 comments on commit 18846cb

Please sign in to comment.