Conversation
|
Not sure what's wrong with the failing build :/ |
@gmponos As per: https://travis-ci.org/github/squizlabs/PHP_CodeSniffer/jobs/701538216#L544-L549 you didn't add the new files to the |
There was a problem hiding this comment.
May I suggest adding an additional check for there being a semi-colon being after the parenthesis_closer and a open curly after that ?
The sniff would now also trigger an error on legitimate uses of control structures without body.
Note: the only control structures which can be declared without a body are: while, for and declare. All other control structures need a body with or without the curly braces or the alternative syntax with a colon and "end...".
Don't quite get that... Is this the case you want to cover? If not can you provide an example similar to the above...
And In anyway I must add I don't think we should include |
Fair enough, but using a foreach without body doesn't really do anything, so there wouldn't be any point doing that. The one exception being when using references, but that's a whole other snakepit. Regarding my suggestion: IMO, the sniff should never trigger on clearly deliberate use of a control structure without body: for($i =0; $i<10; $i++);
echo $i;Only when it could conceivably be a mistake: for($i =0; $i<10; $i++); {
} |
Nowdays due to PSR2/12 it is common to add braces to loops... So IMHO both of the cases above should be reported. |
|
@gmponos This is not about braces or no braces. My example is 100% valid code without any mistakes. And just like you say, as it is now common to use braces, the sniff should not trigger on intentional use of control structures without body. This: for($i =0; $i<10; $i++);
echo $i; // 10Is something completely different from: for($i =0; $i<10; $i++)
echo $i; // 12345678910or from this - which is what you stated you were targetting: for($i =0; $i<10; $i++);
{
echo $i; // 10, but was intended to be 12345678910
} |
|
At the very least, please consider using a different error code for with/without curly brace as that way, people who use the short notation without body could still use the sniff, but exclude the "without braces" error code. |
| $tokens = $phpcsFile->getTokens(); | ||
| $token = $tokens[$stackPtr]; | ||
| if (isset($token['scope_opener']) === false) { | ||
| $phpcsFile->addError('The `%s` loop statement does not have a body', $stackPtr, 'NoBody', [$token['content']]); |
There was a problem hiding this comment.
Sniffs typically use the code Found so the message code reads better - Generic.CodeAnalysis.LoopWithNoBody.Found
I'd also strtolower the token content because you never know what case it will be defined in.
| $tokens = $phpcsFile->getTokens(); | ||
| $token = $tokens[$stackPtr]; | ||
| if (isset($token['scope_opener']) === false) { | ||
| $phpcsFile->addError('The `%s` loop statement does not have a body', $stackPtr, 'NoBody', [$token['content']]); |
There was a problem hiding this comment.
You mentioned on the original issue that you thought this should be a warning, but you are generating an error here. Was that an intentional change or do you think warning is still the correct severity for this?
Follow up of the comment here #2994 (comment)
I also discovered that the foreach-loop can also have a semicolon at the end.