@@ -4388,6 +4388,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
43884388 $ armCondScope = $ matchScope ;
43894389 $ condNodes = [];
43904390 $ armCondResultScope = $ matchScope ;
4391+ $ bodyScope = null ;
43914392 foreach ($ arm ->conds as $ j => $ armCond ) {
43924393 if (isset ($ armCondsToSkip [$ i ][$ j ])) {
43934394 continue ;
@@ -4403,36 +4404,17 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
44034404 if ($ armCondType ->isTrue ()->yes ()) {
44044405 $ hasAlwaysTrueCond = true ;
44054406 }
4406- $ armCondScope = $ armCondResult ->getScope ()->filterByFalseyValue ($ armCondExpr );
4407+ $ armCondScope = $ armCondResultScope ->filterByFalseyValue ($ armCondExpr );
4408+ if ($ bodyScope === null ) {
4409+ $ bodyScope = $ armCondResultScope ->filterByTruthyValue ($ armCondExpr );
4410+ } else {
4411+ $ bodyScope = $ bodyScope ->mergeWith ($ armCondResultScope ->filterByTruthyValue ($ armCondExpr ));
4412+ }
44074413 $ filteringExprs [] = $ armCond ;
44084414 }
44094415
44104416 $ filteringExpr = $ this ->getFilteringExprForMatchArm ($ expr , $ filteringExprs );
4411- $ bodyScope = $ matchScope ->filterByTruthyValue ($ filteringExpr );
4412- $ condResultScope = $ armCondResultScope ;
4413- $ matchScopeKnownVars = array_flip (array_merge ($ matchScope ->getDefinedVariables (), $ matchScope ->getMaybeDefinedVariables ()));
4414- foreach ($ condResultScope ->getDefinedVariables () as $ varName ) {
4415- if (isset ($ matchScopeKnownVars [$ varName ])) {
4416- continue ;
4417- }
4418- $ bodyScope = $ bodyScope ->assignVariable (
4419- $ varName ,
4420- $ condResultScope ->getVariableType ($ varName ),
4421- $ condResultScope ->getNativeType (new Variable ($ varName )),
4422- $ condResultScope ->hasVariableType ($ varName ),
4423- );
4424- }
4425- foreach ($ condResultScope ->getMaybeDefinedVariables () as $ varName ) {
4426- if (isset ($ matchScopeKnownVars [$ varName ])) {
4427- continue ;
4428- }
4429- $ bodyScope = $ bodyScope ->assignVariable (
4430- $ varName ,
4431- $ condResultScope ->getVariableType ($ varName ),
4432- $ condResultScope ->getNativeType (new Variable ($ varName )),
4433- $ condResultScope ->hasVariableType ($ varName ),
4434- );
4435- }
4417+ $ bodyScope ??= $ matchScope ->filterByTruthyValue ($ filteringExpr );
44364418 $ matchArmBody = new MatchExpressionArmBody ($ bodyScope , $ arm ->body );
44374419 $ armNodes [$ i ] = new MatchExpressionArm ($ matchArmBody , $ condNodes , $ arm ->getStartLine ());
44384420
@@ -4449,7 +4431,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
44494431 $ hasYield = $ hasYield || $ armResult ->hasYield ();
44504432 $ throwPoints = array_merge ($ throwPoints , $ armResult ->getThrowPoints ());
44514433 $ impurePoints = array_merge ($ impurePoints , $ armResult ->getImpurePoints ());
4452- $ matchScope = $ matchScope ->filterByFalseyValue ($ filteringExpr );
4434+ $ matchScope = $ armCondScope ->filterByFalseyValue ($ filteringExpr );
44534435 }
44544436
44554437 if (!$ hasDefaultCond && !$ hasAlwaysTrueCond && $ condType ->isBoolean ()->yes () && $ condType ->isConstantScalarValue ()->yes ()) {
0 commit comments