Skip to content
Open
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lint:
--exclude tests/PHPStan/Rules/Names/data \
--exclude tests/PHPStan/Rules/Operators/data/invalid-inc-dec.php \
--exclude tests/PHPStan/Rules/Arrays/data/offset-access-without-dim-for-reading.php \
--exclude tests/PHPStan/Rules/Classes/data/bug-13768.php \
--exclude tests/PHPStan/Rules/Classes/data/duplicate-declarations.php \
--exclude tests/PHPStan/Rules/Classes/data/duplicate-enum-cases.php \
--exclude tests/PHPStan/Rules/Classes/data/enum-sanity.php \
Expand Down
48 changes: 27 additions & 21 deletions src/Rules/Classes/EnumSanityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -16,6 +17,8 @@
use function count;
use function implode;
use function in_array;
use function is_int;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -145,27 +148,16 @@ public function processNode(Node $node, Scope $scope): array
}
$caseName = $stmt->name->name;

if ($stmt->expr instanceof Node\Scalar\Int_ || $stmt->expr instanceof Node\Scalar\String_) {
if ($enumNode->scalarType === null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Enum %s is not backed, but case %s has value %s.',
$classReflection->getDisplayName(),
$caseName,
$stmt->expr->value,
))
->identifier('enum.caseWithValue')
->line($stmt->getStartLine())
->nonIgnorable()
->build();
} else {
$caseValue = $stmt->expr->value;

if (!isset($enumCases[$caseValue])) {
$enumCases[$caseValue] = [];
}

$enumCases[$caseValue][] = $caseName;
}
if ($enumNode->scalarType === null && $stmt->expr !== null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Enum %s is not backed, but case %s has value.',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message changed from

Enum %s is not backed, but case %s has value %s.

to

Enum %s is not backed, but case %s has value.

I'm not sure you're okay with this or if

  • it should be behind bleedingEdge
  • it should be behind some extra condition (and have two differents messages ?)

$classReflection->getDisplayName(),
$caseName,
))
->identifier('enum.caseWithValue')
->line($stmt->getStartLine())
->nonIgnorable()
->build();
}

if ($enumNode->scalarType === null) {
Expand All @@ -189,6 +181,20 @@ public function processNode(Node $node, Scope $scope): array
$exprType = $scope->getType($stmt->expr);
$scalarType = $enumNode->scalarType->toLowerString() === 'int' ? new IntegerType() : new StringType();
if ($scalarType->isSuperTypeOf($exprType)->yes()) {
$constantValues = $exprType->getConstantScalarValues();
if (count($constantValues) === 1) {
$caseValue = $constantValues[0];
if (!is_string($caseValue) && !is_int($caseValue)) {
throw new ShouldNotHappenException();
}

if (!isset($enumCases[$caseValue])) {
$enumCases[$caseValue] = [];
}

$enumCases[$caseValue][] = $caseName;
}

continue;
}

Expand Down
26 changes: 15 additions & 11 deletions tests/PHPStan/Rules/Classes/EnumSanityRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testRule(): void
110,
],
[
'Enum EnumSanity\EnumWithValueButNotBacked is not backed, but case FOO has value 1.',
'Enum EnumSanity\EnumWithValueButNotBacked is not backed, but case FOO has value.',
114,
],
[
Expand All @@ -119,24 +119,28 @@ public function testBug9402(): void
}

#[RequiresPhp('>= 8.1')]
public function testBug11592(): void
public function testBug13768(): void
{
$this->analyse([__DIR__ . '/data/bug-11592.php'], [
$this->analyse([__DIR__ . '/data/bug-13768.php'], [
[
'Enum Bug11592\Test2 cannot redeclare native method cases().',
22,
'Enum Bug13768\Order is not backed, but case A has value.',
7,
],
[
'Enum Bug11592\BackedTest2 cannot redeclare native method cases().',
37,
'Enum Bug13768\Order is not backed, but case B has value.',
8,
],
[
'Enum Bug11592\BackedTest2 cannot redeclare native method from().',
39,
'Enum Bug13768\Order is not backed, but case C has value.',
9,
],
[
'Enum Bug13768\Order is not backed, but case D has value.',
10,
],
[
'Enum Bug11592\BackedTest2 cannot redeclare native method tryFrom().',
41,
'Enum Bug13768\Backed has duplicate value 1 for cases One, Two.',
18,
],
]);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-13768.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php // lint >= 8.1

namespace Bug13768;

enum Order {
case U;
case A = 1.5;
case B = 2.5;
case C = 3;
case D = '3';
}

class Foo
{
public const A = 1;
}

enum Backed: int {
case One = Foo::A;
case Two = Foo::A;
}
Loading