2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Identical;
|
2018-10-12 16:57:21 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
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_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
|
|
|
use Rector\NodeManipulator\BinaryOpManipulator;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Php71\ValueObject\TwoNodeMatch;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Identical\GetClassToInstanceOfRector\GetClassToInstanceOfRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class GetClassToInstanceOfRector extends AbstractRector
|
2018-10-12 16:57:21 +00:00
|
|
|
{
|
2018-11-07 17:04:38 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\BinaryOpManipulator
|
2018-11-07 17:04:38 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $binaryOpManipulator;
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private const NO_NAMESPACED_CLASSNAMES = ['self', 'static'];
|
2023-09-20 12:53:23 +00:00
|
|
|
public function __construct(BinaryOpManipulator $binaryOpManipulator, ValueResolver $valueResolver)
|
2018-11-07 17:04:38 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2018-11-07 17:04:38 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-12 16:57:21 +00:00
|
|
|
{
|
2022-06-07 08:22: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) { }')]);
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-12 16:57:21 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-12 16:57:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Identical::class, NotIdentical::class];
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Identical|NotIdentical $node
|
2018-10-12 16:57:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-12 16:57:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($node, function (Node $node) : bool {
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->isClassReference($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
}, function (Node $node) : bool {
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->isGetClassFuncCallNode($node);
|
|
|
|
});
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$twoNodeMatch instanceof TwoNodeMatch) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
2020-12-20 12:50:55 +00:00
|
|
|
/** @var ClassConstFetch|String_ $firstExpr */
|
|
|
|
$firstExpr = $twoNodeMatch->getFirstExpr();
|
2022-07-18 15:25:27 +00:00
|
|
|
/** @var FuncCall $secondExpr */
|
|
|
|
$secondExpr = $twoNodeMatch->getSecondExpr();
|
2023-06-02 12:32:12 +00:00
|
|
|
if ($secondExpr->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-08 07:37:37 +00:00
|
|
|
if (!isset($secondExpr->getArgs()[0])) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-08 07:37:37 +00:00
|
|
|
$firstArg = $secondExpr->getArgs()[0];
|
|
|
|
$varNode = $firstArg->value;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($firstExpr instanceof String_) {
|
2021-01-30 23:20:05 +00:00
|
|
|
$className = $this->valueResolver->getValue($firstExpr);
|
2020-12-20 12:50:55 +00:00
|
|
|
} else {
|
|
|
|
$className = $this->getName($firstExpr->class);
|
|
|
|
}
|
2019-01-14 18:21:23 +00:00
|
|
|
if ($className === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($className === ObjectReference::PARENT) {
|
2022-04-02 08:40:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22: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);
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
2020-06-29 21:19:37 +00:00
|
|
|
return $instanceof;
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isClassReference(Node $node) : bool
|
2018-10-12 16:57:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node instanceof ClassConstFetch) {
|
2021-02-21 09:32:45 +00:00
|
|
|
// might be
|
2022-06-07 08:22:29 +00:00
|
|
|
return $node instanceof String_;
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
2021-02-21 09:32:45 +00:00
|
|
|
return $this->isName($node->name, 'class');
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isGetClassFuncCallNode(Node $node) : bool
|
2018-10-12 16:57:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node instanceof FuncCall) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
2018-10-15 04:13:42 +00:00
|
|
|
return $this->isName($node, 'get_class');
|
2018-10-12 16:57:21 +00:00
|
|
|
}
|
|
|
|
}
|