Skip to content

Commit 48a35f8

Browse files
committed
Updated Rector to commit dd217d9d2a02a69eb55aa9a5f66f3f6914d74b2b
rectorphp/rector-src@dd217d9 [Php80] Handle dynamic bool expr on ChangeSwitchToMatchRector (#6961)
1 parent a219e97 commit 48a35f8

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
use PhpParser\Node\Stmt\Switch_;
1616
use PHPStan\Type\ObjectType;
1717
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
18+
use Rector\NodeAnalyzer\ExprAnalyzer;
1819
use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer;
1920
use Rector\Php80\NodeFactory\MatchFactory;
2021
use Rector\Php80\NodeResolver\SwitchExprsResolver;
2122
use Rector\Php80\ValueObject\CondAndExpr;
2223
use Rector\Php80\ValueObject\MatchResult;
24+
use Rector\PhpParser\Node\Value\ValueResolver;
2325
use Rector\Rector\AbstractRector;
2426
use Rector\ValueObject\PhpVersionFeature;
2527
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -42,11 +44,21 @@ final class ChangeSwitchToMatchRector extends AbstractRector implements MinPhpVe
4244
* @readonly
4345
*/
4446
private MatchFactory $matchFactory;
45-
public function __construct(SwitchExprsResolver $switchExprsResolver, MatchSwitchAnalyzer $matchSwitchAnalyzer, MatchFactory $matchFactory)
47+
/**
48+
* @readonly
49+
*/
50+
private ValueResolver $valueResolver;
51+
/**
52+
* @readonly
53+
*/
54+
private ExprAnalyzer $exprAnalyzer;
55+
public function __construct(SwitchExprsResolver $switchExprsResolver, MatchSwitchAnalyzer $matchSwitchAnalyzer, MatchFactory $matchFactory, ValueResolver $valueResolver, ExprAnalyzer $exprAnalyzer)
4656
{
4757
$this->switchExprsResolver = $switchExprsResolver;
4858
$this->matchSwitchAnalyzer = $matchSwitchAnalyzer;
4959
$this->matchFactory = $matchFactory;
60+
$this->valueResolver = $valueResolver;
61+
$this->exprAnalyzer = $exprAnalyzer;
5062
}
5163
public function getRuleDefinition() : RuleDefinition
5264
{
@@ -114,6 +126,7 @@ public function refactor(Node $node) : ?Node
114126
$assignVar = $this->resolveAssignVar($condAndExprs);
115127
$hasDefaultValue = $this->matchSwitchAnalyzer->hasDefaultValue($match);
116128
$this->castMatchCond($match);
129+
$this->mirrorDynamicBoolExpr($match);
117130
if ($assignVar instanceof Expr) {
118131
if (!$hasDefaultValue) {
119132
continue;
@@ -176,6 +189,32 @@ private function castMatchCond(Match_ $match) : void
176189
$match->cond = $newMatchCond;
177190
}
178191
}
192+
private function mirrorDynamicBoolExpr(Match_ $match) : void
193+
{
194+
if ($this->valueResolver->isTrue($match->cond)) {
195+
return;
196+
}
197+
$isChanged = \false;
198+
foreach ($match->arms as $arm) {
199+
if ($arm->conds === null) {
200+
continue;
201+
}
202+
foreach ($arm->conds as $cond) {
203+
$type = $this->nodeTypeResolver->getNativeType($cond);
204+
if (!$this->exprAnalyzer->isDynamicExpr($cond) || !$type->isBoolean()->yes()) {
205+
// return early here, as condition is mixed
206+
// we need another real use case for mixed conditions of dynamic + non-dynamic case expr
207+
return;
208+
}
209+
$isChanged = \true;
210+
// dont' stop lookup for dynamic conditions
211+
// continue verify other condition, in case of mixed condition
212+
}
213+
}
214+
if ($isChanged) {
215+
$match->cond = $this->nodeFactory->createTrue();
216+
}
217+
}
179218
/**
180219
* @param CondAndExpr[] $condAndExprs
181220
*/

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = 'a4c6684402ac40cd79fdd2f00de98f041fac13a0';
22+
public const PACKAGE_VERSION = 'dd217d9d2a02a69eb55aa9a5f66f3f6914d74b2b';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-06-08 00:41:26';
27+
public const RELEASE_DATE = '2025-06-08 13:08:47';
2828
/**
2929
* @var int
3030
*/

0 commit comments

Comments
 (0)