File tree Expand file tree Collapse file tree 3 files changed +73
-2
lines changed
tests/PHPStan/Rules/Comparison Expand file tree Collapse file tree 3 files changed +73
-2
lines changed Original file line number Diff line number Diff line change 36
36
use PHPStan \TrinaryLogic ;
37
37
use PHPStan \Type \Accessory \AccessoryNonEmptyStringType ;
38
38
use PHPStan \Type \Accessory \AccessoryNumericStringType ;
39
+ use PHPStan \Type \Accessory \HasOffsetValueType ;
39
40
use PHPStan \Type \Constant \ConstantArrayType ;
40
41
use PHPStan \Type \Constant \ConstantBooleanType ;
41
42
use PHPStan \Type \Constant \ConstantStringType ;
@@ -693,8 +694,17 @@ public function toArray(): Type
693
694
$ classReflection = $ classReflection ->getParentClass ();
694
695
} while ($ classReflection !== null );
695
696
696
- if (!$ isFinal && count ($ arrayKeys ) === 0 ) {
697
- return new ArrayType (new MixedType (), new MixedType ());
697
+ if (!$ isFinal ) {
698
+ if (count ($ arrayKeys ) === 0 ) {
699
+ return new ArrayType (new MixedType (), new MixedType ());
700
+ }
701
+
702
+ $ types = [new ArrayType (new MixedType (), new MixedType ())];
703
+ foreach ($ arrayKeys as $ i => $ arrayKey ) {
704
+ $ types [] = new HasOffsetValueType ($ arrayKey , $ arrayValues [$ i ]);
705
+ }
706
+
707
+ return new IntersectionType ($ types );
698
708
}
699
709
700
710
return new ConstantArrayType ($ arrayKeys , $ arrayValues );
Original file line number Diff line number Diff line change @@ -1025,6 +1025,17 @@ public function testBug12412(): void
1025
1025
$ this ->analyse ([__DIR__ . '/data/bug-12412.php ' ], []);
1026
1026
}
1027
1027
1028
+ public function testBug2730 (): void
1029
+ {
1030
+ $ this ->treatPhpDocTypesAsCertain = true ;
1031
+ $ this ->analyse ([__DIR__ . '/data/bug-2730.php ' ], [
1032
+ [
1033
+ 'Call to function is_object() with int will always evaluate to false. ' ,
1034
+ 43 ,
1035
+ ],
1036
+ ]);
1037
+ }
1038
+
1028
1039
#[RequiresPhp('>= 8.2 ' )]
1029
1040
public function testBug13291 (): void
1030
1041
{
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Bug2730 ;
4
+
5
+ class A{
6
+ /** @var string */
7
+ public $ a = "hi " ;
8
+ /** @var int */
9
+ public $ b = 0 ;
10
+
11
+ public function dummy () : void {
12
+ foreach ((array ) $ this as $ k => $ v ){
13
+ if (is_string ($ v )){
14
+ echo "string \n" ;
15
+ }elseif (is_object ($ v )){
16
+ echo "object \n" ;
17
+ }else {
18
+ echo gettype ($ v ) . "\n" ;
19
+ }
20
+ }
21
+ }
22
+ }
23
+
24
+ class B extends A{
25
+ /** @var \stdClass */
26
+ public $ obj ;
27
+
28
+ public function __construct (){
29
+ $ this ->obj = new \stdClass ;
30
+ }
31
+ }
32
+
33
+ final class C{
34
+ /** @var string */
35
+ public $ a = "hi " ;
36
+ /** @var int */
37
+ public $ b = 0 ;
38
+
39
+ public function dummy () : void {
40
+ foreach ((array ) $ this as $ k => $ v ){
41
+ if (is_string ($ v )){
42
+ echo "string \n" ;
43
+ }elseif (is_object ($ v )){
44
+ echo "object \n" ;
45
+ }else {
46
+ echo gettype ($ v ) . "\n" ;
47
+ }
48
+ }
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments