-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PHPStan rule to detect duplicate enum values (#343)
- Loading branch information
Showing
8 changed files
with
120 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
services: | ||
- class: \BenSampo\Enum\PHPStan\EnumMethodsClassReflectionExtension | ||
- class: BenSampo\Enum\PHPStan\EnumMethodsClassReflectionExtension | ||
tags: | ||
- phpstan.broker.methodsClassReflectionExtension | ||
- class: BenSampo\Enum\PHPStan\UniqueValuesRule | ||
tags: | ||
- phpstan.rules.rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace BenSampo\Enum\PHPStan; | ||
|
||
use BenSampo\Enum\Enum; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** @implements Rule<InClassNode> */ | ||
final class UniqueValuesRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof InClassNode); | ||
|
||
$reflection = $node->getClassReflection(); | ||
if (! $reflection->isSubclassOf(Enum::class)) { | ||
return []; | ||
} | ||
|
||
$constants = []; | ||
foreach ($reflection->getNativeReflection()->getReflectionConstants() as $constant) { | ||
$constants[$constant->name] = $constant->getValue(); | ||
} | ||
|
||
$duplicateConstants = []; | ||
foreach ($constants as $name => $value) { | ||
$constantsWithValue = array_filter($constants, fn (mixed $v): bool => $v === $value); | ||
if (count($constantsWithValue) > 1) { | ||
$duplicateConstants []= array_keys($constantsWithValue); | ||
} | ||
} | ||
$duplicateConstants = array_unique($duplicateConstants); | ||
|
||
if (count($duplicateConstants) > 0) { | ||
$fqcn = $reflection->getName(); | ||
$constantsString = json_encode($duplicateConstants); | ||
|
||
return [ | ||
RuleErrorBuilder::message("Enum class {$fqcn} contains constants with duplicate values: {$constantsString}.") | ||
->build(), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace BenSampo\Enum\Tests\PHPStan\Fixtures; | ||
|
||
use BenSampo\Enum\Enum; | ||
|
||
/** | ||
* @extends Enum<string> | ||
* | ||
* @method static static A() | ||
* @method static static B() | ||
*/ | ||
final class DuplicateValue extends Enum | ||
{ | ||
public const A = 'A'; | ||
public const B = 'A'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace BenSampo\Enum\Tests\PHPStan; | ||
|
||
use BenSampo\Enum\PHPStan\UniqueValuesRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** @extends RuleTestCase<UniqueValuesRule> */ | ||
final class UniqueValuesRuleTest extends RuleTestCase | ||
{ | ||
protected function getRule(): Rule | ||
{ | ||
return new UniqueValuesRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse( | ||
[ | ||
__DIR__ . '/Fixtures/DuplicateValue.php', | ||
], | ||
[ | ||
[ | ||
'Enum class BenSampo\Enum\Tests\PHPStan\Fixtures\DuplicateValue contains constants with duplicate values: [["A","B"]].', | ||
13, | ||
], | ||
], | ||
); | ||
} | ||
} |