From a30dec0f94c33bd3c69d0a4a30d043482c9c00ba Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: 2025年9月11日 09:17:36 +0200 Subject: [PATCH] MatchExpressionRule - ignore reportAlwaysTrueInLastCondition --- src/Rules/Comparison/MatchExpressionRule.php | 22 +++++----- .../Comparison/MatchExpressionRuleTest.php | 40 +++---------------- .../Rules/Comparison/data/match-expr.php | 29 ++++++++++++++ 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/Rules/Comparison/MatchExpressionRule.php b/src/Rules/Comparison/MatchExpressionRule.php index e18180e08a..8c31b92242 100644 --- a/src/Rules/Comparison/MatchExpressionRule.php +++ b/src/Rules/Comparison/MatchExpressionRule.php @@ -31,8 +31,6 @@ final class MatchExpressionRule implements Rule public function __construct( private ConstantConditionRuleHelper $constantConditionRuleHelper, #[AutowiredParameter] - private bool $reportAlwaysTrueInLastCondition, - #[AutowiredParameter] private bool $treatPhpDocTypesAsCertain, ) { @@ -105,22 +103,20 @@ public function processNode(Node $node, Scope $scope): array continue; } - $reportAlwaysTrueInLastCondition = $this->reportAlwaysTrueInLastCondition && $matchConditionType->getEnumCases() === []; - if ($i === $armsCount - 1 && !$reportAlwaysTrueInLastCondition) { + if ($i === $armsCount - 1) { continue; } - $errorBuilder = RuleErrorBuilder::message(sprintf( + + $message = sprintf( 'Match arm comparison between %s and %s is always true.', $armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()), $armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()), - ))->line($armLine); - if ($i !== $armsCount - 1 && !$reportAlwaysTrueInLastCondition) { - $errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.'); - } - - $errorBuilder->identifier('match.alwaysTrue'); - - $errors[] = $errorBuilder->build(); + ); + $errors[] = RuleErrorBuilder::message($message) + ->line($armLine) + ->identifier('match.alwaysTrue') + ->tip('Remove remaining cases below this one and this error will disappear too.') + ->build(); } } diff --git a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php index 023f1091b2..014b57899c 100644 --- a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php @@ -4,7 +4,6 @@ use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; -use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\RequiresPhp; /** @@ -15,8 +14,6 @@ class MatchExpressionRuleTest extends RuleTestCase private bool $treatPhpDocTypesAsCertain = true; - private bool $reportAlwaysTrueInLastCondition = false; - protected function getRule(): Rule { return new MatchExpressionRule( @@ -29,7 +26,6 @@ protected function getRule(): Rule ), $this->treatPhpDocTypesAsCertain, ), - $this->reportAlwaysTrueInLastCondition, $this->treatPhpDocTypesAsCertain, ); } @@ -280,21 +276,11 @@ public function testLastArmAlwaysTrue(): void ]); } - public static function dataReportAlwaysTrueInLastCondition(): iterable + #[RequiresPhp('>= 8.1')] + public function testLastCondition(): void { - yield [false, [ - [ - 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', - 23, - 'Remove remaining cases below this one and this error will disappear too.', - ], - [ - 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', - 49, - 'Remove remaining cases below this one and this error will disappear too.', - ], - ]]; - yield [true, [ + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/match-always-true-last-arm.php'], [ [ 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', 23, @@ -305,23 +291,7 @@ public static function dataReportAlwaysTrueInLastCondition(): iterable 49, 'Remove remaining cases below this one and this error will disappear too.', ], - [ - 'Match arm comparison between false and false is always true.', - 58, - ], - ]]; - } - - /** - * @param list $expectedErrors - */ - #[RequiresPhp('>= 8.1')] - #[DataProvider('dataReportAlwaysTrueInLastCondition')] - public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLastCondition, array $expectedErrors): void - { - $this->treatPhpDocTypesAsCertain = true; - $this->reportAlwaysTrueInLastCondition = $reportAlwaysTrueInLastCondition; - $this->analyse([__DIR__ . '/data/match-always-true-last-arm.php'], $expectedErrors); + ]); } #[RequiresPhp('>= 8.0')] diff --git a/tests/PHPStan/Rules/Comparison/data/match-expr.php b/tests/PHPStan/Rules/Comparison/data/match-expr.php index 5970804bb4..baeec9922d 100644 --- a/tests/PHPStan/Rules/Comparison/data/match-expr.php +++ b/tests/PHPStan/Rules/Comparison/data/match-expr.php @@ -215,3 +215,32 @@ public function doMatch(FinalFoo|FinalBar $class): void } } + +class LastArm +{ + public const TYPE_A = 1; + public const TYPE_B = 2; + + + /** + * @param self::TYPE_* $type + */ + public function doMatch(int $type): void + { + match ($type) { + self::TYPE_A => 'A', + self::TYPE_B => 'B', + }; + + $day = date('N'); + match ($day) { + '1' => 'Mon', + '2' => 'Tue', + '3' => 'Wed', + '4' => 'Thu', + '5' => 'Fri', + '6' => 'Sat', + '7' => 'Sun', + }; + } +}

AltStyle によって変換されたページ (->オリジナル) /