Skip to content

Commit

Permalink
Revert "More match fixes"
Browse files Browse the repository at this point in the history
This reverts commit d87b3a5.
  • Loading branch information
ondrejmirtes committed Jan 15, 2024
1 parent 25d7a5e commit 3d4486d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1527,20 +1527,17 @@ private function resolveType(string $exprString, Expr $node): Type
);
}

$noopCallback = static function (): void {
};
$filteringExprResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($filteringExpr), $filteringExpr, $matchScope, $noopCallback, ExpressionContext::createDeep());

$filteringExprType = $matchScope->getType($filteringExpr);

if (!$filteringExprType->isFalse()->yes()) {
$truthyScope = $filteringExprResult->getTruthyScope();
$truthyScope = $matchScope->filterByTruthyValue($filteringExpr);
if ($node->hasAttribute(self::KEEP_VOID_ATTRIBUTE_NAME)) {
$arm->body->setAttribute(self::KEEP_VOID_ATTRIBUTE_NAME, $node->getAttribute(self::KEEP_VOID_ATTRIBUTE_NAME));
}
$types[] = $truthyScope->getType($arm->body);
}

$matchScope = $filteringExprResult->getFalseyScope();
$matchScope = $matchScope->filterByFalseyValue($filteringExpr);
}

return TypeCombinator::union(...$types);
Expand Down
11 changes: 5 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2885,24 +2885,23 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
);
}

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

$armResult = $this->processExprNode(
$stmt,
$arm->body,
$truthyScope,
$bodyScope,
$nodeCallback,
ExpressionContext::createTopLevel(),
);
$armScope = $armResult->getScope();
$scope = $scope->mergeWith($armScope);
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$matchScope = $filteringExprResult->getFalseyScope();
$matchScope = $matchScope->filterByFalseyValue($filteringExpr);
}

$remainingType = $matchScope->getType($expr->cond);
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,13 @@ public function testBug6407(): void
$this->analyse([__DIR__ . '/data/bug-6407.php'], []);
}

public function testBugUnhandledTrueWithComplexCondition(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-unhandled-true-with-complex-condition.php'], []);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php // lint >= 8.1

namespace MatchUnhandledTrueWithComplexCondition;

enum Bar
{

case ONE;
case TWO;
case THREE;

}

class Foo
{

public Bar $type;

public function getRand(): int
{
return rand(0, 10);
}

public function getPriority(): int
{
return match (true) {
$this->type === Bar::ONE => 0,
$this->type === Bar::TWO && $this->getRand() !== 8 => 1,
$this->type === BAR::THREE => 2,
$this->type === BAR::TWO => 3,
};
}

}

0 comments on commit 3d4486d

Please sign in to comment.