diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index b4373013af5..4a8450bfaf4 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -6,6 +6,7 @@ namespace Rector\DeadCode\Rector\If_; 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_; @@ -64,7 +65,7 @@ CODE_SAMPLE } /** * @param If_ $node - * @return Stmt[]|null|int + * @return Stmt[]|null|int|If_ */ public function refactor(Node $node) { @@ -74,6 +75,9 @@ CODE_SAMPLE 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); } @@ -84,17 +88,7 @@ CODE_SAMPLE */ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof) { - 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) !== \true) { return null; } if ($this->shouldSkipFromNotTypedParam($instanceof)) { @@ -125,4 +119,36 @@ CODE_SAMPLE } 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); + 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; + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 836bdf64a07..fad6ecc9ce4 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '0c177409ed477e978433597639335fb2a78bc331'; + public const PACKAGE_VERSION = 'da67bb97a141fd0feb2ab90f5576138489f2f67d'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-03-21 11:01:32'; + public const RELEASE_DATE = '2024-03-21 11:03:36'; /** * @var int */