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

Add support for asserting the type of $this #8904

Closed
greg0ire opened this issue Feb 16, 2023 · 3 comments · Fixed by phpstan/phpdoc-parser#209
Closed

Add support for asserting the type of $this #8904

greg0ire opened this issue Feb 16, 2023 · 3 comments · Fixed by phpstan/phpdoc-parser#209

Comments

@greg0ire
Copy link

Feature request

I'm currently trying to refactor a part of Doctrine ORM to use DTOs: doctrine/orm#10405
I have build a complex hierarchy of classes, and at some point, I've grown tired of doing

if ($assoc instanceof ToOneAssociationMapping && $assoc AssociationOwningSideMapping) {

I thought I'd just introduce a method to answer that particular question, and use psalm-assert to make PHPStan and Psalm understand what I'm doing.

This does not seem to be supported by PHPStan currently: https://phpstan.org/r/6416296d-9c38-4510-a45c-251f96e61fcd
Psalm seems to support it: https://psalm.dev/r/452fefed38

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

I was happy to see the Algolia integration on the website. Searchable docs are great!

@ondrejmirtes
Copy link
Member

Another example where this would be useful: https://phpstan.org/r/bc8a92a6-c5a4-4252-94bf-456d943d0bb8

@phpstan-bot
Copy link
Contributor

@greg0ire After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-13: PHPDoc tag @phpstan-assert-if-true has invalid value (OwningSide $this): Unexpected token "\n\t ", expected '->' at offset 48
+13: PHPDoc tag @phpstan-assert-if-true has invalid value (OwningSide $this): Unexpected token "\n\t ", expected '->' at offset 48 on line 2
 22: Call to an undefined method Association::foo().
Full report
Line Error
13 PHPDoc tag @phpstan-assert-if-true has invalid value (OwningSide $this): Unexpected token "\n\t ", expected '->' at offset 48 on line 2
22 Call to an undefined method Association::foo().

@github-actions
Copy link

github-actions bot commented Oct 9, 2023

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 Oct 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants