symfony/symfony

[PropertyInfo] Add support for generics

Aperta

#45.071 aperta il 19 gen 2022

 (14 commenti) (5 reazioni) (0 assegnatari)PHP (9819 fork)batch import
FeatureHelp wantedPropertyInfo

Metriche repository

Star
 (31.047 stelle)
Metriche merge PR
 (Merge medio 17g 3h) (157 PR mergiate in 30 g)

Descrizione

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 ?

Guida contributor