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;
|
2019-11-08 13:45:50 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2019-11-08 15:48:05 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeContainer\ParsedNodesByType;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2018-12-31 17:39:36 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
|
|
|
/**
|
2019-04-13 09:20:27 +00:00
|
|
|
* @var ParsedNodesByType
|
2018-12-31 17:39:36 +00:00
|
|
|
*/
|
2019-04-13 09:20:27 +00:00
|
|
|
private $parsedNodesByType;
|
2018-12-31 17:39:36 +00:00
|
|
|
|
2019-04-13 09:20:27 +00:00
|
|
|
public function __construct(ParsedNodesByType $parsedNodesByType)
|
2018-12-31 17:39:36 +00:00
|
|
|
{
|
2019-04-13 09:20:27 +00:00
|
|
|
$this->parsedNodesByType = $parsedNodesByType;
|
2018-12-31 17:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
2018-12-31 17:39:36 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public static function run()
|
|
|
|
{
|
|
|
|
$this->eat();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function eat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-12-31 17:39:36 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-12-31 17:39:36 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public static function run()
|
|
|
|
{
|
|
|
|
self::eat();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function eat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
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
|
|
|
|
{
|
2019-11-08 13:45:50 +00:00
|
|
|
if (! $node->var instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-31 17:39:36 +00:00
|
|
|
if (! $this->isName($node->var, 'this')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:48:05 +00:00
|
|
|
if ($this->isObjectType($node->var, TestCase::class)) {
|
|
|
|
if ($this->isName($node->name, 'assert*')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-13 09:20:27 +00:00
|
|
|
$isStaticMethod = $this->parsedNodesByType->isStaticMethod($methodName, $className);
|
2018-12-31 17:39:36 +00:00
|
|
|
if (! $isStaticMethod) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-10 13:15:04 +00:00
|
|
|
return $this->createStaticCall('self', $methodName, $node->args);
|
2018-12-31 17:39:36 +00:00
|
|
|
}
|
|
|
|
}
|