mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
9a4a5b2bc5
eb5df4dde7
Use vendor-patches main branch (#6453)
50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\CodingStyle\Guard;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
use PhpParser\Node\Expr\Closure;
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PHPStan\Reflection\MethodReflection;
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
use Rector\Reflection\ReflectionResolver;
|
|
final class StaticGuard
|
|
{
|
|
/**
|
|
* @readonly
|
|
*/
|
|
private BetterNodeFinder $betterNodeFinder;
|
|
/**
|
|
* @readonly
|
|
*/
|
|
private ReflectionResolver $reflectionResolver;
|
|
public function __construct(BetterNodeFinder $betterNodeFinder, ReflectionResolver $reflectionResolver)
|
|
{
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
$this->reflectionResolver = $reflectionResolver;
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $node
|
|
*/
|
|
public function isLegal($node) : bool
|
|
{
|
|
if ($node->static) {
|
|
return \false;
|
|
}
|
|
$nodes = $node instanceof Closure ? $node->stmts : [$node->expr];
|
|
return !(bool) $this->betterNodeFinder->findFirst($nodes, function (Node $subNode) : bool {
|
|
if (!$subNode instanceof StaticCall) {
|
|
return $subNode instanceof Variable && $subNode->name === 'this';
|
|
}
|
|
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromStaticCall($subNode);
|
|
if (!$methodReflection instanceof MethodReflection) {
|
|
return \false;
|
|
}
|
|
return !$methodReflection->isStatic();
|
|
});
|
|
}
|
|
}
|