mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Merge pull request #8913 from weirdan/cast-object-with-properties-to-array
Recognize casts from object-with-properties to array
This commit is contained in:
commit
5935012642
@ -259,6 +259,15 @@ class CastAnalyzer
|
||||
|| $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;
|
||||
|
@ -40,5 +40,14 @@ class CastTest extends TestCase
|
||||
'$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>}',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user