|
6 | 6 | use PHPStan\Analyser\Scope; |
7 | 7 | use PHPStan\DependencyInjection\RegisteredRule; |
8 | 8 | use PHPStan\Node\InClassNode; |
| 9 | +use PHPStan\Reflection\InitializerExprContext; |
| 10 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
9 | 11 | use PHPStan\Rules\Rule; |
10 | 12 | use PHPStan\Rules\RuleErrorBuilder; |
11 | 13 | use PHPStan\ShouldNotHappenException; |
@@ -34,6 +36,12 @@ final class EnumSanityRule implements Rule |
34 | 36 | '__invoke' => true, |
35 | 37 | ]; |
36 | 38 |
|
| 39 | + public function __construct( |
| 40 | + private InitializerExprTypeResolver $initializerExprTypeResolver, |
| 41 | + ) |
| 42 | + { |
| 43 | + } |
| 44 | + |
37 | 45 | public function getNodeType(): string |
38 | 46 | { |
39 | 47 | return InClassNode::class; |
@@ -149,20 +157,14 @@ public function processNode(Node $node, Scope $scope): array |
149 | 157 | $caseName = $stmt->name->name; |
150 | 158 |
|
151 | 159 | if ($enumNode->scalarType === null && $stmt->expr !== null) { |
152 | | - $value = null; |
153 | | - if ( |
154 | | - $stmt->expr instanceof Node\Scalar\Int_ |
155 | | - || $stmt->expr instanceof Node\Scalar\String_ |
156 | | - || $stmt->expr instanceof Node\Scalar\Float_ |
157 | | - ) { |
158 | | - $value = $stmt->expr->value; |
159 | | - } |
160 | | - |
161 | 160 | $errors[] = RuleErrorBuilder::message(sprintf( |
162 | | - 'Enum %s is not backed, but case %s has value%s.', |
| 161 | + 'Enum %s is not backed, but case %s has value %s.', |
163 | 162 | $classReflection->getDisplayName(), |
164 | 163 | $caseName, |
165 | | - $value !== null ? ' ' . $value : '', |
| 164 | + $this->initializerExprTypeResolver->getType( |
| 165 | + $stmt->expr, |
| 166 | + InitializerExprContext::fromScope($scope) |
| 167 | + )->describe(VerbosityLevel::value()), |
166 | 168 | )) |
167 | 169 | ->identifier('enum.caseWithValue') |
168 | 170 | ->line($stmt->getStartLine()) |
|
0 commit comments