@@ -4207,35 +4207,59 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
42074207 continue ;
42084208 }
42094209
4210- $ condNodes = [];
4211- $ conditionCases = [];
4212- $ conditionExprs = [] ;
4210+ // First pass: validate all conditions are enum case references
4211+ $ validatedConds = [];
4212+ $ allCondsValid = true ;
42134213 foreach ($ arm ->conds as $ j => $ cond ) {
42144214 if (!$ cond instanceof Expr \ClassConstFetch) {
4215- continue 2 ;
4215+ $ allCondsValid = false ;
4216+ break ;
42164217 }
42174218 if (!$ cond ->class instanceof Name) {
4218- continue 2 ;
4219+ $ allCondsValid = false ;
4220+ break ;
42194221 }
42204222 if (!$ cond ->name instanceof Node \Identifier) {
4221- continue 2 ;
4223+ $ allCondsValid = false ;
4224+ break ;
42224225 }
42234226 $ fetchedClassName = $ scope ->resolveName ($ cond ->class );
42244227 $ loweredFetchedClassName = strtolower ($ fetchedClassName );
42254228 if (!array_key_exists ($ loweredFetchedClassName , $ indexedEnumCases )) {
4226- continue 2 ;
4229+ $ allCondsValid = false ;
4230+ break ;
42274231 }
4232+ $ caseName = $ cond ->name ->toString ();
4233+ if (!array_key_exists ($ caseName , $ indexedEnumCases [$ loweredFetchedClassName ])) {
4234+ $ allCondsValid = false ;
4235+ break ;
4236+ }
4237+ $ validatedConds [$ j ] = [
4238+ 'cond ' => $ cond ,
4239+ 'loweredFetchedClassName ' => $ loweredFetchedClassName ,
4240+ 'caseName ' => $ caseName ,
4241+ 'enumCase ' => $ indexedEnumCases [$ loweredFetchedClassName ][$ caseName ],
4242+ ];
4243+ }
4244+
4245+ if (!$ allCondsValid ) {
4246+ continue ;
4247+ }
4248+
4249+ $ condNodes = [];
4250+ $ conditionCases = [];
4251+ $ conditionExprs = [];
4252+ // Second pass: process validated conditions with side effects
4253+ foreach ($ validatedConds as $ j => $ validatedCond ) {
4254+ $ cond = $ validatedCond ['cond ' ];
4255+ $ loweredFetchedClassName = $ validatedCond ['loweredFetchedClassName ' ];
4256+ $ caseName = $ validatedCond ['caseName ' ];
4257+ $ enumCase = $ validatedCond ['enumCase ' ];
42284258
42294259 if (!array_key_exists ($ loweredFetchedClassName , $ unusedIndexedEnumCases )) {
42304260 throw new ShouldNotHappenException ();
42314261 }
42324262
4233- $ caseName = $ cond ->name ->toString ();
4234- if (!array_key_exists ($ caseName , $ indexedEnumCases [$ loweredFetchedClassName ])) {
4235- continue 2 ;
4236- }
4237-
4238- $ enumCase = $ indexedEnumCases [$ loweredFetchedClassName ][$ caseName ];
42394263 $ conditionCases [] = $ enumCase ;
42404264 $ armConditionScope = $ matchScope ;
42414265 if (!array_key_exists ($ caseName , $ unusedIndexedEnumCases [$ loweredFetchedClassName ])) {
0 commit comments