Skip to content

Commit

Permalink
add boolean and support to RemoveDeadInstanceOfRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 21, 2024
1 parent 5602ed6 commit 9ee68a1
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 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,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);
}
Expand All @@ -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;
}

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

0 comments on commit 9ee68a1

Please sign in to comment.