Skip to content

Commit

Permalink
Improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Nov 15, 2023
1 parent f65900f commit 93819a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/PHPStan/UniqueValuesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ public function processNode(Node $node, Scope $scope): array
$constants[$constant->name] = $constant->getValue();
}

if (count($constants) !== count(array_unique($constants))) {
$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($constants);
$constantsString = json_encode($duplicateConstants);

return [
RuleErrorBuilder::message("Enum class {$fqcn} contains constants with duplicate values: {$constantsString}.")
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/UniqueValuesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testRule(): void
],
[
[
'Enum class BenSampo\Enum\Tests\PHPStan\Fixtures\DuplicateValue contains constants with duplicate values: {"A":"A","B":"A"}.',
'Enum class BenSampo\Enum\Tests\PHPStan\Fixtures\DuplicateValue contains constants with duplicate values: [["A","B"]].',
13,
],
],
Expand Down

0 comments on commit 93819a9

Please sign in to comment.