Skip to content
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

Class not found in PhpStorm for Entities that implement an interface in PHP 8 using attributes #5850

Closed
knallcharge opened this issue Oct 29, 2021 · 5 comments

Comments

@knallcharge
Copy link

knallcharge commented Oct 29, 2021

Bug report

I'm having a hard time finding the cause of the following issue: I migrated a project from PHP 7.4 to 8.0 and converted all route- and doctrine-annotations to attributes. The code works just fine, but I get "class not found"-issues from phpstan for all (doctrine-)entities that implement an interface when using attributes.

grafik

This does not only happen when using the entities as class-members but everywhere in the project:
grafik

Some strange things I found out:

  • The errors show only in PhpStorm. When executing phpstan in the console, I get the "No errors"-message. I still can't just ignore the errors as somehow also rector is affected as it's using the same phpstan.neon.
  • PhpStorm itself finds all classes (also executing PhpStorm's code inspection shows no errors, even though it also lists phpstan-errors), it's just phpstan "inside" PhpStorm. The configuration for phpstan in PhpStorm has not been changed. Also the code runs without errors.
  • Only entities that implement an interface are not found (e.g. Stringable or UserInterface/Serializable for an User object), all other doctrine-entities are ok.
  • When removing the interface (and the needed methods) the errors disappear (but of course this is not an option).
  • When reverting from attributes for the repository (#[\Doctrine\ORM\Mapping\Entity(repositoryClass: UserRepository::class)]) back to annotations (@Orm\Entity(repositoryClass="App\Repository\UserRepository")) all entity-classes are found again.

I'm out of ideas here. Is it PhpStorm or my project-configuration, did something go wrong during the migration? Is this a bug in phpstan or phpstan-doctrine? Is this also related to some (missed) setting in the repository-classes?
I did endless searches for almost two days now, have tried everything I can think of that I may have missed during my migration, but to no avail.

Code snippet that reproduces the problem

https://phpstan.org/r/16989271-d54b-4fc8-9a08-2bff94418999

Note that this snippet only shows a (very reduced) part of my setup but does not reproduce the problem which seems somehow related to PhpStorm. The class "SomeObject" from the snippet would show as "not found" in my project.

Expected output

All entities should be found.

Did PHPStan help you today? Did it make you happy in any way?

Apart from the above issue phpstan (and rector) made the migration from PHP 7.4 to 8.0 a piece of cake :)

@ondrejmirtes
Copy link
Member

Thank you for your kind words! This has to have to do something with how PhpStorm executes PHPStan - it should be reproducible on the command line if you recreate how PhpStorm does it. I guess some tmp files are involved.

@knallcharge
Copy link
Author

Thanks for your quick reply!
I did some more testing and changed things in my phpstan.neon:
In addition to

    paths:
        - ../src
        - ../tests

I added

    scanDirectories:
        - ../src

This idea never occurred to me as page "Discovering symbols" says: "By default, it looks for them in these two places: Analysed paths (files and directories)", so I never tried this before, because I thought adding "../src" once to my phpstan.neon (which is in a subfolder "build" thus "../") should be enough. Which it - kind of - is, so I still don't understand why adding this setting should make a difference only for entites that implement an interface, but: this seems to resolve my issue! (fingers crossed)

@ondrejmirtes
Copy link
Member

I just made a small change that would make the scanDirectories line unnecessary: phpstan/phpstan-src@9f5f911

Please test it now with dev-master or on Monday when PHPStan 1.0 is released :) Thank you!

@knallcharge
Copy link
Author

Works, thanks!

@github-actions
Copy link

github-actions bot commented Dec 4, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants