Skip to content

Commit

Permalink
handle empty expr
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed May 13, 2023
1 parent 7edd4dd commit a27b32e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
24 changes: 18 additions & 6 deletions src/Rules/Classes/EnumSanityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,25 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if ($stmt->expr === null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Enum case %s::%s without type doesn\'t match the "int" type.',
$node->namespacedName->toString(),
$caseName,
))
->identifier('enum.missingCase')
->line($stmt->getLine())
->nonIgnorable()
->build();
continue;
}

if ($node->scalarType->name === 'int' && !($stmt->expr instanceof Node\Scalar\LNumber)) {
$errors[] = RuleErrorBuilder::message(sprintf(
"Enum case %s::%s type %s doesn't match the 'int' type",
'Enum case %s::%s type %s doesn\'t match the "int" type.',
$node->namespacedName->toString(),
$scope->getType($stmt->expr)->describe(VerbosityLevel::typeOnly()),
$caseName,
$scope->getType($stmt->expr)->describe(VerbosityLevel::typeOnly()),
))
->identifier('enum.caseType')
->line($stmt->getLine())
Expand All @@ -177,10 +190,10 @@ public function processNode(Node $node, Scope $scope): array
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
"Enum case %s::%s type %sdoesn't match the 'string' type",
'Enum case %s::%s type %s doesn\'t match the "string" type.',
$node->namespacedName->toString(),
$scope->getType($stmt->expr)->describe(VerbosityLevel::typeOnly()),
$caseName,
$scope->getType($stmt->expr)->describe(VerbosityLevel::typeOnly()),
))
->identifier('enum.caseType')
->line($stmt->getLine())
Expand All @@ -194,10 +207,9 @@ public function processNode(Node $node, Scope $scope): array
}

$errors[] = RuleErrorBuilder::message(sprintf(
'Enum %s has duplicate value %s for %s %s.',
'Enum %s has duplicate value %s for cases %s.',
$node->namespacedName->toString(),
$caseValue,
count($caseNames) === 1 ? 'case' : 'cases',
implode(', ', $caseNames),
))
->identifier('enum.duplicateValue')
Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Rules/Classes/EnumSanityRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ public function testRule(): void
81,
],
[
'Enum EnumSanity\EnumDuplicateValue has duplicated value 1 for keys A, E',
'Enum EnumSanity\EnumDuplicateValue has duplicate value 1 for cases A, E.',
86,
],
[
'Enum EnumSanity\EnumDuplicateValue has duplicated value 2 for keys B, C',
'Enum EnumSanity\EnumDuplicateValue has duplicate value 2 for cases B, C.',
86,
],
[
"Enum case EnumSanity\EnumInconsistentCaseType::FOO doesn't match the 'int' type",
'Enum case EnumSanity\EnumInconsistentCaseType::FOO type string doesn\'t match the "int" type.',
105,
],
[
"Enum case EnumSanity\EnumInconsistentCaseType::BAR doesn't match the 'int' type",
'Enum case EnumSanity\EnumInconsistentCaseType::BAR without type doesn\'t match the "int" type.',
106,
],
[
'Enum EnumSanity\EnumWithValueButNotBacked is not backed, but EnumSanity\EnumWithValueButNotBacked::FOO has value 1',
'Enum EnumSanity\EnumWithValueButNotBacked is not backed, but case FOO has value 1.',
110,
],
];
Expand Down

0 comments on commit a27b32e

Please sign in to comment.