-
Notifications
You must be signed in to change notification settings - Fork 15
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
Allow for loop increment expression to use variable defined inside loop #262
Conversation
Little heads-up for when you continue working on this: each "expression" segment in a |
75a0803
to
10b9396
Compare
Ugh, trying to find the semicolons that divide a for loop's initialization, condition, and increment expressions is really hard since each expression could contain a closure that itself contains semicolons. 😩 |
Haven't looked at your current code, but in case you aren't already, you may want to use something along the lines of this code to jump over closed structures: https://github.com/PHPCSStandards/PHPCSUtils/blob/b65fbd47c38202a667ea3930a4a51c5c8b9ca434/PHPCSUtils/Utils/PassedParameters.php#L243-L280 (That's the code I use to loop through and split out the parameters in a function call, where parameter values may contain the comma delimiter as well) |
You may also find this code useful to see: https://github.com/PHPCSStandards/PHPCSUtils/blob/b65fbd47c38202a667ea3930a4a51c5c8b9ca434/PHPCSUtils/Utils/Context.php#L152-L204 (utility which splits a |
d561cb7
to
f06152f
Compare
Thank you, @jrfnl! That was the key; I needed to check for the |
In most cases, for loops do not require special processing by this sniff because they share the variable scope of their container. However, the third expression of the loop (what I'm calling the "increment expression") is technically not evaluated until after the loop runs at least once. This means that you could define a variable inside the loop that is then used in the increment expression, despite the definition coming lexically after the use.
In this PR, we modify the sniff to set aside variables in the increment expression until after the loop has been parsed. Variables found in this way are then parsed in their own operation (but in their original position).
Fixes #261