Skip to content

Commit

Permalink
[TypeDeclaration] Remove parent lookup on AddMethodCallBasedStrictPar…
Browse files Browse the repository at this point in the history
…amTypeRector (#4173)
  • Loading branch information
samsonasik committed Jun 11, 2023
1 parent 64156d1 commit 72ac71b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\PhpParser\NodeFinder\LocalMethodCallFinder;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -72,25 +73,38 @@ private function resolve(int $value)
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [Class_::class];
}

/**
* @param ClassMethod $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->params === []) {
return null;
}
$hasChanged = false;

foreach ($node->getMethods() as $method) {
if ($method->params === []) {
continue;
}

if (! $method->isPrivate()) {
continue;
}

if (! $node->isPrivate()) {
return null;
$methodCalls = $this->localMethodCallFinder->match($node, $method);
$classMethodParameterTypes = $this->callTypesResolver->resolveStrictTypesFromCalls($methodCalls);

$classMethod = $this->classMethodParamTypeCompleter->complete($method, $classMethodParameterTypes, self::MAX_UNION_TYPES);
if ($classMethod instanceof ClassMethod) {
$hasChanged = true;
}
}

$methodCalls = $this->localMethodCallFinder->match($node);
$classMethodParameterTypes = $this->callTypesResolver->resolveStrictTypesFromCalls($methodCalls);
if ($hasChanged) {
return $node;
}

return $this->classMethodParamTypeCompleter->complete($node, $classMethodParameterTypes, self::MAX_UNION_TYPES);
return null;
}
}
9 changes: 2 additions & 7 deletions src/PhpParser/NodeFinder/LocalMethodCallFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ public function __construct(
/**
* @return MethodCall[]
*/
public function match(ClassMethod $classMethod): array
public function match(Class_ $class, ClassMethod $classMethod): array
{
$class = $this->betterNodeFinder->findParentType($classMethod, Class_::class);
if (! $class instanceof Class_) {
return [];
}

$className = $this->nodeNameResolver->getName($class);
if (! is_string($className)) {
return [];
}

/** @var MethodCall[] $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstanceOf($class, MethodCall::class);
$methodCalls = $this->betterNodeFinder->findInstanceOf($class->getMethods(), MethodCall::class);

$classMethodName = $this->nodeNameResolver->getName($classMethod);

Expand Down

0 comments on commit 72ac71b

Please sign in to comment.