Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Call to undefined method Doctrine\ORM\Query\Expr::myCustomMethod() #62

Closed
esbobkov opened this issue May 20, 2019 · 9 comments
Closed

Comments

@esbobkov
Copy link

I have custom Expr class extended from Doctrine\ORM\Query\Expr

I think, PHPStan doesn't know about that class and trigger error

In ExpressionBuilderDynamicReturnTypeExtension.php line 67:
                                                                                                         
  Attempted to call an undefined method named "myCustomMethod" of class "Doctrine\ORM\Query\Expr".

is it because Doctrine\ORM\Query\Expr harcoded in getClass() method?
How to fix that?

@ondrejmirtes
Copy link
Member

Hi, how are you able to use custom Expr class? Do you extend your QueryBuilder? Can you show me your $qb->expr() method?

@esbobkov
Copy link
Author

It's used as helper class for building custom DQL parts.
instantiate only as new CustomExpr(). We able to generate some DQL parts and pass their to standard QueryBuilder methods.

So, our EntityManager doesn't know about CustomExpr too..)

Thinking about adding exprClass argument for ExpressionBuilderDynamicReturnTypeExtension costructor. What do You think?

@ondrejmirtes
Copy link
Member

Can you create a small sample repo? Still can't imagine how it works and why PHPStan fails on it.

@esbobkov
Copy link
Author

Can you create a small sample repo? Still can't imagine how it works and why PHPStan fails on it.

I prepared an example project

@ondrejmirtes
Copy link
Member

Hi, I just commited a fix to master that solves this: 9fac3a7

With a few caveats:

  1. Right now, your code won't crash PHPStan, but it won't be checked for any errors. Run PHPStan with doctrine.reportDynamicQueryBuilders set to true (https://github.com/phpstan/phpstan-doctrine/blob/master/rules.neon#L3) to verify this. It might find other dynamic QBs that are impossible to verify.
  2. To be able to verify code using your custom Expr class, unfortunately, the class has to contain a constructor, even if it's empty. This is currently a limitation of dynamic return type extension system in PHPStan: https://github.com/phpstan/phpstan/blob/master/src/Analyser/Scope.php#L1051-L1070

Please follow these steps and tell me if it solves your problem and I can tag a new version. Thanks.

@esbobkov
Copy link
Author

This commit fixed my issue and thanks for caveats!

@ondrejmirtes
Copy link
Member

@ondrejmirtes
Copy link
Member

You no longer need the empty constructor, thanks to 0.11.9: https://github.com/phpstan/phpstan/releases/tag/0.11.9 (and this commit: phpstan/phpstan@5fc3a8d)

@github-actions
Copy link

github-actions bot commented May 1, 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 May 1, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants