Skip to content

Commit

Permalink
Merge pull request #7401 from orklah/getObjectVarsNoValue
Browse files Browse the repository at this point in the history
Psalm can't be sure get_object_vars will return an empty array unless object is known AND final
  • Loading branch information
orklah committed Jan 15, 2022
2 parents 32c8e63 + 3260b1a commit 3c726e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Expand Up @@ -43,7 +43,7 @@ public static function getGetObjectVarsReturnType(

if ($object_type instanceof TObjectWithProperties) {
if ([] === $object_type->properties) {
return Type::getEmptyArray();
return Type::parseString('array<string, mixed>');
}
return new Union([
new TKeyedArray($object_type->properties)
Expand All @@ -62,7 +62,11 @@ public static function getGetObjectVarsReturnType(
}

if ([] === $class_storage->appearing_property_ids) {
return Type::getEmptyArray();
if ($class_storage->final) {
return Type::getEmptyArray();
}

return Type::parseString('array<string, mixed>');
}

$properties = [];
Expand All @@ -86,7 +90,11 @@ public static function getGetObjectVarsReturnType(
}

if ([] === $properties) {
return Type::getEmptyArray();
if ($class_storage->final) {
return Type::getEmptyArray();
}

return Type::parseString('array<string, mixed>');
}

return new Union([
Expand Down
2 changes: 1 addition & 1 deletion tests/ReturnTypeProvider/GetObjectVarsTest.php
Expand Up @@ -24,7 +24,7 @@ class C {

yield 'omitsPrivateAndProtectedPropertiesWhenCalledOutsideOfClassScope' => [
'<?php
class C {
final class C {
/** @var string */
private $priv = "val";
Expand Down

0 comments on commit 3c726e7

Please sign in to comment.