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

Skip to content

[PropertyInfo] Array of union types is not extracted correctly #50891

Closed
@maxim-dovydenok

Description

@maxim-dovydenok

Symfony version(s) affected

6.2.8

Description

Types like array<int|string> are not processed correctly, collection type is defined as array of Object "(int|string)", but should be array of (int or string)

How to reproduce

Define class with a property array $property, add type in docblock, array value type should be a union, for example int|string. For example array<int|string> or (int|string)[]

class ClassType
{
    /**
     * @param array<int|string> $property
     */
    public function __construct(
        public readonly array $property,
    ) {
    }
}

Use PropertyInfoExtractor with PhpDocExtractor to get property types

      $reflectionExtractor = new ReflectionExtractor();
      $phpDocExtractor = new PhpDocExtractor();
      $propertyInfo = new PropertyInfoExtractor(
          listExtractors: [$reflectionExtractor],
          typeExtractors: [$phpDocExtractor, $reflectionExtractor],
          accessExtractors: [$reflectionExtractor],
          initializableExtractors: [$reflectionExtractor]
      );

      $types = $propertyInfo->getTypes(ClassType::class, 'property');

You'll get

^ array:1 [
  0 => Symfony\Component\PropertyInfo\Type^ {#737
    -builtinType: "array"
    -nullable: false
    -class: null
    -collection: true
    -collectionKeyType: array:1 [
      0 => Symfony\Component\PropertyInfo\Type^ {#829
        -builtinType: "int"
        -nullable: false
        -class: null
        -collection: false
        -collectionKeyType: []
        -collectionValueType: []
      }
    ]
    -collectionValueType: array:1 [
      0 => Symfony\Component\PropertyInfo\Type^ {#738
        -builtinType: "object"
        -nullable: false
        -class: "(int|string)"
        -collection: false
        -collectionKeyType: []
        -collectionValueType: []
      }
    ]
  }
]

Weird part is -builtinType: "object", -class: "(int|string)"

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions