Skip to content

[TypeDeclaration] Infer object return type from serializer method #8650

@TomasVotruba

Description

@TomasVotruba

The serializer of Symfony\Component\Serializer\Serializer type that unserialize to an object have only 1 exact return type.

That means we can add specific type on deserialize() call, based on 2nd argument:

-public function resolveEntity($data)
+public function resolveEntity($data): SomeType
{
    return $this->serializer->deserialize($data, SomeType::class, 'json');
}

If the deserialize() fails, it will throw an exception. So the value is never nullable.

The rule should handle only direct returns like these, keep it simple.

This rule should be part of type declaration set.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions