rector/packages/Php70/src/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php

113 lines
2.5 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;
2019-11-08 13:45:50 +00:00
use PhpParser\Node\Expr\Variable;
use PHPUnit\Framework\TestCase;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://3v4l.org/rkiSC
* @see \Rector\Php70\Tests\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector\ThisCallOnStaticMethodToStaticCallRectorTest
*/
final class ThisCallOnStaticMethodToStaticCallRector extends AbstractRector
{
/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;
public function __construct(ParsedNodesByType $parsedNodesByType)
{
$this->parsedNodesByType = $parsedNodesByType;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Changes $this->call() to static method to static call', [
new CodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
class SomeClass
{
public static function run()
{
$this->eat();
}
public static function eat()
{
}
}
2019-09-18 06:14:35 +00:00
PHP
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
class SomeClass
{
public static function run()
{
self::eat();
}
public static function eat()
{
}
}
2019-09-18 06:14:35 +00:00
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
2019-11-08 13:45:50 +00:00
if (! $node->var instanceof Variable) {
return null;
}
if (! $this->isName($node->var, 'this')) {
return null;
}
if ($this->isObjectType($node->var, TestCase::class)) {
if ($this->isName($node->name, 'assert*')) {
return null;
}
}
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
if (! is_string($className)) {
return null;
}
2019-11-08 13:45:50 +00:00
$methodName = $this->getName($node->name);
2019-01-14 18:21:23 +00:00
if ($methodName === null) {
return null;
}
$isStaticMethod = $this->parsedNodesByType->isStaticMethod($methodName, $className);
if (! $isStaticMethod) {
return null;
}
return $this->createStaticCall('self', $methodName, $node->args);
}
}