Skip to content

Commit 6e191bd

Browse files
Fix match expression and treatPhpDocTypesAsCertain
1 parent 2ca9793 commit 6e191bd

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

conf/config.level4.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ services:
131131
checkAlwaysTrueStrictComparison: %checkAlwaysTrueStrictComparison%
132132
disableUnreachable: %featureToggles.disableUnreachableBranchesRules%
133133
reportAlwaysTrueInLastCondition: %reportAlwaysTrueInLastCondition%
134+
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
134135
tags:
135136
- phpstan.rules.rule
136137

src/Rules/Comparison/MatchExpressionRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function __construct(
3030
private bool $checkAlwaysTrueStrictComparison,
3131
private bool $disableUnreachable,
3232
private bool $reportAlwaysTrueInLastCondition,
33+
private bool $treatPhpDocTypesAsCertain,
3334
)
3435
{
3536
}
@@ -64,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
6465
$matchCondition,
6566
$armCondition->getCondition(),
6667
);
67-
$armConditionResult = $armConditionScope->getType($armConditionExpr);
68+
$armConditionResult = ($this->treatPhpDocTypesAsCertain ? $armConditionScope->getType($armConditionExpr) : $armConditionScope->getNativeType($armConditionExpr));
6869
if (!$armConditionResult instanceof ConstantBooleanType) {
6970
continue;
7071
}

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function getRule(): Rule
3535
true,
3636
$this->disableUnreachable,
3737
$this->reportAlwaysTrueInLastCondition,
38+
$this->treatPhpDocTypesAsCertain,
3839
);
3940
}
4041

@@ -441,4 +442,14 @@ public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLast
441442
$this->analyse([__DIR__ . '/data/match-always-true-last-arm.php'], $expectedErrors);
442443
}
443444

445+
public function testBug8932(): void
446+
{
447+
if (PHP_VERSION_ID < 80000) {
448+
$this->markTestSkipped('Test requires PHP 8.0.');
449+
}
450+
451+
$this->treatPhpDocTypesAsCertain = false;
452+
$this->analyse([__DIR__ . '/data/bug-8932.php'], []);
453+
}
454+
444455
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug8932;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param 'A'|'B' $string
9+
*/
10+
public function sayHello(string $string): int
11+
{
12+
return match ($string) {
13+
'A' => 1,
14+
'B' => 2,
15+
default => throw new \LogicException(),
16+
};
17+
}
18+
}

0 commit comments

Comments
 (0)