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

Skip to content

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

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

Closed
maxim-dovydenok opened this issue Jul 5, 2023 · 5 comments
Closed

Comments

@maxim-dovydenok
Copy link
Contributor

maxim-dovydenok commented Jul 5, 2023

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

@maxim-dovydenok maxim-dovydenok changed the title PropertyInfo: Array of union types is not extracted correctly [PropertyInfo] Array of union types is not extracted correctly Jul 5, 2023
@alamirault
Copy link
Contributor

alamirault commented Jul 25, 2023

PhpDocExtractor does not support union types in collection key

Can you try to use PhpStanExtractor instead https://symfony.com/doc/6.2/components/property_info.html#phpstanextractor #40457 ?

@maxim-dovydenok-busuu
Copy link
Contributor

Can you try to use PhpStanExtractor instead

It works, thank you! 👍

PhpDocExtractor does not support union types in collection key

Is it by design? Or just a bug?

I found this code in PhpDocTypeHelper

  $collectionValueTypes = $this->getTypes($type->getValueType());
  if (1 != \count($collectionValueTypes)) {
      // the Type class does not support union types yet, so assume that no type was defined
      $collectionValueType = null;
  } else {
      $collectionValueType = $collectionValueTypes[0];
  }

I assume when it was implemented - Type class didn't support union types, but it's possible to return multiple types, same way as PhpStanExtractor does

    -collectionValueType: array:2 [
      0 => Symfony\Component\PropertyInfo\Type^ {#639
        -builtinType: "string"
        -nullable: false
        -class: null
        -collection: false
        -collectionKeyType: []
        -collectionValueType: []
      }
      1 => Symfony\Component\PropertyInfo\Type^ {#654
        -builtinType: "int"
        -nullable: false
        -class: null
        -collection: false
        -collectionKeyType: []
        -collectionValueType: []
      }
    ]

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

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

No branches or pull requests

5 participants