Skip to content

Commit

Permalink
[DeadCode] Skip $this in trait on RemoveDeadInstanceOfRector (#5818)
Browse files Browse the repository at this point in the history
* [DeadCode] Skip $this in trait on RemoveDeadInstanceOfRector

* fix

* [ci-review] Rector Rectify

* clean up

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 12, 2024
1 parent 7d69fc2 commit ec59456
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

use DateTimeInterface;

trait SkipTraitThis
{
public function getIssue(): void
{
foreach (['value_1', 'value_2'] as $value) {
if ($this instanceof DateTimeInterface && $this->getExample() === false) {
$this->setExample('demo');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace Rector\DeadCode\NodeAnalyzer;

use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Reflection\ReflectionResolver;

Expand All @@ -23,8 +22,7 @@
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ExprAnalyzer $exprAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
private readonly NodeTypeResolver $nodeTypeResolver
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -54,7 +52,7 @@ public function isSafe(BooleanAnd|BooleanOr $booleanAnd): bool
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
return ! (bool) $this->betterNodeFinder->findFirst(
$booleanAnd->left,
fn (Node $node): bool => $this->nodeTypeResolver->getType($node) instanceof ObjectType
static fn(Node $node): bool => $node instanceof Instanceof_
);
}

Expand Down
8 changes: 8 additions & 0 deletions rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DeadCode\Rector\If_;

use PHPStan\Reflection\ClassReflection;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
Expand All @@ -21,6 +22,7 @@
use PHPStan\Type\MixedType;
use Rector\NodeManipulator\IfManipulator;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -31,6 +33,7 @@ final class RemoveDeadInstanceOfRector extends AbstractRector
{
public function __construct(
private readonly IfManipulator $ifManipulator,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -148,6 +151,11 @@ private function isInstanceofTheSameType(Instanceof_ $instanceof): ?bool
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
return null;
}

$classType = $this->nodeTypeResolver->getType($instanceof->class);
$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);

Expand Down

0 comments on commit ec59456

Please sign in to comment.