squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.68k stars 1.48k forks source link

Feature request: A sniff for accidentally using bitwise operators in in conditionals #3600

Open deviantintegral opened 2 years ago

deviantintegral commented 2 years ago

An easy mistake to make (and an easy one to miss in code review) is to accidentally use a single & or | inside of an if or other conditional. For example, I recently discovered a long-lived bug along the lines of:

if (file_exists($path) & filesize($path) != 0) {

At least in the code our teams write, it's very rare that they work with bitfields. Would the maintainers be open to adding a sniff in \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis to detect this? As a first pass, I think the sniff would check inside of if, while, and do...while clauses. This would avoid false positives on assignments.

If there's interest, we'd be glad to contribute a PR to make this happen. Thanks!

To gauge interest, please leave a 👍 on this comment you would use this sniff, 👎 if not.

gsherwood commented 2 years ago

I don't accept new sniffs often unless they are part of a published standard. I think this sounds like a good idea but I'd want it to have more support before accepting it so I know it's going to provide enough value for the extra code maintenance.