Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c6cabc

Browse files
vranaondrejmirtes
authored andcommitted
MatchExpressionRule - ignore reportAlwaysTrueInLastCondition
1 parent fede695 commit 9c6cabc

File tree

3 files changed

+43
-48
lines changed

3 files changed

+43
-48
lines changed

‎src/Rules/Comparison/MatchExpressionRule.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ final class MatchExpressionRule implements Rule
3131
public function __construct(
3232
private ConstantConditionRuleHelper $constantConditionRuleHelper,
3333
#[AutowiredParameter]
34-
private bool $reportAlwaysTrueInLastCondition,
35-
#[AutowiredParameter]
3634
private bool $treatPhpDocTypesAsCertain,
3735
)
3836
{
@@ -105,22 +103,20 @@ public function processNode(Node $node, Scope $scope): array
105103
continue;
106104
}
107105

108-
$reportAlwaysTrueInLastCondition = $this->reportAlwaysTrueInLastCondition && $matchConditionType->getEnumCases() === [];
109-
if ($i === $armsCount - 1 && !$reportAlwaysTrueInLastCondition) {
106+
if ($i === $armsCount - 1) {
110107
continue;
111108
}
112-
$errorBuilder = RuleErrorBuilder::message(sprintf(
109+
110+
$message = sprintf(
113111
'Match arm comparison between %s and %s is always true.',
114112
$armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()),
115113
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
116-
))->line($armLine);
117-
if ($i !== $armsCount - 1 && !$reportAlwaysTrueInLastCondition) {
118-
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
119-
}
120-
121-
$errorBuilder->identifier('match.alwaysTrue');
122-
123-
$errors[] = $errorBuilder->build();
114+
);
115+
$errors[] = RuleErrorBuilder::message($message)
116+
->line($armLine)
117+
->identifier('match.alwaysTrue')
118+
->tip('Remove remaining cases below this one and this error will disappear too.')
119+
->build();
124120
}
125121
}
126122

‎tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7-
use PHPUnit\Framework\Attributes\DataProvider;
87
use PHPUnit\Framework\Attributes\RequiresPhp;
98

109
/**
@@ -15,8 +14,6 @@ class MatchExpressionRuleTest extends RuleTestCase
1514

1615
private bool $treatPhpDocTypesAsCertain = true;
1716

18-
private bool $reportAlwaysTrueInLastCondition = false;
19-
2017
protected function getRule(): Rule
2118
{
2219
return new MatchExpressionRule(
@@ -29,7 +26,6 @@ protected function getRule(): Rule
2926
),
3027
$this->treatPhpDocTypesAsCertain,
3128
),
32-
$this->reportAlwaysTrueInLastCondition,
3329
$this->treatPhpDocTypesAsCertain,
3430
);
3531
}
@@ -280,21 +276,11 @@ public function testLastArmAlwaysTrue(): void
280276
]);
281277
}
282278

283-
public static function dataReportAlwaysTrueInLastCondition(): iterable
279+
#[RequiresPhp('>= 8.1')]
280+
public function testLastCondition(): void
284281
{
285-
yield [false, [
286-
[
287-
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
288-
23,
289-
'Remove remaining cases below this one and this error will disappear too.',
290-
],
291-
[
292-
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
293-
49,
294-
'Remove remaining cases below this one and this error will disappear too.',
295-
],
296-
]];
297-
yield [true, [
282+
$this->treatPhpDocTypesAsCertain = true;
283+
$this->analyse([__DIR__ . '/data/match-always-true-last-arm.php'], [
298284
[
299285
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
300286
23,
@@ -305,23 +291,7 @@ public static function dataReportAlwaysTrueInLastCondition(): iterable
305291
49,
306292
'Remove remaining cases below this one and this error will disappear too.',
307293
],
308-
[
309-
'Match arm comparison between false and false is always true.',
310-
58,
311-
],
312-
]];
313-
}
314-
315-
/**
316-
* @param list<array{0: string, 1: int, 2?: string}> $expectedErrors
317-
*/
318-
#[RequiresPhp('>= 8.1')]
319-
#[DataProvider('dataReportAlwaysTrueInLastCondition')]
320-
public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLastCondition, array $expectedErrors): void
321-
{
322-
$this->treatPhpDocTypesAsCertain = true;
323-
$this->reportAlwaysTrueInLastCondition = $reportAlwaysTrueInLastCondition;
324-
$this->analyse([__DIR__ . '/data/match-always-true-last-arm.php'], $expectedErrors);
294+
]);
325295
}
326296

327297
#[RequiresPhp('>= 8.0')]

‎tests/PHPStan/Rules/Comparison/data/match-expr.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,32 @@ public function doMatch(FinalFoo|FinalBar $class): void
215215
}
216216

217217
}
218+
219+
class LastArm
220+
{
221+
public const TYPE_A = 1;
222+
public const TYPE_B = 2;
223+
224+
225+
/**
226+
* @param self::TYPE_* $type
227+
*/
228+
public function doMatch(int $type): void
229+
{
230+
match ($type) {
231+
self::TYPE_A => 'A',
232+
self::TYPE_B => 'B',
233+
};
234+
235+
$day = date('N');
236+
match ($day) {
237+
'1' => 'Mon',
238+
'2' => 'Tue',
239+
'3' => 'Wed',
240+
'4' => 'Thu',
241+
'5' => 'Fri',
242+
'6' => 'Sat',
243+
'7' => 'Sun',
244+
};
245+
}
246+
}

0 commit comments

Comments
(0)

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