2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-12-31 17:39:36 +00:00
|
|
|
|
2019-09-22 18:57:03 +00:00
|
|
|
namespace Rector\Php70\Rector\MethodCall;
|
2018-12-31 17:39:36 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2021-01-24 20:12:02 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-01-24 20:12:02 +00:00
|
|
|
use Rector\NodeCollector\Reflection\MethodReflectionProvider;
|
2020-03-28 23:06:05 +00:00
|
|
|
use Rector\NodeCollector\StaticAnalyzer;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-01-24 20:12:02 +00:00
|
|
|
use ReflectionMethod;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-31 17:39:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://3v4l.org/rkiSC
|
2019-09-23 11:43:13 +00:00
|
|
|
* @see \Rector\Php70\Tests\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\ThisCallOnStaticMethodToStaticCallRectorTest
|
2018-12-31 17:39:36 +00:00
|
|
|
*/
|
|
|
|
final class ThisCallOnStaticMethodToStaticCallRector extends AbstractRector
|
|
|
|
{
|
2020-03-28 23:06:05 +00:00
|
|
|
/**
|
|
|
|
* @var StaticAnalyzer
|
|
|
|
*/
|
|
|
|
private $staticAnalyzer;
|
|
|
|
|
2021-01-24 20:12:02 +00:00
|
|
|
/**
|
|
|
|
* @var MethodReflectionProvider
|
|
|
|
*/
|
|
|
|
private $methodReflectionProvider;
|
|
|
|
|
|
|
|
public function __construct(StaticAnalyzer $staticAnalyzer, MethodReflectionProvider $methodReflectionProvider)
|
2020-03-28 23:06:05 +00:00
|
|
|
{
|
|
|
|
$this->staticAnalyzer = $staticAnalyzer;
|
2021-01-24 20:12:02 +00:00
|
|
|
$this->methodReflectionProvider = $methodReflectionProvider;
|
2020-03-28 23:06:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-12-31 17:39:36 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Changes $this->call() to static method to static call',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2018-12-31 17:39:36 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public static function run()
|
|
|
|
{
|
|
|
|
$this->eat();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function eat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2018-12-31 17:39:36 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public static function run()
|
|
|
|
{
|
2020-06-19 13:09:46 +00:00
|
|
|
static::eat();
|
2018-12-31 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function eat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2018-12-31 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [MethodCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MethodCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-05-02 18:52:16 +00:00
|
|
|
if (! $this->isVariableName($node->var, 'this')) {
|
2018-12-31 17:39:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-24 20:12:02 +00:00
|
|
|
$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
|
2020-04-08 15:33:08 +00:00
|
|
|
if ($this->isObjectType($node->var, 'PHPUnit\Framework\TestCase')) {
|
2019-11-23 20:12:57 +00:00
|
|
|
return null;
|
2019-11-08 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 22:36:51 +00:00
|
|
|
/** @var class-string $className */
|
2019-04-13 09:20:27 +00:00
|
|
|
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
|
2019-01-25 14:32:42 +00:00
|
|
|
if (! is_string($className)) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-31 17:39:36 +00:00
|
|
|
|
2020-03-28 23:06:05 +00:00
|
|
|
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($methodName, $className);
|
2018-12-31 17:39:36 +00:00
|
|
|
if (! $isStaticMethod) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-24 20:12:02 +00:00
|
|
|
$classReference = $this->resolveClassSelf($node);
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createStaticCall($classReference, $methodName, $node->args);
|
2021-01-24 20:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
2018-12-31 17:39:36 +00:00
|
|
|
}
|
|
|
|
}
|