File tree 3 files changed +71
-0
lines changed
tests/PHPStan/Rules/Arrays
3 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 28
28
use PHPStan \Type \Accessory \AccessoryNumericStringType ;
29
29
use PHPStan \Type \Accessory \AccessoryType ;
30
30
use PHPStan \Type \Accessory \AccessoryUppercaseStringType ;
31
+ use PHPStan \Type \Accessory \HasOffsetType ;
32
+ use PHPStan \Type \Accessory \HasOffsetValueType ;
31
33
use PHPStan \Type \Accessory \NonEmptyArrayType ;
32
34
use PHPStan \Type \Constant \ConstantArrayType ;
33
35
use PHPStan \Type \Constant \ConstantIntegerType ;
45
47
use function count ;
46
48
use function implode ;
47
49
use function in_array ;
50
+ use function is_int ;
48
51
use function ksort ;
49
52
use function md5 ;
50
53
use function sprintf ;
@@ -738,6 +741,21 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
738
741
if ((new ConstantIntegerType (0 ))->isSuperTypeOf ($ arrayKeyOffsetType )->yes ()) {
739
742
return TrinaryLogic::createYes ();
740
743
}
744
+
745
+ foreach ($ this ->types as $ type ) {
746
+ if (!$ type instanceof HasOffsetValueType && !$ type instanceof HasOffsetType) {
747
+ continue ;
748
+ }
749
+
750
+ foreach ($ type ->getOffsetType ()->getConstantScalarValues () as $ constantScalarValue ) {
751
+ if (!is_int ($ constantScalarValue )) {
752
+ continue ;
753
+ }
754
+ if (IntegerRangeType::fromInterval (0 , $ constantScalarValue )->isSuperTypeOf ($ arrayKeyOffsetType )->yes ()) {
755
+ return TrinaryLogic::createYes ();
756
+ }
757
+ }
758
+ }
741
759
}
742
760
743
761
return $ this ->intersectResults (static fn (Type $ type ): TrinaryLogic => $ type ->hasOffsetValueType ($ offsetType ));
Original file line number Diff line number Diff line change @@ -900,6 +900,22 @@ public function testNarrowSuperglobals(): void
900
900
$ this ->analyse ([__DIR__ . '/data/narrow-superglobal.php ' ], []);
901
901
}
902
902
903
+ public function testBug12605 (): void
904
+ {
905
+ $ this ->reportPossiblyNonexistentGeneralArrayOffset = true ;
906
+
907
+ $ this ->analyse ([__DIR__ . '/data/bug-12605.php ' ], [
908
+ [
909
+ 'Offset 1 might not exist on list<int>. ' ,
910
+ 19 ,
911
+ ],
912
+ [
913
+ 'Offset 10 might not exist on non-empty-list<int>. ' ,
914
+ 26 ,
915
+ ],
916
+ ]);
917
+ }
918
+
903
919
public function testBug11602 (): void
904
920
{
905
921
$ this ->reportPossiblyNonexistentGeneralArrayOffset = true ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Bug12605 ;
4
+
5
+ /**
6
+ * @return list<int>
7
+ */
8
+ function test (): array
9
+ {
10
+ return [];
11
+ }
12
+
13
+ function doFoo (): void {
14
+ $ test = test ();
15
+
16
+ if (isset ($ test [3 ])) {
17
+ echo $ test [1 ];
18
+ }
19
+ echo $ test [1 ];
20
+ }
21
+
22
+ function doFooBar (): void {
23
+ $ test = test ();
24
+
25
+ if (isset ($ test [4 ])) {
26
+ echo $ test [10 ];
27
+ }
28
+ }
29
+
30
+ function doBaz (): void {
31
+ $ test = test ();
32
+
33
+ if (array_key_exists (5 , $ test ) && is_int ($ test [5 ])) {
34
+ echo $ test [3 ];
35
+ }
36
+ }
37
+
You can’t perform that action at this time.
0 commit comments