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 3636use PHPStan \TrinaryLogic ;
3737use PHPStan \Type \Accessory \AccessoryNonEmptyStringType ;
3838use PHPStan \Type \Accessory \AccessoryNumericStringType ;
39+ use PHPStan \Type \Accessory \HasOffsetValueType ;
3940use PHPStan \Type \Constant \ConstantArrayType ;
4041use PHPStan \Type \Constant \ConstantBooleanType ;
4142use PHPStan \Type \Constant \ConstantStringType ;
@@ -693,8 +694,17 @@ public function toArray(): Type
693694 $ classReflection = $ classReflection ->getParentClass ();
694695 } while ($ classReflection !== null );
695696
696- if (!$ isFinal && count ($ arrayKeys ) === 0 ) {
697- return new ArrayType (new MixedType (), new MixedType ());
697+ if (!$ isFinal ) {
698+ if (count ($ arrayKeys ) === 0 || count ($ arrayKeys ) > 16 ) {
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 );
698708 }
699709
700710 return new ConstantArrayType ($ arrayKeys , $ arrayValues );
Original file line number Diff line number Diff line change @@ -1025,6 +1025,17 @@ public function testBug12412(): void
10251025 $ this ->analyse ([__DIR__ . '/data/bug-12412.php ' ], []);
10261026 }
10271027
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+
10281039 #[RequiresPhp('>= 8.2 ' )]
10291040 public function testBug13291 (): void
10301041 {
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