File tree Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change 88use PHPStan \Node \InClassNode ;
99use PHPStan \Rules \Rule ;
1010use PHPStan \Rules \RuleErrorBuilder ;
11+ use PHPStan \ShouldNotHappenException ;
1112use PHPStan \Type \IntegerType ;
1213use PHPStan \Type \StringType ;
1314use PHPStan \Type \VerbosityLevel ;
1617use function count ;
1718use function implode ;
1819use function in_array ;
20+ use function is_int ;
21+ use function is_string ;
1922use 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
You can’t perform that action at this time.
0 commit comments