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

Checking a type (Null) of a variable #8653

Closed
githubjeka opened this issue Jan 6, 2023 · 7 comments
Closed

Checking a type (Null) of a variable #8653

githubjeka opened this issue Jan 6, 2023 · 7 comments

Comments

@githubjeka
Copy link

Bug report

/** @var null|SomeClass $someVar */
if ($this->check($someVar) // checked ($someVar !== null)
    $someVar->someMethod();
Cannot access $someMethod on SomeClass|null.

P.S. more real example https://phpstan.org/r/8eb1d76d-6ea8-4ab1-aee5-5d2511acb1b7

Expected output

No error

@githubjeka
Copy link
Author

Are there issues that prevent this from being done without introducing additional PHPDoc tags?

@ondrejmirtes
Copy link
Member

@githubjeka
Copy link
Author

githubjeka commented Jan 6, 2023

For me @phpstan-assert-if-true looks redundant, then @var phpDoc tag can be used.
https://phpstan.org/r/78bfdec6-2c64-4e22-9a3b-f0d367bc44fd

In any case, thanks for your work and quick feedback.

@ondrejmirtes
Copy link
Member

@var is much worse and I hate it. PHPStan can't verify anything in it, so it's always trusted. It's anti-DRY and very error prone.

@githubjeka
Copy link
Author

I didn't think about it. You're right.

@github-actions
Copy link

github-actions bot commented Feb 7, 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 Feb 7, 2023
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