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
22 changes: 9 additions & 13 deletions src/Rules/Comparison/MatchExpressionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ final class MatchExpressionRule implements Rule
public function __construct(
private ConstantConditionRuleHelper $constantConditionRuleHelper,
#[AutowiredParameter]
private bool $reportAlwaysTrueInLastCondition,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
)
{
Expand Down Expand Up @@ -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();
}
}

Expand Down
40 changes: 5 additions & 35 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhp;

/**
Expand All @@ -15,8 +14,6 @@ class MatchExpressionRuleTest extends RuleTestCase

private bool $treatPhpDocTypesAsCertain = true;

private bool $reportAlwaysTrueInLastCondition = false;

protected function getRule(): Rule
{
return new MatchExpressionRule(
Expand All @@ -29,7 +26,6 @@ protected function getRule(): Rule
),
$this->treatPhpDocTypesAsCertain,
),
$this->reportAlwaysTrueInLastCondition,
$this->treatPhpDocTypesAsCertain,
);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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<array{0: string, 1: int, 2?: string}> $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')]
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/match-expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
}
}
Loading