Skip to content

Commit 3d4486d

Browse files
committed
Revert "More match fixes"
This reverts commit d87b3a5.
1 parent 25d7a5e commit 3d4486d

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

src/Analyser/MutatingScope.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -1527,20 +1527,17 @@ private function resolveType(string $exprString, Expr $node): Type
15271527
);
15281528
}
15291529

1530-
$noopCallback = static function (): void {
1531-
};
1532-
$filteringExprResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($filteringExpr), $filteringExpr, $matchScope, $noopCallback, ExpressionContext::createDeep());
1533-
15341530
$filteringExprType = $matchScope->getType($filteringExpr);
1531+
15351532
if (!$filteringExprType->isFalse()->yes()) {
1536-
$truthyScope = $filteringExprResult->getTruthyScope();
1533+
$truthyScope = $matchScope->filterByTruthyValue($filteringExpr);
15371534
if ($node->hasAttribute(self::KEEP_VOID_ATTRIBUTE_NAME)) {
15381535
$arm->body->setAttribute(self::KEEP_VOID_ATTRIBUTE_NAME, $node->getAttribute(self::KEEP_VOID_ATTRIBUTE_NAME));
15391536
}
15401537
$types[] = $truthyScope->getType($arm->body);
15411538
}
15421539

1543-
$matchScope = $filteringExprResult->getFalseyScope();
1540+
$matchScope = $matchScope->filterByFalseyValue($filteringExpr);
15441541
}
15451542

15461543
return TypeCombinator::union(...$types);

src/Analyser/NodeScopeResolver.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -2885,24 +2885,23 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
28852885
);
28862886
}
28872887

2888-
$filteringExprResult = $this->processExprNode($stmt, $filteringExpr, $matchScope, static function (): void {
2889-
}, $deepContext);
2890-
$truthyScope = $filteringExprResult->getTruthyScope();
2891-
$matchArmBody = new MatchExpressionArmBody($truthyScope, $arm->body);
2888+
$bodyScope = $this->processExprNode($stmt, $filteringExpr, $matchScope, static function (): void {
2889+
}, $deepContext)->getTruthyScope();
2890+
$matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body);
28922891
$armNodes[] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getLine());
28932892

28942893
$armResult = $this->processExprNode(
28952894
$stmt,
28962895
$arm->body,
2897-
$truthyScope,
2896+
$bodyScope,
28982897
$nodeCallback,
28992898
ExpressionContext::createTopLevel(),
29002899
);
29012900
$armScope = $armResult->getScope();
29022901
$scope = $scope->mergeWith($armScope);
29032902
$hasYield = $hasYield || $armResult->hasYield();
29042903
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
2905-
$matchScope = $filteringExprResult->getFalseyScope();
2904+
$matchScope = $matchScope->filterByFalseyValue($filteringExpr);
29062905
}
29072906

29082907
$remainingType = $matchScope->getType($expr->cond);

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,13 @@ public function testBug6407(): void
534534
$this->analyse([__DIR__ . '/data/bug-6407.php'], []);
535535
}
536536

537+
public function testBugUnhandledTrueWithComplexCondition(): void
538+
{
539+
if (PHP_VERSION_ID < 80100) {
540+
$this->markTestSkipped('Test requires PHP 8.1.');
541+
}
542+
543+
$this->analyse([__DIR__ . '/data/bug-unhandled-true-with-complex-condition.php'], []);
544+
}
545+
537546
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php // lint >= 8.1
2+
3+
namespace MatchUnhandledTrueWithComplexCondition;
4+
5+
enum Bar
6+
{
7+
8+
case ONE;
9+
case TWO;
10+
case THREE;
11+
12+
}
13+
14+
class Foo
15+
{
16+
17+
public Bar $type;
18+
19+
public function getRand(): int
20+
{
21+
return rand(0, 10);
22+
}
23+
24+
public function getPriority(): int
25+
{
26+
return match (true) {
27+
$this->type === Bar::ONE => 0,
28+
$this->type === Bar::TWO && $this->getRand() !== 8 => 1,
29+
$this->type === BAR::THREE => 2,
30+
$this->type === BAR::TWO => 3,
31+
};
32+
}
33+
34+
}

0 commit comments

Comments
 (0)