1

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.

user17535142
  • 71
  • 1
  • 6
  • You might find this doc page useful. https://phpstan.org/user-guide/ignoring-errors – nice_dev Dec 01 '21 at 05: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. – nice_dev Dec 01 '21 at 05:36
  • @nice_dev No, because I'm not trying to ignore errors. – user17535142 Dec 01 '21 at 05: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. – user17535142 Dec 01 '21 at 05: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? http://sandbox.onlinephpfunctions.com/code/f9551c1360543be6dbf8a003576f11b994663f2e – nice_dev Dec 01 '21 at 06:03
  • @nice_dev Adding a bunch of custom comments everywhere in my code is madness. You can't be serious. – user17535142 Dec 01 '21 at 06:26
  • @nice_dev I ran PHPStan on your example script and it reported this: `"Unreachable statement - code above always terminates."` (for the echo line). With the echo line removed, it doesn't report any error. – user17535142 Dec 01 '21 at 06:30
  • Then for the errors part, you could contact some PHPStan expert to check for simpler ways. Regarding my script part, yes, this is the expected behaviour from PHPStan. If this was between function/method calls, we are done for good as runtime errors are anyways hard to track. Also, `2147483647 & 1` is a pretty static literal values' AND operation to begin with. In reality, it could be `$var1 & $var2` – nice_dev Dec 01 '21 at 06:36

0 Answers0