Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -693,8 +694,17 @@ public function toArray(): Type
$classReflection = $classReflection->getParentClass();
} while ($classReflection !== null);

if (!$isFinal && count($arrayKeys) === 0) {
return new ArrayType(new MixedType(), new MixedType());
if (!$isFinal) {
if (count($arrayKeys) === 0) {
return new ArrayType(new MixedType(), new MixedType());
}

$types = [new ArrayType(new MixedType(), new MixedType())];
foreach ($arrayKeys as $i => $arrayKey) {
$types[] = new HasOffsetValueType($arrayKey, $arrayValues[$i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have this limited to a few items, to prevent combinatorial explosion. 16 should be fine.

If there's more, just return plain array type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with ddfa734

}

return new IntersectionType($types);
}

return new ConstantArrayType($arrayKeys, $arrayValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,17 @@ public function testBug12412(): void
$this->analyse([__DIR__ . '/data/bug-12412.php'], []);
}

public function testBug2730(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-2730.php'], [
[
'Call to function is_object() with int will always evaluate to false.',
43,
],
]);
}

#[RequiresPhp('>= 8.2')]
public function testBug13291(): void
{
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-2730.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Bug2730;

class A{
/** @var string */
public $a = "hi";
/** @var int */
public $b = 0;

public function dummy() : void{
foreach((array) $this as $k => $v){
if(is_string($v)){
echo "string\n";
}elseif(is_object($v)){
echo "object\n";
}else{
echo gettype($v) . "\n";
}
}
}
}

class B extends A{
/** @var \stdClass */
public $obj;

public function __construct(){
$this->obj = new \stdClass;
}
}

final class C{
/** @var string */
public $a = "hi";
/** @var int */
public $b = 0;

public function dummy() : void{
foreach((array) $this as $k => $v){
if(is_string($v)){
echo "string\n";
}elseif(is_object($v)){
echo "object\n";
}else{
echo gettype($v) . "\n";
}
}
}
}
Loading