rector/rules/code-quality/src/Rector/Identical/GetClassToInstanceOfRector.php

131 lines
3.6 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Identical;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Core\NodeManipulator\BinaryOpManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Php71\ValueObject\TwoNodeMatch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\CodeQuality\Tests\Rector\Identical\GetClassToInstanceOfRector\GetClassToInstanceOfRectorTest
*/
final class GetClassToInstanceOfRector extends AbstractRector
{
/**
* @var string[]
*/
private const NO_NAMESPACED_CLASSNAMES = ['self', 'static'];
2018-11-07 17:04:38 +00:00
/**
* @var BinaryOpManipulator
2018-11-07 17:04:38 +00:00
*/
private $binaryOpManipulator;
2018-11-07 17:04:38 +00:00
public function __construct(BinaryOpManipulator $binaryOpManipulator)
2018-11-07 17:04:38 +00:00
{
$this->binaryOpManipulator = $binaryOpManipulator;
2018-11-07 17:04:38 +00:00
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes comparison with get_class to instanceof',
[
new CodeSample(
'if (EventsListener::class === get_class($event->job)) { }',
'if ($event->job instanceof EventsListener) { }'
),
]
);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Identical::class, NotIdentical::class];
}
/**
* @param Identical|NotIdentical $node
*/
public function refactor(Node $node): ?Node
{
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
2018-11-07 17:04:38 +00:00
$node,
function (Node $node): bool {
2018-11-07 17:04:38 +00:00
return $this->isClassReference($node);
},
function (Node $node): bool {
2018-11-07 17:04:38 +00:00
return $this->isGetClassFuncCallNode($node);
}
);
if (! $twoNodeMatch instanceof TwoNodeMatch) {
return null;
}
/** @var ClassConstFetch|String_ $firstExpr */
$firstExpr = $twoNodeMatch->getFirstExpr();
/** @var FuncCall $funcCall */
$funcCall = $twoNodeMatch->getSecondExpr();
$varNode = $funcCall->args[0]->value;
if ($firstExpr instanceof String_) {
2021-01-30 23:20:05 +00:00
$className = $this->valueResolver->getValue($firstExpr);
} else {
$className = $this->getName($firstExpr->class);
}
2018-11-07 17:04:38 +00:00
2019-01-14 18:21:23 +00:00
if ($className === null) {
return null;
}
2018-11-07 17:04:38 +00:00
$class = in_array($className, self::NO_NAMESPACED_CLASSNAMES, true)
? new Name($className)
: new FullyQualified($className);
$instanceof = new Instanceof_($varNode, $class);
if ($node instanceof NotIdentical) {
2020-06-29 21:19:37 +00:00
return new BooleanNot($instanceof);
}
2020-06-29 21:19:37 +00:00
return $instanceof;
}
private function isClassReference(Node $node): bool
{
if (! $node instanceof ClassConstFetch) {
// might be
return $node instanceof String_;
}
return $this->isName($node->name, 'class');
}
private function isGetClassFuncCallNode(Node $node): bool
{
if (! $node instanceof FuncCall) {
return false;
}
2018-10-15 04:13:42 +00:00
return $this->isName($node, 'get_class');
}
}