-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Generic.Formatting.DisallowMultipleStatements false positive for FOR loop with no body #2994
Comments
The sample code to test this is: for ($i = 0 ; $i < 10; $i++);
{
} The sniff auto-fixes this to be: for ($i = 0 ; $i < 10;
$i++);
{
} |
The reason this happens is because the semicolon at the end of the FOR declaration means that the bracket statement below has nothing to do with the FOR statement at all. So PHPCS is not finding brackets and is not finding a valid FOR declaration as part of tokenizing. Technically, I think this is just an empty FOR declaration. So the sniff sees 2 statements on a line - The fix here would have to be in the tokenizer itself, to allow FOR declarations to be valid even when they have no body. Then somehow let the sniffs know that this is a valid construct. I'm not sure if it is worth the effort trying to get this right for FOR declarations, and everything else developers could write. In this specific case, it's good that the sniff generates an error so it can at least be manually reviewed. A workaround might be to have the sniff ignore multiple statements if they are contained within parenthesis, but I haven't thought of the full implications of a change like that. |
Technically, it is a perfectly valid |
I didn't mean to imply that it isn't. I just mean't it isn't tokenizing it as a FOR declaration. |
Sorry, this is confusing me. What do you mean ? |
I meant that it's not part of the scope map. I've gotten some more time to look at the code now and I can see that the sniff is trying to ignore FOR statements but is confused in this specific case because it never expects to see one end with a semicolon. It looks to me like it would need to perform the same check on the previous semicolon it found to confirm that it too is not part of a FOR statement. |
Do you have room for a sniff that detects for-loop without a body and maybe return a warning (as it is valid). Users can raise this to error. Asking because this happened as a bug for me. Had a semicolon missed and I could not understand why the for-loop is not entering the body. |
Yeah, I would include that due to its simplicity. It would likely just look for T_FOR without a scope opener, indicating that the FOR has no body and may be an error. |
… positive for FOR loop with no body
… positive for FOR loop with no body
I've committed a fix for this issue, which will be released in 3.5.6. Thanks for reporting it. |
Hi,
When I run phpcs against this file https://github.com/doctrine/coding-standard/pull/208/files#diff-9023c633b2425ded52916bf44f88ede1R17
The error I get are:
I care mainly for
17 | ERROR | [x] Each PHP statement must be on a line by itself (Generic.Formatting.DisallowMultipleStatements.SameLine)
The autofix is this:
https://github.com/doctrine/coding-standard/pull/208/files#diff-df7e22f20fb934baed4dd64e4ff1cd75R17
which seems wrong.
The text was updated successfully, but these errors were encountered: