8000 [PropertyInfo] Invalid type extracted when property defined in trait · Issue #28732 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Invalid type extracted when property defined in trait #28732

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
ychanan opened this issue Oct 5, 2018 · 9 comments
Closed

[PropertyInfo] Invalid type extracted when property defined in trait #28732

ychanan opened this issue Oct 5, 2018 · 9 comments

Comments

@ychanan
Copy link
ychanan commented Oct 5, 2018
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version v4.1.3

Description
When using the PhpDocExtractor to extract information about a property, if that property is defined within a trait, the resulting type does not take into consideration the use statements in that trait.

How to reproduce

# src/App/Entity/TestEntity.php

namespace App\Entity;

class TestEntity
{
    use TestTrait;
}
# src/App/Entity/TestTrait.php

namespace App\Entity;

use Doctrine\Common\Collections\Collection;

trait TestTrait
{
    /**
     * @var Collection
     */
    protected $myCollection;
}
$phpDocExtractor = new PhpDocExtractor();
$types = $phpDocExtractor->getTypes(TestEntity::class, 'myCollection');
echo $types[0]->getClassName();

Got: App\Entity\Collection
Expected: Doctrine\Common\Collections\Collection

Additional information

  • Tested with other classes as well, not just Doctrine Collection, same result.
  • If we add use Doctrine\Common\Collections\Collection; to the TestEntity class, then it works as expected.
  • If we add the fully-qualified class name (\Doctrine\Common\Collections\Collection) to the @var tag on the $myCollection property, then it also works as expected.
@linaori
Copy link
Contributor
linaori commented Oct 5, 2018

I believe this is a limitation within PHP

@dunglas
Copy link
Member
dunglas commented Jan 21, 2020

I also think that it's a limitation within PHP that us hard to circumvent. Two possible solutions:

  • introduce a hack using ReflectionMethod::getFileName(), assuming the project is using PSR-4
  • switch from phpDocumentor's lib to a parser-based solution such as https://github.com/phpstan/phpdoc-parser

cc @soyuka

@soyuka
Copy link
Contributor
soyuka commented Jan 28, 2020

Switching to phpdoc-parser might be good, lot of work I don't have the time for this for now... see also api-platform/core#3349 for research material / ways to reproduce.

@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?

@soyuka
Copy link
Contributor
soyuka commented Jan 30, 2021

yes @carsonbot it is still relevant :)

@xabbuh
Copy link
Member
xabbuh commented Feb 13, 2021

Can you confirm that #40175 fixes this?

@fabpot fabpot closed this as completed Feb 16, 2021
fabpot added a commit that referenced this issue Feb 16, 2021
…ed in traits (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[PropertyInfo]  use the right context for properties defined in traits

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #28732, #34191
| License       | MIT
| Doc PR        |

Commits
-------

1572491 use the right context for properties defined in traits
@soyuka
Copy link
Contributor
soyuka commented Feb 16, 2021

this may also fix #37149 then

@xabbuh
Copy link
Member
xabbuh commented Feb 16, 2021

@soyuka Are you sure?

@xabbuh
Copy link
Member
xabbuh commented Feb 16, 2021

it doesn't seem so, but #40208 should fix it

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

7 participants
0