Skip to content

Commit

Permalink
StrictComparisonOfDifferentTypesRule - tip for always true comparison…
Browse files Browse the repository at this point in the history
… between enums
  • Loading branch information
ondrejmirtes committed Apr 3, 2023
1 parent e06c529 commit a327965
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public function processNode(Node $node, Scope $scope): array
$rightType->describe(VerbosityLevel::value()),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
$errorBuilder->addTip('Remove remaining cases below this one and this error will disappear too.');
}

if ($leftType->isEnum()->yes() && $rightType->isEnum()->yes()) {
$errorBuilder->addTip('Use match expression instead. PHPStan will report unhandled enum cases.');
}

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public function testBug8485(): void
[
'Strict comparison using === between Bug8485\E::c and Bug8485\E::c will always evaluate to true.',
19,
'Use match expression instead. PHPStan will report unhandled enum cases.',
],
[
'Strict comparison using === between Bug8485\F::c and Bug8485\E::c will always evaluate to false.',
Expand All @@ -657,12 +658,12 @@ public function testBug8485(): void
[
'Strict comparison using === between Bug8485\FooEnum::C and Bug8485\FooEnum::C will always evaluate to true.',
67,
'Remove remaining cases below this one and this error will disappear too.',
"Remove remaining cases below this one and this error will disappear too.\n• Use match expression instead. PHPStan will report unhandled enum cases.",
],
[
'Strict comparison using === between Bug8485\FooEnum::C and Bug8485\FooEnum::C will always evaluate to true.',
74,
'Remove remaining cases below this one and this error will disappear too.',
"Remove remaining cases below this one and this error will disappear too.\n• Use match expression instead. PHPStan will report unhandled enum cases.",
],
]);
}
Expand Down Expand Up @@ -936,4 +937,25 @@ public function testBug9104(): void
]);
}

public function testEnumTips(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/strict-comparison-enum-tips.php'], [
[
'Strict comparison using === between $this(StrictComparisonEnumTips\SomeEnum)&StrictComparisonEnumTips\SomeEnum::Two and StrictComparisonEnumTips\SomeEnum::Two will always evaluate to true.',
15,
"• Remove remaining cases below this one and this error will disappear too.\n• Use match expression instead. PHPStan will report unhandled enum cases.",
],
[
'Strict comparison using === between $this(StrictComparisonEnumTips\SomeEnum)&StrictComparisonEnumTips\SomeEnum::Two and StrictComparisonEnumTips\SomeEnum::Two will always evaluate to true.',
29,
'Use match expression instead. PHPStan will report unhandled enum cases.',
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php // lint >= 8.1

namespace StrictComparisonEnumTips;

enum SomeEnum
{

case One;
case Two;

public function exhaustiveWithSafetyCheck(): int
{
if ($this === self::One) {
return -1;
} elseif ($this === self::Two) {
return 0;
} else {
throw new \LogicException('New case added, handling missing');
}
}


public function exhaustiveWithSafetyCheck2(): int
{
if ($this === self::One) {
return -1;
}

if ($this === self::Two) {
return 0;
}

throw new \LogicException('New case added, handling missing');
}

}

0 comments on commit a327965

Please sign in to comment.