phpstan / phpstan-strict-rules

Extra strict and opinionated rules for PHPStan
MIT License
592 stars 46 forks source link

False positive about possibly undefined variable after for loop #195

Closed greg0ire closed 1 year ago

greg0ire commented 1 year ago

Bug report

With strict rules turned on PHPStan reports that a variable will always be overwritten if followed by a for loop that uses it in the assignment part, and that's great: https://phpstan.org/r/244ed1d0-003c-492f-99d2-ce5a075ae515

If you attempt to fix that though, you run into a contradictory error:

Code snippet that reproduces the problem

https://phpstan.org/r/030872ba-acba-4a27-b267-1f30156b1417

Expected output

No errors

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

Yes, it helps me make it easier to spot errors in data providers

MartinMystikJonas commented 1 year ago

This is expected. Strict rules disallow use of variables from loops outside loops. You can turn off this behaviour (enabled by strict rules) by this config option https://phpstan.org/config-reference#pollutescopewithloopinitialassignments to false true (edited)

greg0ire commented 1 year ago

Thanks @MartinMystikJonas !

github-actions[bot] commented 1 year ago

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.