@@ -3644,6 +3644,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
3644
3644
3645
3645
$ condNodes = [];
3646
3646
$ conditionCases = [];
3647
+ $ conditionExprs = [];
3647
3648
foreach ($ arm ->conds as $ cond ) {
3648
3649
if (!$ cond instanceof Expr \ClassConstFetch) {
3649
3650
continue 2 ;
@@ -3701,6 +3702,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
3701
3702
$ armConditionScope ,
3702
3703
$ cond ->getStartLine (),
3703
3704
);
3705
+ $ conditionExprs [] = $ cond ;
3704
3706
3705
3707
unset($ unusedIndexedEnumCases [$ loweredFetchedClassName ][$ caseName ]);
3706
3708
}
@@ -3714,10 +3716,11 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
3714
3716
$ conditionCaseType = new UnionType ($ conditionCases );
3715
3717
}
3716
3718
3719
+ $ filteringExpr = $ this ->getFilteringExprForMatchArm ($ expr , $ conditionExprs );
3717
3720
$ matchArmBodyScope = $ matchScope ->addTypeToExpression (
3718
3721
$ expr ->cond ,
3719
3722
$ conditionCaseType ,
3720
- );
3723
+ )-> filterByTruthyValue ( $ filteringExpr ) ;
3721
3724
$ matchArmBody = new MatchExpressionArmBody ($ matchArmBodyScope , $ arm ->body );
3722
3725
$ armNodes [$ i ] = new MatchExpressionArm ($ matchArmBody , $ condNodes , $ arm ->getStartLine ());
3723
3726
@@ -3793,22 +3796,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
3793
3796
$ filteringExprs [] = $ armCond ;
3794
3797
}
3795
3798
3796
- if (count ($ filteringExprs ) === 1 ) {
3797
- $ filteringExpr = new BinaryOp \Identical ($ expr ->cond , $ filteringExprs [0 ]);
3798
- } else {
3799
- $ items = [];
3800
- foreach ($ filteringExprs as $ filteringExpr ) {
3801
- $ items [] = new Node \ArrayItem ($ filteringExpr );
3802
- }
3803
- $ filteringExpr = new FuncCall (
3804
- new Name \FullyQualified ('in_array ' ),
3805
- [
3806
- new Arg ($ expr ->cond ),
3807
- new Arg (new Array_ ($ items )),
3808
- new Arg (new ConstFetch (new Name \FullyQualified ('true ' ))),
3809
- ],
3810
- );
3811
- }
3799
+ $ filteringExpr = $ this ->getFilteringExprForMatchArm ($ expr , $ filteringExprs );
3812
3800
3813
3801
$ bodyScope = $ this ->processExprNode ($ stmt , $ filteringExpr , $ matchScope , static function (): void {
3814
3802
}, $ deepContext )->getTruthyScope ();
@@ -6591,4 +6579,28 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding):
6591
6579
return $ stmts ;
6592
6580
}
6593
6581
6582
+ /**
6583
+ * @param array<Expr> $conditions
6584
+ */
6585
+ public function getFilteringExprForMatchArm (Expr \Match_ $ expr , array $ conditions ): BinaryOp \Identical |FuncCall
6586
+ {
6587
+ if (count ($ conditions ) === 1 ) {
6588
+ return new BinaryOp \Identical ($ expr ->cond , $ conditions [0 ]);
6589
+ }
6590
+
6591
+ $ items = [];
6592
+ foreach ($ conditions as $ filteringExpr ) {
6593
+ $ items [] = new Node \ArrayItem ($ filteringExpr );
6594
+ }
6595
+
6596
+ return new FuncCall (
6597
+ new Name \FullyQualified ('in_array ' ),
6598
+ [
6599
+ new Arg ($ expr ->cond ),
6600
+ new Arg (new Array_ ($ items )),
6601
+ new Arg (new ConstFetch (new Name \FullyQualified ('true ' ))),
6602
+ ],
6603
+ );
6604
+ }
6605
+
6594
6606
}
0 commit comments