Skip to content

Commit fa843bf

Browse files
Fix object cast to array
1 parent d81cb77 commit fa843bf

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/Type/ObjectType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PHPStan\TrinaryLogic;
3737
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
3838
use PHPStan\Type\Accessory\AccessoryNumericStringType;
39+
use PHPStan\Type\Accessory\HasOffsetValueType;
3940
use PHPStan\Type\Constant\ConstantArrayType;
4041
use PHPStan\Type\Constant\ConstantBooleanType;
4142
use 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) {
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);

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)