diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index f40ac8c5215..76dc62b9493 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Instanceof_; @@ -68,20 +69,22 @@ public function getNodeTypes(): array /** * @param If_ $node - * @return Stmt[]|null|int + * @return Stmt[]|null|int|If_ */ - public function refactor(Node $node): array|null|int + public function refactor(Node $node): array|null|int|If_ { if (! $this->ifManipulator->isIfWithoutElseAndElseIfs($node)) { return null; } - die; - if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof Instanceof_) { return $this->refactorStmtAndInstanceof($node, $node->cond->expr); } + if ($node->cond instanceof BooleanAnd) { + return $this->refactorIfWithBooleanAnd($node); + } + if ($node->cond instanceof Instanceof_) { return $this->refactorStmtAndInstanceof($node, $node->cond); } @@ -94,22 +97,7 @@ public function refactor(Node $node): array|null|int */ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): null|array|int { - if (! $instanceof->class instanceof Name) { - return null; - } - - // handle in another rule - if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) { - return null; - } - - $classType = $this->nodeTypeResolver->getType($instanceof->class); - $exprType = $this->nodeTypeResolver->getType($instanceof->expr); - - $isSameStaticTypeOrSubtype = $classType->equals($exprType) || $classType->isSuperTypeOf($exprType) - ->yes(); - - if (! $isSameStaticTypeOrSubtype) { + if (! $this->isInstanceofTheSameType($instanceof)) { return null; } @@ -148,4 +136,44 @@ private function isPropertyFetch(Expr $expr): bool return $expr instanceof StaticPropertyFetch; } + + 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; + } + + $classType = $this->nodeTypeResolver->getType($instanceof->class); + $exprType = $this->nodeTypeResolver->getType($instanceof->expr); + + $isSameStaticTypeOrSubtype = $classType->equals($exprType) || $classType->isSuperTypeOf($exprType) + ->yes(); + + return $isSameStaticTypeOrSubtype; + } + + private function refactorIfWithBooleanAnd(If_ $if): null|If_ + { + if (! $if->cond instanceof BooleanAnd) { + return null; + } + + $booleanAnd = $if->cond; + if (! $booleanAnd->left instanceof Instanceof_) { + return new MixedType(); + } + + $instanceof = $booleanAnd->left; + if (! $this->isInstanceofTheSameType($instanceof)) { + return null; + } + + $if->cond = $booleanAnd->right; + return $if; + } }