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

Enable resolving collection type from the var doblock #1208

Closed
dgafka opened this issue May 28, 2020 · 2 comments
Closed

Enable resolving collection type from the var doblock #1208

dgafka opened this issue May 28, 2020 · 2 comments

Comments

@dgafka
Copy link
Contributor

dgafka commented May 28, 2020

Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? yes

Currently JMS Serializer can resolve type declaration from properties (PHP 7.4), yet can not resolve the type, when type is collection (array of specific classes).

The type however is available for us in most of the time in form of the docblock @var.

Steps required to reproduce the problem

Right now, in order to describe the collection we need to do it like this:

	/**
	 * @JMS\Type("array<Application\Message>")
	 * @var Message[]
	 */
	private array $messages;

Unfortunately this solution has disadvantages.

  1. We duplicate the type in the form of the @var and as well @JMS\Type
  2. IDE refactoring tools are not able to find and replace, when the class name is changed
  3. We can resolve types from property declaration without any additional configuration, however not when it's collection.

In PHP we are having convention to describe collections as @var docblock and mark it with suffix [].
This is respected by IDE, works with refactoring tools and is widely used in the community in order to describe the collection.
What I would propose is to fallback to the docblock, when declared type is array.

Previous code would looks like this:

	/**
	 * @var Message[]
	 */
	private array $messages;

Expected Result

Transforming Message[] into the form respected by JMS array<Application\Message>

Actual Result

Require annotation in order to work


I have built such feature on top of JMS Serializer in integration with my framework.
However I think such feature could be really nice addition build into JMS Serializer directly.

I am willing to prepare implementation for that, if you will find it useful. :)

@goetas
Copy link
Collaborator

goetas commented May 31, 2020

That sound a good idea. I'm curious on how is implemented. Is it something similar to what i did to detect php 7.4 types in #1192 ?

@goetas
Copy link
Collaborator

goetas commented May 31, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants