sascha-egerer / phpstan-typo3

TYPO3 CMS class reflection extension for PHPStan & framework-specific rules
MIT License
42 stars 22 forks source link

AbstractRepository tries to resolve Abstract model #140

Closed a-r-m-i-n closed 11 months ago

a-r-m-i-n commented 11 months ago

In projects where we provide an AbstractRepository, I get the error:

Class Vendor\Extension\Domain\Model\Abstract was not found while trying to analyse it - discovering symbols is probably not configured properly. 💡 Learn more at https://phpstan.org/user-guide/discovering-symbols

Although the class itself is marked as "abstract":

abstract class AbstractRepository extends Repository
{
}

Excluding the path of the AbstractRepository did not solve the issue, nor moving the AbstractRepository out of the Domain/Repository namespace. Also, providing a model named "Abstract" is not possible. because abstract is a reserved word in PHP.

The only solution for this is, to not include the vendor/saschaegerer/phpstan-typo3/extension.neon as long as this bug is fixed.

oliverklee commented 11 months ago

I solved this by propagating the parent classes' generics annotations in my abstract repository: https://github.com/oliverklee/ext-feuserextrafields/blob/v5.4.0/Classes/Domain/Repository/AbstractFrontendUserRepository.php

a-r-m-i-n commented 11 months ago

Very cool! Thank you @oliverklee This solved the issue for me as well.

a-r-m-i-n commented 11 months ago

However, when a repository class is marked as "abstract" phpstan should not expect a model for this repository existing.

oliverklee commented 11 months ago

@a-r-m-i-n Could you check if @inheritdoc would also solve the problem?

oliverklee commented 11 months ago

(just a wild guess; haven't tried this yet)

a-r-m-i-n commented 11 months ago

Oh, no. It did not work. I've already renamed the repository to "AbstractEntityRepository" and now was able to provide an abstract model for this - and thought because of this, it would work. My fault!

After reverting, I've added

/**
 * @template TEntityClass of \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
 * @extends Repository<TEntityClass>
 */

to the AbstractRepository. Did I miss something?

oliverklee commented 11 months ago

@a-r-m-i-n Do your non-abstract repos also have the corresponding generics annotation, e.g. like this?

https://github.com/oliverklee/ext-feuserextrafields/blob/main/Classes/Domain/Repository/FrontendUserRepository.php

a-r-m-i-n commented 11 months ago
@inheritdoc

to the AbstractRepository did not change anything.

a-r-m-i-n commented 11 months ago

@a-r-m-i-n Do your non-abstract repos also have the corresponding generics annotation, e.g. like this?

https://github.com/oliverklee/ext-feuserextrafields/blob/main/Classes/Domain/Repository/FrontendUserRepository.php

No. I try this

sascha-egerer commented 11 months ago

@a-r-m-i-n Do you have more information about the source of the error? Where does it happen?

oliverklee commented 11 months ago

Thanks for trying @inheritdoc. :pray: It was worth a try. :slightly_smiling_face:

a-r-m-i-n commented 11 months ago

@a-r-m-i-n Do your non-abstract repos also have the corresponding generics annotation, e.g. like this?

https://github.com/oliverklee/ext-feuserextrafields/blob/main/Classes/Domain/Repository/FrontendUserRepository.php

Did not help

a-r-m-i-n commented 11 months ago

@a-r-m-i-n Do you have more information about the source of the error? Where does it happen?

Not sure what you mean. The error is assigned to a file named project/Classes/DataProcessing/BenefitProcessor.php which is a DataProcessor.

The line which it is pointing to, calls a non-existing-method (triggering magic __call) of one repository, extending from my AbstractRepository.

sascha-egerer commented 11 months ago

Not sure what you mean. The error is assigned to a file named project/Classes/DataProcessing/BenefitProcessor.php which is a DataProcessor.

The line which it is pointing to, calls a non-existing-method (triggering magic __call) of one repository, extending from my AbstractRepository.

Ok, how doe the code look like in the processor? Are you using AbstractRepository as type or annotation somewhere in there?

a-r-m-i-n commented 11 months ago

Ok, how doe the code look like in the processor? Are you using AbstractRepository as type or annotation somewhere in there?

No, the only files referencing to AbstractRepository are the repositories extending from it, within the same namespace. AbstractRepository is not being used elsewhere.

a-r-m-i-n commented 11 months ago

Maybe good to know: It only happens when I call a non-existing method. Existing methods - in the same repo - do not trigger the issue.

sascha-egerer commented 11 months ago

Maybe good to know: It only happens when I call a non-existing method. Existing methods - in the same repo - do not trigger the issue.

You mean methods that are not implemented right? So it does also happen for "magic" methods like findByFieldname, right?

a-r-m-i-n commented 11 months ago

Ahhh. Actually there was a bug in the code. Because the model has no "blah" property, but I called findByBlah($blubb). When I call a magic method findByTitle, which' property actually exist, I get no error anymore.

So the fact that I got an error was correct. Just the error message wasn't.

sascha-egerer commented 11 months ago

Ahhh. Actually there was a bug in the code. Because the model has no "blah" property, but I called findByBlah($blubb). When I call a magic method findByTitle, which' property actually exist, I get no error anymore.

So the fact that I got an error was correct. Just the error message wasn't.

Ok, i can reproduce it. I'll take care

sascha-egerer commented 11 months ago

@a-r-m-i-n Could you please check if you'll get a proper error message with the small adjustment i did?

a-r-m-i-n commented 11 months ago

Yes, I was able to confirm that your patch works. 👍

Before:

 ------ -----------------------------------------------------------------------------------------------------------
  Line   extension/Classes/DataProcessing/BenefitProcessor.php
 ------ -----------------------------------------------------------------------------------------------------------
  28     Class Vendor\Extension\Domain\Model\Abstract was not found while trying to analyse it - discovering symbols is
         probably not configured properly.
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
 ------ -----------------------------------------------------------------------------------------------------------

After:

 ------ ------------------------------------------------------------------------------------------------------
  Line   extension/Classes/DataProcessing/BenefitProcessor.php
 ------ ------------------------------------------------------------------------------------------------------
  28     Call to an undefined method Vendor\Extension\Domain\Repository\BenefitRepository::findByBlah().
 ------ ------------------------------------------------------------------------------------------------------

Please merge and release 😊