Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect incomplete match error for treatPhpDocTypesAsCertain: false #1560

Merged
merged 1 commit into from
Jul 26, 2022
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
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); // lint >= 8.0

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,
};
}
}