2021-02-09 13:31:11 +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\DeadCode\Rector\If_;
|
2021-02-09 13:31:11 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
2023-12-04 14:56:20 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2024-03-21 11:05:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2023-06-10 18:53:37 +00:00
|
|
|
use PhpParser\Node\Expr\CallLike;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Instanceof_;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt;
|
2023-12-04 14:56:20 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
2023-06-05 08:01:06 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2024-04-14 00:31:32 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2023-06-10 18:53:37 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeManipulator\IfManipulator;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2024-04-12 09:47:55 +00:00
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-02-09 13:31:11 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveDeadInstanceOfRector\RemoveDeadInstanceOfRectorTest
|
2021-02-09 13:31:11 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadInstanceOfRector extends AbstractRector
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\IfManipulator
|
2021-02-09 13:31:11 +00:00
|
|
|
*/
|
|
|
|
private $ifManipulator;
|
2024-04-12 09:47:55 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
|
|
|
public function __construct(IfManipulator $ifManipulator, ReflectionResolver $reflectionResolver)
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
|
|
|
$this->ifManipulator = $ifManipulator;
|
2024-04-12 09:47:55 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove dead instanceof check on type hinted variable', [new CodeSample(<<<'CODE_SAMPLE'
|
2023-06-10 18:53:37 +00:00
|
|
|
function run(stdClass $stdClass)
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
2023-06-10 18:53:37 +00:00
|
|
|
if (! $stdClass instanceof stdClass) {
|
|
|
|
return false;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2023-06-10 18:53:37 +00:00
|
|
|
|
|
|
|
return true;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2023-06-10 18:53:37 +00:00
|
|
|
function run(stdClass $stdClass)
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
2023-06-10 18:53:37 +00:00
|
|
|
return true;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-02-09 13:31:11 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
2023-06-12 09:17:29 +00:00
|
|
|
return [If_::class];
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-12 09:17:29 +00:00
|
|
|
* @param If_ $node
|
2024-03-21 11:05:56 +00:00
|
|
|
* @return Stmt[]|null|int|If_
|
2021-02-09 13:31:11 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node)
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
2023-06-10 18:53:37 +00:00
|
|
|
if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($node)) {
|
2021-08-13 20:58:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof Instanceof_) {
|
2023-06-05 08:01:06 +00:00
|
|
|
return $this->refactorStmtAndInstanceof($node, $node->cond->expr);
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2024-03-21 11:05:56 +00:00
|
|
|
if ($node->cond instanceof BooleanAnd) {
|
|
|
|
return $this->refactorIfWithBooleanAnd($node);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->cond instanceof Instanceof_) {
|
2023-06-05 08:01:06 +00:00
|
|
|
return $this->refactorStmtAndInstanceof($node, $node->cond);
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2021-10-23 21:09:26 +00:00
|
|
|
return null;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2022-05-07 09:38:38 +00:00
|
|
|
/**
|
2023-06-05 08:01:06 +00:00
|
|
|
* @return null|Stmt[]|int
|
2022-05-07 09:38:38 +00:00
|
|
|
*/
|
2023-06-05 08:01:06 +00:00
|
|
|
private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof)
|
2021-02-09 13:31:11 +00:00
|
|
|
{
|
2024-03-21 11:05:56 +00:00
|
|
|
if ($this->isInstanceofTheSameType($instanceof) !== \true) {
|
2021-02-09 13:31:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-09-08 14:55:40 +00:00
|
|
|
if ($this->shouldSkipFromNotTypedParam($instanceof)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-03 20:12:56 +00:00
|
|
|
if ($instanceof->expr instanceof Assign) {
|
|
|
|
$assignExpression = new Expression($instanceof->expr);
|
|
|
|
return \array_merge([$assignExpression], $if->stmts);
|
|
|
|
}
|
2023-06-05 08:01:06 +00:00
|
|
|
if ($if->cond !== $instanceof) {
|
|
|
|
return NodeTraverser::REMOVE_NODE;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2023-06-05 08:01:06 +00:00
|
|
|
if ($if->stmts === []) {
|
|
|
|
return NodeTraverser::REMOVE_NODE;
|
|
|
|
}
|
2023-06-10 18:53:37 +00:00
|
|
|
// unwrap stmts
|
2023-06-05 08:01:06 +00:00
|
|
|
return $if->stmts;
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipFromNotTypedParam(Instanceof_ $instanceof) : bool
|
2021-09-08 14:55:40 +00:00
|
|
|
{
|
2023-06-10 18:53:37 +00:00
|
|
|
$nativeParamType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
|
|
|
|
return $nativeParamType instanceof MixedType;
|
2021-09-08 14:55:40 +00:00
|
|
|
}
|
2023-06-10 18:53:37 +00:00
|
|
|
private function isPropertyFetch(Expr $expr) : bool
|
2021-07-27 14:23:09 +00:00
|
|
|
{
|
2023-06-10 18:53:37 +00:00
|
|
|
if ($expr instanceof PropertyFetch) {
|
2021-07-28 07:07:05 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2023-06-10 18:53:37 +00:00
|
|
|
return $expr instanceof StaticPropertyFetch;
|
2021-07-27 14:23:09 +00:00
|
|
|
}
|
2024-03-21 11:05:56 +00:00
|
|
|
private function isInstanceofTheSameType(Instanceof_ $instanceof) : ?bool
|
|
|
|
{
|
|
|
|
if (!$instanceof->class instanceof Name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// handled in another rule
|
|
|
|
if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-04-12 09:47:55 +00:00
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
|
|
|
|
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-03-21 11:05:56 +00:00
|
|
|
$classType = $this->nodeTypeResolver->getType($instanceof->class);
|
2024-03-21 11:37:34 +00:00
|
|
|
$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
|
2024-03-21 11:05:56 +00:00
|
|
|
if ($classType->equals($exprType)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $classType->isSuperTypeOf($exprType)->yes();
|
|
|
|
}
|
|
|
|
private function refactorIfWithBooleanAnd(If_ $if) : ?\PhpParser\Node\Stmt\If_
|
|
|
|
{
|
|
|
|
if (!$if->cond instanceof BooleanAnd) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$booleanAnd = $if->cond;
|
|
|
|
if (!$booleanAnd->left instanceof Instanceof_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$instanceof = $booleanAnd->left;
|
|
|
|
if ($this->isInstanceofTheSameType($instanceof) !== \true) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$if->cond = $booleanAnd->right;
|
|
|
|
return $if;
|
|
|
|
}
|
2021-02-09 13:31:11 +00:00
|
|
|
}
|