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

[PROPOSAL] Disallow empty if and else blocks #3408

Closed
jlherren opened this issue Aug 10, 2021 · 3 comments
Closed

[PROPOSAL] Disallow empty if and else blocks #3408

jlherren opened this issue Aug 10, 2021 · 3 comments

Comments

@jlherren
Copy link

I'd like to propose a sniff that detects empty if and else blocks, since they never make any sense. Adding a comment inside the empty block would suppress the error, similar to DisallowEmptyFunctionSniff.

Empty elseif blocks can sometimes make sense if followed by other elseifs or an else.

Perhaps the sniff could even be fixable by extracting side-effects of the condition, however, it would be more useful if we can detect whether there are any side-effects at all, which I'm not sure is possible in phpcs.

@jlherren
Copy link
Author

Oops, I meant to post this to slevomat/coding-standard, disregard that, sorry :(

@jrfnl
Copy link
Contributor

jrfnl commented Aug 10, 2021

@jlherren Just so you know, the Generic.CodeAnalysis.EmptyStatement would largely fit your needs. Could possibly do with some fine-tuning, in which case, I suspect a PR would be welcome.

if ($true) {
} elseif($bar) {
} else {
    // Do something.
}
--------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
--------------------------------------------------------------------------------------------------
 3 | ERROR | Empty IF statement detected (Generic.CodeAnalysis.EmptyStatement.DetectedIf)
 4 | ERROR | Empty ELSEIF statement detected
   |       | (Generic.CodeAnalysis.EmptyStatement.DetectedElseif)
 5 | ERROR | Empty ELSE statement detected (Generic.CodeAnalysis.EmptyStatement.DetectedElse)
--------------------------------------------------------------------------------------------------

@jlherren
Copy link
Author

Thanks for the hint. I always find it somewhat hard to discover what sniffs already exists. I've created #3409 to improve it.

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

No branches or pull requests

2 participants