File tree Expand file tree Collapse file tree 2 files changed +30
-4
lines changed
tests/PHPStan/Rules/Variables/data Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -652,23 +652,33 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
652
652
653
653
public function getOffsetValueType (Type $ offsetType ): Type
654
654
{
655
+ if (count ($ this ->keyTypes ) === 0 ) {
656
+ return new ErrorType ();
657
+ }
658
+
655
659
$ offsetType = $ offsetType ->toArrayKey ();
656
660
$ matchingValueTypes = [];
657
661
$ all = true ;
662
+ $ maybeAll = true ;
658
663
foreach ($ this ->keyTypes as $ i => $ keyType ) {
659
664
if ($ keyType ->isSuperTypeOf ($ offsetType )->no ()) {
660
665
$ all = false ;
666
+
667
+ if (
668
+ $ keyType instanceof ConstantIntegerType
669
+ && !$ offsetType ->isString ()->no ()
670
+ && $ offsetType ->isConstantScalarValue ()->no ()
671
+ ) {
672
+ continue ;
673
+ }
674
+ $ maybeAll = false ;
661
675
continue ;
662
676
}
663
677
664
678
$ matchingValueTypes [] = $ this ->valueTypes [$ i ];
665
679
}
666
680
667
681
if ($ all ) {
668
- if (count ($ this ->keyTypes ) === 0 ) {
669
- return new ErrorType ();
670
- }
671
-
672
682
return $ this ->getIterableValueType ();
673
683
}
674
684
@@ -681,6 +691,10 @@ public function getOffsetValueType(Type $offsetType): Type
681
691
return $ type ;
682
692
}
683
693
694
+ if ($ maybeAll ) {
695
+ return $ this ->getIterableValueType ();
696
+ }
697
+
684
698
return new ErrorType (); // undefined offset
685
699
}
686
700
Original file line number Diff line number Diff line change @@ -20,10 +20,22 @@ public function validate(string $value): void
20
20
throw new \RuntimeException ();
21
21
}
22
22
23
+ assertType ("non-empty-string " , $ value );
24
+ assertType ("'Test1'|'Test2' " , self ::MAP [$ value ]);
25
+
23
26
$ value = self ::MAP [$ value ] ?? $ value ;
24
27
28
+ assertType ("non-empty-string " , $ value );
25
29
assertType ("'Test1'|'Test2' " , self ::MAP [$ value ]);
26
30
27
31
// ...
28
32
}
33
+
34
+ public function validateNumericString (string $ value ): void
35
+ {
36
+ if (!is_numeric ($ value )) return ;
37
+
38
+ assertType ("numeric-string " , $ value );
39
+ assertType ("'Test1'|'Test2' " , self ::MAP [$ value ]);
40
+ }
29
41
}
You can’t perform that action at this time.
0 commit comments