Skip to content

Commit

Permalink
Fix incorrect incomplete match error for `treatPhpDocTypesAsCertain: …
Browse files Browse the repository at this point in the history
…false`
  • Loading branch information
Richard van Velzen committed Jul 26, 2022
1 parent 1eec05f commit 998671e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ public function specifyTypesInCondition(
$exprRightType = $scope->getType($expr->right);
if (
($exprLeftType instanceof ConstantType && !$exprRightType->equals($exprLeftType) && $exprRightType->isSuperTypeOf($exprLeftType)->yes())
|| $exprLeftType instanceof ConstantScalarType
|| $exprLeftType instanceof EnumCaseObjectType
) {
$types = $this->create(
Expand All @@ -319,6 +320,7 @@ public function specifyTypesInCondition(
}
if (
($exprRightType instanceof ConstantType && !$exprLeftType->equals($exprRightType) && $exprLeftType->isSuperTypeOf($exprRightType)->yes())
|| $exprRightType instanceof ConstantScalarType
|| $exprRightType instanceof EnumCaseObjectType
) {
$leftType = $this->create(
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
class MatchExpressionRuleTest extends RuleTestCase
{

private bool $treatPhpDocTypesAsCertain = true;

protected function getRule(): Rule
{
return new MatchExpressionRule(true);
}

protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return $this->treatPhpDocTypesAsCertain;
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/match-expr.php'], [
Expand Down Expand Up @@ -208,4 +215,13 @@ public function testBug6647(): void
$this->analyse([__DIR__ . '/data/bug-6647.php'], []);
}

public function testBug7622(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/bug-7622.php'], []);
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-7622.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug7622;

final class AnalyticsKpiType
{
public const SESSION_COUNT = 'session_count';
public const MISSION_COUNT = 'mission_count';
public const SESSION_GAP = 'session_gap';
}

class HelloWorld
{

/**
* @param AnalyticsKpiType::* $currentKpi
* @param int[] $filteredMemberIds
*/
public function test(string $currentKpi, array $filteredMemberIds): int
{
return match ($currentKpi) {
AnalyticsKpiType::SESSION_COUNT => 12,
AnalyticsKpiType::MISSION_COUNT => 5,
AnalyticsKpiType::SESSION_GAP => 14,
};
}
}

0 comments on commit 998671e

Please sign in to comment.