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.67k stars 1.48k forks source link

Feature suggestion: sniff to check whitespace around incrementors/decrementors #2172

Closed jrfnl closed 6 years ago

jrfnl commented 6 years ago

While working on something else, I suddenly realized that there is no sniff to check the whitespace before/after incrementors/decrementors.

$i = 10;
--$i; // This is what would be valid.
-- $i; // This should be flagged and fixed.
-- /*comment*/ $i; // This should just be flagged.
$i++; // This is what would be valid.
$i ++; // This should be flagged and fixed.
$i /*comment*/ ++; // This should just be flagged.

I realize it is rare to encounter such code, but it is perfectly valid PHP code and PHP will just ignore the whitespace and comments and function as expected.

I haven't (yet) checked if there are any other sniffs which would get confused by this, but running the above code samples against all the included standards in PHPCS itself, doesn't throw any unexpected false positive/negatives.

@gsherwood Do you think its a good idea to add a new sniff for this ? I'm thinking Generic.WhiteSpace.IncrementDecrementSpacing. And if so, should it be added to any of the existing rulesets ?

I'd be happy to create the sniff.

gsherwood commented 6 years ago

Thanks a lot for this. I've added it to the Squiz and PHPCS standards as well.