Skip to content

Commit 7d158ab

Browse files
authored
[DeadCode] Skip trait on RemoveAlwaysTrueIfConditionRector (#7759)
1 parent f914c84 commit 7d158ab

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\NullCompareInTrait\Fixture;
4+
5+
trait SkipBoolTypeTrait
6+
{
7+
/** @var array<string,class-string> */
8+
protected array $dispatchesEvents = [];
9+
10+
protected function fireResourceEvent(string $event, SomeModel $model, mixed ...$args): void
11+
{
12+
$class = $this->dispatchesEvents[$event] ?? null;
13+
14+
if ($class === null) {
15+
return;
16+
}
17+
18+
some_event(new $class($model, ...$args));
19+
}
20+
}
21+
22+
?>

rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpParser\Node\Stmt\Else_;
1919
use PhpParser\Node\Stmt\If_;
2020
use PhpParser\NodeVisitor;
21+
use PHPStan\Reflection\ClassReflection;
2122
use PHPStan\Type\IntersectionType;
2223
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
2324
use Rector\NodeAnalyzer\ExprAnalyzer;
@@ -118,11 +119,17 @@ public function refactor(Node $node): int|null|array|If_
118119
return null;
119120
}
120121

121-
$type = ScopeFetcher::fetch($node)->getNativeType($node->cond);
122+
$scope = ScopeFetcher::fetch($node);
123+
$type = $scope->getNativeType($node->cond);
122124
if (! $type->isTrue()->yes()) {
123125
return null;
124126
}
125127

128+
$classReflection = $scope->getClassReflection();
129+
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
130+
return null;
131+
}
132+
126133
if ($node->stmts === []) {
127134
return NodeVisitor::REMOVE_NODE;
128135
}

0 commit comments

Comments
 (0)