Skip to content

Commit

Permalink
Add boolean and support to RemoveDeadInstanceOfRector (#5748)
Browse files Browse the repository at this point in the history
* Add fixture to RemoveDeadInstanceOfRector

* add boolean and support to RemoveDeadInstanceOfRector
  • Loading branch information
TomasVotruba committed Mar 21, 2024
1 parent 0c17740 commit da67bb9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadInstanceOfRector\Fixture;

use PhpParser\Node;

final class IncludeAnd
{
public function go(Node $var)
{
if ($var instanceof Node && $var->getLine() === 100) {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadInstanceOfRector\Fixture;

use PhpParser\Node;

final class IncludeAnd
{
public function go(Node $var)
{
if ($var->getLine() === 100) {
}
}
}

?>
68 changes: 50 additions & 18 deletions rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit da67bb9

Please sign in to comment.