Skip to content

Commit

Permalink
Merge pull request #8913 from weirdan/cast-object-with-properties-to-…
Browse files Browse the repository at this point in the history
…array

Recognize casts from object-with-properties to array
  • Loading branch information
orklah committed Dec 17, 2022
2 parents b57e5ba + 425d9fa commit 5935012
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Expand Up @@ -259,6 +259,15 @@ public static function analyze(
|| $type instanceof TKeyedArray
) {
$permissible_atomic_types[] = $type;
} elseif ($type instanceof TObjectWithProperties) {
$array_type = $type->properties === []
? new TArray([Type::getArrayKey(), Type::getMixed()])
: new TKeyedArray(
$type->properties,
null,
[Type::getArrayKey(), Type::getMixed()]
);
$permissible_atomic_types[] = $array_type;
} else {
$all_permissible = false;
break;
Expand Down
9 changes: 9 additions & 0 deletions tests/CastTest.php
Expand Up @@ -40,5 +40,14 @@ public function providerValidCodeParse(): iterable
'$int===' => '0|1|int<10, 20>',
],
];
yield 'castObjectWithPropertiesToArray' => [
'code' => '<?php
/** @var object{a:int,b:string} $o */
$a = (array) $o;
',
'assertions' => [
'$a===' => 'array{a: int, b: string, ...<array-key, mixed>}',
],
];
}
}

0 comments on commit 5935012

Please sign in to comment.