Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyInfo] Add support for generics  #45071

Open
@raphaelvoisin

Description

@raphaelvoisin

Description

Is there a plan to support generics (@template) in PropertyInfo ? I would love the Serializer to be able to deserialize data based on types extracted from generics analysis.

Note: There is this discussion in phpdocumentor about the missing template support

Example

Let's suppose we have such case :

class MyClass
{
    /**
     * @var VersionedValue<ValueObject>
     */
    private VersionedValue $versionedVO;

    public function __construct(VersionedValue $versionedVO)
    {
        $this->versionedVO = $versionedVO;
    }
}

/**
 * @template T
 */
class VersionedValue
{
    /**
     * @param T $value
     */
    public function __construct(
        private mixed $value,
        private int $version
    )
    {
    }
}

class ValueObject
{
    public function __construct(private string $someVoProp)
    {

    }
}

Here, VersionedValue act as a generic class to associate a value with a version number.
The symfony Serializer can serialize a MyClass object easily into something like (here in JSON) :

{
	"versionedVO": {
		"value": {
			"someVoProp": "foo"
		},
		"version": 1
	}
}

...but will be unable to deserialize such format (at least without extra help from a custom denormalizer), as neither PhpDocExtractor neither PhpStanExtractor understand that versionedVO.value is a ValueObject (to PhpDocExtractor, @var VersionedValue<ValueObject> means "an array of ValueObject", as if I would have typed @var anything<ValueObject>)

Am I missing something already implemented ? If not, is there a plan to introduce property extraction from @template analysis ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureHelp wantedIssues and PRs which are looking for volunteers to complete them.PropertyInfo

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions