1515use PhpParser \Node \Stmt \Switch_ ;
1616use PHPStan \Type \ObjectType ;
1717use Rector \Contract \PhpParser \Node \StmtsAwareInterface ;
18+ use Rector \NodeAnalyzer \ExprAnalyzer ;
1819use Rector \Php80 \NodeAnalyzer \MatchSwitchAnalyzer ;
1920use Rector \Php80 \NodeFactory \MatchFactory ;
2021use Rector \Php80 \NodeResolver \SwitchExprsResolver ;
2122use Rector \Php80 \ValueObject \CondAndExpr ;
2223use Rector \Php80 \ValueObject \MatchResult ;
24+ use Rector \PhpParser \Node \Value \ValueResolver ;
2325use Rector \Rector \AbstractRector ;
2426use Rector \ValueObject \PhpVersionFeature ;
2527use 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 */
0 commit comments