Skip to content
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

Fix object properties lose type #11156

Open
wants to merge 2 commits into
base: 5.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
4 changes: 4 additions & 0 deletions src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Psalm\Type\Atomic\TNever;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateParam;
use ReflectionProperty;
Expand Down Expand Up @@ -845,6 +846,9 @@ private static function getValueForKey(

if ($existing_key_type_part instanceof TNull) {
$class_property_type = Type::getNull();
} elseif ($existing_key_type_part instanceof TObjectWithProperties) {
$class_property_type =
$existing_key_type_part->properties[$property_name] ?? Type::getMixed();
} elseif ($existing_key_type_part instanceof TMixed
|| $existing_key_type_part instanceof TObject
|| ($existing_key_type_part instanceof TNamedObject
Expand Down
13 changes: 13 additions & 0 deletions tests/ReturnTypeProvider/GetObjectVarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ final class C {
'assertions' => ['$ret' => 'array{prop: string}'],
];

yield 'retrurnsNotMixed' => [
'code' => '<?php
/**
* @param object{g: bool} $o
*/
function f(object $o, bool $b): bool {
if ($o->g && $b) {
return $o->g;
}
return true;
}',
];

yield 'returnsSealedArrayForFinalClass' => [
'code' => '<?php
final class C {
Expand Down
Loading