Skip to content

Commit 2c62a53

Browse files
Rework
1 parent 37b0e41 commit 2c62a53

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Rules/Classes/EnumSanityRule.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Node\InClassNode;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleErrorBuilder;
11+
use PHPStan\ShouldNotHappenException;
1112
use PHPStan\Type\IntegerType;
1213
use PHPStan\Type\StringType;
1314
use PHPStan\Type\VerbosityLevel;
@@ -16,6 +17,8 @@
1617
use function count;
1718
use function implode;
1819
use function in_array;
20+
use function is_int;
21+
use function is_string;
1922
use function sprintf;
2023

2124
/**
@@ -176,18 +179,22 @@ public function processNode(Node $node, Scope $scope): array
176179
}
177180

178181
$exprType = $scope->getType($stmt->expr);
179-
$constantValues = $exprType->getConstantScalarValues();
180-
if (count($constantValues) === 1) {
181-
$caseValue = $constantValues[0];
182-
if (!isset($enumCases[$caseValue])) {
183-
$enumCases[$caseValue] = [];
184-
}
185-
186-
$enumCases[$caseValue][] = $caseName;
187-
}
188-
189182
$scalarType = $enumNode->scalarType->toLowerString() === 'int' ? new IntegerType() : new StringType();
190183
if ($scalarType->isSuperTypeOf($exprType)->yes()) {
184+
$constantValues = $exprType->getConstantScalarValues();
185+
if (count($constantValues) === 1) {
186+
$caseValue = $constantValues[0];
187+
if (!is_string($caseValue) && !is_int($caseValue)) {
188+
throw new ShouldNotHappenException();
189+
}
190+
191+
if (!isset($enumCases[$caseValue])) {
192+
$enumCases[$caseValue] = [];
193+
}
194+
195+
$enumCases[$caseValue][] = $caseName;
196+
}
197+
191198
continue;
192199
}
193200

0 commit comments

Comments
 (0)