diff --git a/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/skip_bool_type_trait.php.inc b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/skip_bool_type_trait.php.inc new file mode 100644 index 00000000000..97d51ef05e5 --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/skip_bool_type_trait.php.inc @@ -0,0 +1,22 @@ + */ + protected array $dispatchesEvents = []; + + protected function fireResourceEvent(string $event, SomeModel $model, mixed ...$args): void + { + $class = $this->dispatchesEvents[$event] ?? null; + + if ($class === null) { + return; + } + + some_event(new $class($model, ...$args)); + } +} + +?> diff --git a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php index 0fed21a7697..b17a1b2678e 100644 --- a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php +++ b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php @@ -18,6 +18,7 @@ use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use PhpParser\NodeVisitor; +use PHPStan\Reflection\ClassReflection; use PHPStan\Type\IntersectionType; use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer; use Rector\NodeAnalyzer\ExprAnalyzer; @@ -118,11 +119,17 @@ public function refactor(Node $node): int|null|array|If_ return null; } - $type = ScopeFetcher::fetch($node)->getNativeType($node->cond); + $scope = ScopeFetcher::fetch($node); + $type = $scope->getNativeType($node->cond); if (! $type->isTrue()->yes()) { return null; } + $classReflection = $scope->getClassReflection(); + if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) { + return null; + } + if ($node->stmts === []) { return NodeVisitor::REMOVE_NODE; }