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

Logic not operator followed by a space #1051

Closed eclipxe13 closed 8 years ago

eclipxe13 commented 8 years ago

Would you consider to add a Sniff for logic not operator followed by a space?

// changes
if (!$someVar || !$x instanceOf 'stdClass') {
// to
if (! $someVar || ! $x instanceOf 'stdClass') {

In FriendsOfPHP/PHP-CS-Fixer they use the fix logical_not_operators_with_successor_space

Also, other developers and projects like http://auraphp.com/contributing/#standards use this style

It would be great to have it on PHP_CodeSniffer (thanks for your great work!)

gsherwood commented 8 years ago

Seemed like a good idea and an easy sniff to create, so I've committed a sniff and it will be in the next release.

The sniff does checking and fixing for PHP and JS files. Include it n a ruleset using this tag:

<rule ref="Generic.Formatting.SpaceAfterNot" />

Or run only that check over your entire code base: phpcs --standard=Generic --sniffs=Generic.Formatting.SpaceAfterNot /path/to/code

Or just fix that in the code base: phpcbf --standard=Generic --sniffs=Generic.Formatting.SpaceAfterNot /path/to/code

Thanks for the suggestion.

aik099 commented 8 years ago

Maybe it also makes sense to create another sniff that would enforce the opposite checking/fixing behavior?

glen-84 commented 8 years ago

I agree with @aik099, except that I think that it could have been done with a single sniff.

Something like Generic.Formatting.LogicalNotSpacing with options spacesBefore and spacesAfter.

mkarnicki commented 5 years ago

I know this is old as the world, but I can't seem to find it - has enforcing a space after the logical not operator been added perhaps? Thanks!

jrfnl commented 5 years ago

@mkarnicki As stated above, the Generic.Formatting.SpaceAfterNot sniff does just that (enfore one space after the not operator). Open PR #2233 will make the sniff configurable, so you would also be able to use it to, for instance, enforce no space after the not operator.

mkarnicki commented 5 years ago

Ah, great. Thank you @jrfnl . I thought it sniffed that as bad style, and you're saying it's the exact opposite :'). That works for me!

yamenarahman commented 4 years ago

@eclipxe13 Would you help me here? https://github.com/ikappas/vscode-phpcs/issues/178