Skip to content

Commit 86456bd

Browse files
[DeadCode] Skip mixed fallback null on trait on RemoveAlwaysTrueIfConditionRector (#7098)
* [DeadCode] Skip mixed fallback null on trait on RemoveAlwaysTrueIfConditionRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 0ae8f53 commit 86456bd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;
4+
5+
trait SkipMixedFallbackNullOnTrait
6+
{
7+
/**
8+
* @array<string, string>
9+
*/
10+
private $dispatchesEvents;
11+
12+
protected function fireResourceEvent(string $event, Model $model, mixed ...$args): void
13+
{
14+
if ($this->silent) {
15+
return;
16+
}
17+
18+
$event = $this->dispatchesEvents[$event] ?? null;
19+
20+
if (is_null($event)) {
21+
return;
22+
}
23+
24+
event(new $event($model, ...$args));
25+
}
26+
}

rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
2323
use Rector\NodeAnalyzer\ExprAnalyzer;
2424
use Rector\PhpParser\Node\BetterNodeFinder;
25+
use Rector\PHPStan\ScopeFetcher;
2526
use Rector\Rector\AbstractRector;
2627
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2728
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -116,6 +117,11 @@ public function refactor(Node $node): int|null|array|If_
116117
return null;
117118
}
118119

120+
$type = ScopeFetcher::fetch($node)->getNativeType($node->cond);
121+
if (! $type->isTrue()->yes()) {
122+
return null;
123+
}
124+
119125
if ($node->stmts === []) {
120126
return NodeVisitor::REMOVE_NODE;
121127
}

0 commit comments

Comments
 (0)