rector/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php

133 lines
4.6 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Php70\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Type\ObjectType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
2020-03-28 23:06:05 +00:00
use Rector\NodeCollector\StaticAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://3v4l.org/rkiSC
* @see \Rector\Tests\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\ThisCallOnStaticMethodToStaticCallRectorTest
*/
final class ThisCallOnStaticMethodToStaticCallRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
{
2020-03-28 23:06:05 +00:00
/**
* @readonly
* @var \Rector\NodeCollector\StaticAnalyzer
2020-03-28 23:06:05 +00:00
*/
private $staticAnalyzer;
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(\Rector\NodeCollector\StaticAnalyzer $staticAnalyzer, \Rector\Core\Reflection\ReflectionResolver $reflectionResolver)
2020-03-28 23:06:05 +00:00
{
$this->staticAnalyzer = $staticAnalyzer;
$this->reflectionResolver = $reflectionResolver;
2020-03-28 23:06:05 +00:00
}
public function provideMinPhpVersion() : int
{
return \Rector\Core\ValueObject\PhpVersionFeature::STATIC_CALL_ON_NON_STATIC;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Changes $this->call() to static method to static call', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public static function run()
{
$this->eat();
}
public static function eat()
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public static function run()
{
static::eat();
}
public static function eat()
{
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if (!$node->var instanceof \PhpParser\Node\Expr\Variable) {
return null;
}
if (!$this->nodeNameResolver->isName($node->var, 'this')) {
return null;
}
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}
2020-06-21 10:08:41 +00:00
// skip PHPUnit calls, as they accept both self:: and $this-> formats
if ($this->isObjectType($node->var, new \PHPStan\Type\ObjectType('PHPUnit\\Framework\\TestCase'))) {
return null;
}
$classLike = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\ClassLike::class);
if (!$classLike instanceof \PhpParser\Node\Stmt\ClassLike) {
return null;
}
$className = (string) $this->nodeNameResolver->getName($classLike);
2020-03-28 23:06:05 +00:00
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($methodName, $className);
if (!$isStaticMethod) {
return null;
}
$objectReference = $this->resolveClassSelf($node);
return $this->nodeFactory->createStaticCall($objectReference, $methodName, $node->args);
}
private function resolveClassSelf(\PhpParser\Node\Expr\MethodCall $methodCall) : \Rector\Core\Enum\ObjectReference
{
$classLike = $this->betterNodeFinder->findParentType($methodCall, \PhpParser\Node\Stmt\Class_::class);
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
return \Rector\Core\Enum\ObjectReference::STATIC();
}
if ($classLike->isFinal()) {
return \Rector\Core\Enum\ObjectReference::SELF();
}
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($methodCall);
if (!$methodReflection instanceof \PHPStan\Reflection\Php\PhpMethodReflection) {
return \Rector\Core\Enum\ObjectReference::STATIC();
}
if (!$methodReflection->isPrivate()) {
return \Rector\Core\Enum\ObjectReference::STATIC();
}
return \Rector\Core\Enum\ObjectReference::SELF();
}
}