From da67bb97a141fd0feb2ab90f5576138489f2f67d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 21 Mar 2024 12:03:36 +0100 Subject: [PATCH] Add boolean and support to RemoveDeadInstanceOfRector (#5748) * Add fixture to RemoveDeadInstanceOfRector * add boolean and support to RemoveDeadInstanceOfRector --- .../Fixture/include_and.php.inc | 33 +++++++++ .../Rector/If_/RemoveDeadInstanceOfRector.php | 68 ++++++++++++++----- 2 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/If_/RemoveDeadInstanceOfRector/Fixture/include_and.php.inc diff --git a/rules-tests/DeadCode/Rector/If_/RemoveDeadInstanceOfRector/Fixture/include_and.php.inc b/rules-tests/DeadCode/Rector/If_/RemoveDeadInstanceOfRector/Fixture/include_and.php.inc new file mode 100644 index 00000000000..483fb23e07b --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/RemoveDeadInstanceOfRector/Fixture/include_and.php.inc @@ -0,0 +1,33 @@ +getLine() === 100) { + } + } +} + +?> +----- +getLine() === 100) { + } + } +} + +?> diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index d03cf9f03fd..bd268ba565a 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,9 +69,9 @@ 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; @@ -80,6 +81,10 @@ public function refactor(Node $node): array|null|int 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); } @@ -92,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) !== true) { return null; } @@ -146,4 +136,46 @@ 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); + + if ($classType->equals($exprType)) { + return true; + } + + return $classType->isSuperTypeOf($exprType) + ->yes(); + } + + private function refactorIfWithBooleanAnd(If_ $if): null|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; + } }