Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boolean and support to RemoveDeadInstanceOfRector #5748

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
samsonasik marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}