8000 [PropertyInfo] PhpStanExtractor with @phpstan-type is treated as a class, which not exists · Issue #59227 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] PhpStanExtractor with @phpstan-type is treated as a class, which not exists #59227

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
michaljusiega opened this issue Dec 16, 2024 · 3 comments

Comments

@michaljusiega
Copy link
Contributor
michaljusiega commented Dec 16, 2024

Symfony version(s) affected

7.2

Description

Hi,
I'm facing weird issue in SF 7.2. Using local-aliases (https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases) with @phpstan-type is badly resolved by extractors.

How to reproduce

<?php

use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;

/** @phpstan-type AspectRatio 1.0|1.1|1.2|1.3|1.4|1.5|1.6|1.7|1.8|1.9|2.0 */
class Test1234
{
    /** @param AspectRatio|null $aspectRatio */
    public function __construct(public float|null $aspectRatio)
    {
    }
}

$propertyInfo = new PropertyInfoExtractor(
    listExtractors: [],
    typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()],
    descriptionExtractors: [],
    accessExtractors: [],
    initializableExtractors: []
);

$types = $propertyInfo->getTypes(Test1234::class, 'aspectRatio');

dump($types);

Resulted in this output:

array:1 [▼
  0 => Symfony\Component\PropertyInfo\Type {#42 ▼
    -collectionKeyType: []
    -collectionValueType: []
    -builtinType: "object"
    -nullable: true
    -class: "AspectRatio" <== this class not exists
    -collection: false
  }
]

But I expected this output instead:

array:1 [▼
  0 => Symfony\Component\PropertyInfo\Type {#23 ▼
    -collectionKeyType: []
    -collectionValueType: []
    -builtinType: "float"
    -nullable: true
    -class: null
    -collection: false
  }
]

Possible Solution

Exactly the same result is expected when we set PropertyInfoExtrator to typeExtractors: [new ReflectionExtractor()].

I don't know how to fix this, but I guess this bug is maybe related with phpstan/phpdoc-parser by #58800

Thanks for help!

Additional Context

No response

@stof
Copy link
Member
stof commented Dec 17, 2024

This is why it is not wise to use a phpstan-only type alias in @param instead of using @phpstan-param, as it breaks tools that support reading phpdoc without being aware of the phpstan type aliases.
There is no way to know that @param AspectRatio|null $aspectRatio is not referring to an AspectRatio class for such tool, as it could totally be the meaning it has if there was no type alias.

@fabpot
Copy link
Member
fabpot commented Dec 18, 2024

Based on stof’s comment, shall we close add won’t fix?

@michaljusiega
Copy link
Contributor Author
michaljusiega commented Dec 19, 2024

Based on stof’s comment, shall we close add won’t fix?

Probably. I'll let you know today. :)

@fabpot - yes, we can close. @phpstan-* resolve this.

Thank you @fabpot @stof

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

4 participants
0