2

I have a lot of while (1) and if (1) etc. in my code. PHPStan thinks these are errors, saying things such as While loop condition is always true. or If condition is always true..

I don't want to turn off those errors, because there could be valid instances of them. For example, while ($some_var) could always be true if I have a bug in my code, and that's something I definitely want to catch. I just want it to ignore those specific code strings, when I know for sure it's not an error but intentional.

How is this done? I've read the manual a lot but not found a way.

8
  • 1
    You might find this doc page useful. phpstan.org/user-guide/ignoring-errors Commented Dec 1, 2021 at 5:34
  • while ($some_var) could always be true if I have a bug in my code Since PHPStan is a static analyzer, it is unlikely that it would capture run time errors. Commented Dec 1, 2021 at 5:36
  • @nice_dev No, because I'm not trying to ignore errors. Commented Dec 1, 2021 at 5:47
  • @nice_dev It's remarkably intelligent. It really seems to "understand" the code on a level which you'd want PHP itself to do. Commented Dec 1, 2021 at 5:47
  • I am pretty sure you could ignore certain lines as their doc is describing. Can you let me know if PHPStan could find out runtime issues like this one? sandbox.onlinephpfunctions.com/code/… Commented Dec 1, 2021 at 6:03

1 Answer 1

3

You can use comments like for those cases

// @phpstan-ignore-next-line
Sign up to request clarification or add additional context in comments.

1 Comment

While this will effectively suppress the error, I would recommend against it as it will suppress all errors for next line. So you could introduce bugs if you change the condition later, and they would go unoticed. Better be future-proof by ignoring the specific error bothering OP: // @phpstan-ignore while.alwaysTrue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.