rector/rules/php70/src/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php

139 lines
3.4 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\Stmt\Class_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeCollector\Reflection\MethodReflectionProvider;
2020-03-28 23:06:05 +00:00
use Rector\NodeCollector\StaticAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/rkiSC
* @see \Rector\Php70\Tests\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\ThisCallOnStaticMethodToStaticCallRectorTest
*/
final class ThisCallOnStaticMethodToStaticCallRector extends AbstractRector
{
2020-03-28 23:06:05 +00:00
/**
* @var StaticAnalyzer
*/
private $staticAnalyzer;
/**
* @var MethodReflectionProvider
*/
private $methodReflectionProvider;
public function __construct(StaticAnalyzer $staticAnalyzer, MethodReflectionProvider $methodReflectionProvider)
2020-03-28 23:06:05 +00:00
{
$this->staticAnalyzer = $staticAnalyzer;
$this->methodReflectionProvider = $methodReflectionProvider;
2020-03-28 23:06:05 +00:00
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes $this->call() to static method to static call',
[
new 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 string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isVariableName($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, 'PHPUnit\Framework\TestCase')) {
return null;
}
2020-04-02 22:36:51 +00:00
/** @var class-string $className */
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
if (! is_string($className)) {
return null;
}
2020-03-28 23:06:05 +00:00
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($methodName, $className);
if (! $isStaticMethod) {
return null;
}
$classReference = $this->resolveClassSelf($node);
2021-01-30 21:41:25 +00:00
return $this->nodeFactory->createStaticCall($classReference, $methodName, $node->args);
}
private function resolveClassSelf(MethodCall $methodCall): string
{
$classLike = $methodCall->getAttribute(AttributeKey::CLASS_NODE);
if (! $classLike instanceof Class_) {
return 'static';
}
if ($classLike->isFinal()) {
return 'self';
}
$methodReflection = $this->methodReflectionProvider->provideByMethodCall($methodCall);
if ($methodReflection instanceof ReflectionMethod && $methodReflection->isPrivate()) {
return 'self';
}
return 'static';
}
}