1
0
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:
orklah 2022-12-17 09:28:17 +01:00 committed by GitHub
commit 5935012642
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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;

View File

@ -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>}',
],
];
}
}