rector/rules/CodeQuality/Rector/Identical/GetClassToInstanceOfRector.php

110 lines
4.1 KiB
PHP
Raw Normal View History

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