Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,7 @@ private static function intersectButNotNever(Type $nativeType, Type $inferredTyp
return $result;
}

public function enterMatch(Expr\Match_ $expr): self
public function enterMatch(Expr\Match_ $expr, Type $condType, Type $condNativeType): self
{
if ($expr->cond instanceof Variable) {
return $this;
Expand All @@ -3001,8 +3001,8 @@ public function enterMatch(Expr\Match_ $expr): self
return $this;
}

$type = $this->getType($cond);
$nativeType = $this->getNativeType($cond);
$type = $condType;
$nativeType = $condNativeType;
$condExpr = new AlwaysRememberedExpr($cond, $type, $nativeType);
$expr->cond = $condExpr;

Expand Down
3 changes: 2 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4179,13 +4179,14 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
} elseif ($expr instanceof Expr\Match_) {
$deepContext = $context->enterDeep();
$condType = $scope->getType($expr->cond);
$condNativeType = $scope->getNativeType($expr->cond);
$condResult = $this->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $deepContext);
$scope = $condResult->getScope();
$hasYield = $condResult->hasYield();
$throwPoints = $condResult->getThrowPoints();
$impurePoints = $condResult->getImpurePoints();
$isAlwaysTerminating = $condResult->isAlwaysTerminating();
$matchScope = $scope->enterMatch($expr);
$matchScope = $scope->enterMatch($expr, $condType, $condNativeType);
$armNodes = [];
$hasDefaultCond = false;
$hasAlwaysTrueCond = false;
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,23 @@ public function testBug9534(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug11310(): void
{
$this->analyse([__DIR__ . '/data/bug-11310.php'], [
[
'Match arm comparison between int<1, max> and 0 is always false.',
24,
],
[
'Match arm comparison between int<1, 6>|int<8, 14> and 0 is always false.',
49,
],
[
'Match arm comparison between 1|2|3|4|5|6 and 0 is always false.',
74,
],
]);
}

}
78 changes: 78 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11310.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php // lint >= 8.0

namespace Bug11310;

/** @param int<0, max> $i */
function foo(int $i): void {
echo match ($i++) {
0 => 'zero',
default => 'default',
};
}

/** @param int<0, max> $i */
function bar(int $i): void {
echo match ($i--) {
0 => 'zero',
default => 'default',
};
}

/** @param int<0, max> $i */
function baz(int $i): void {
echo match (++$i) {
0 => 'zero',
1 => 'one',
default => 'default',
};
}

/** @param int<0, 5>|int<7, 13> $i */
function foo2(int $i): void {
echo match ($i++) {
0 => 'zero',
default => 'default',
};
}

/** @param int<0, 5>|int<7, 13> $i */
function bar2(int $i): void {
echo match ($i--) {
0 => 'zero',
default => 'default',
};
}

/** @param int<0, 5>|int<7, 13> $i */
function baz2(int $i): void {
echo match (++$i) {
0 => 'zero',
1 => 'one',
default => 'default',
};
}

/** @param 0|1|2|3|4|5 $i */
function foo3(int $i): void {
echo match ($i++) {
0 => 'zero',
default => 'default',
};
}

/** @param 0|1|2|3|4|5 $i */
function bar3(int $i): void {
echo match ($i--) {
0 => 'zero',
default => 'default',
};
}

/** @param 0|1|2|3|4|5 $i */
function baz3(int $i): void {
echo match (++$i) {
0 => 'zero',
1 => 'one',
default => 'default',
};
}
Loading