Skip to content

Fix non final object cast to array #4167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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]);
}

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