symfony/symfony

[PropertyInfo] Add support for generics

开放

#45,071 创建于 2022年1月19日

 (14 条评论) (5 个反应) (0 位负责人)PHP (9,819 个派生)batch import
FeatureHelp wantedPropertyInfo

仓库指标

星标
 (31,047 个星标)
PR 合并指标
 (平均合并 17天 3小时) (30 天内合并 157 个 PR)

描述

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 ?

贡献者指南