Skip to content
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.CodeAnalysis.EmptyStatement Add option to allow only comments in statements #2240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thewilkybarkid
Copy link
Contributor

@thewilkybarkid thewilkybarkid commented Nov 23, 2018

There are cases where it's considered valid to only have a comment in a statement, this adds an option to allow it (refs libero/php-coding-standard#34 (comment)).

($token['scope_opener'] + 1),
($token['scope_closer'] - 1),
$token['scope_closer'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't expecting to have to change this, but skips the last token otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a known issue, see #1319

*/
} elseif ($baz) {
// phpcs:set Generic.CodeAnalysis.EmptyStatement somethingMadeUp true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent confusion between real test related PHPCS annotations and annotations which are part of the test, it's better to use something like //phpcs:ignore Standard.Category.SniffName -- for reasons here.

} else {

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably reset the changed property to the default at the end of this test to prevent issues with future test cases being added.

@jrfnl
Copy link
Contributor

jrfnl commented Nov 23, 2018

Just thinking out loud: is a property the way to go here or should this be solved via error codes, i.e. having a different error code for a completely empty statement vs an empty statement only containing a comment ?

That way, you could, for instance, exclude DetectedCatchComment, while still throwing errors for DetectedCatch and DetectedIfComment.

@thewilkybarkid
Copy link
Contributor Author

I like the idea of more granular errors. Might need to go for something like DetectedCatch.ContainsComment to maintain BC? (eg to not change the behaviour of https://github.com/doctrine/coding-standard/blob/0247a0d440e196d93611bd0f3c561ee12bd82789/lib/Doctrine/ruleset.xml#L24-L27)

@jrfnl
Copy link
Contributor

jrfnl commented Nov 24, 2018

Error codes are not supposed to contain .s, which is why I suggested DetectedCatchComment.

Adding additional error codes will be BC-break, but only a quite minor one in this case, so I suspect it would be an acceptable one, though @gsherwood can give you a more definite answer.

@gsherwood
Copy link
Member

I prefer the new error codes over the sniff property as it is more flexible, but this BC break will silently stop existing include and exclude rules from working.

This sniff was changed to use new error codes (issues #314) so that it could be customised more easily, so I assume people are doing that today.

What PHPCS probably needs is the ability to provide aliases for message codes to allow for these sort of deprecations to occur. For example, we throw a EmptyCatchWithComment but we alias it as EmptyCatch so it still works with old rulesets. These aliases can then be removed during a major release.

@jrfnl
Copy link
Contributor

jrfnl commented Jul 13, 2023

PR #3409 was marked as a duplicate of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants