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/IncrementDecrementSpacing: handle more situations #3626

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Jul 1, 2022

The Generic.WhiteSpace.IncrementDecrementSpacing sniff, so far, only handled incrementors/decrementors when they were directly before/after the variable they apply to.

This commit enhances the sniff to also allow for finding superfluous whitespace when incrementing/decrementing a property or an array item.

Includes unit tests.

Note: I've only made this change for PHP files as JS support will be dropped anyway, so it didn't feel like a good use of my time to work on that.

@gsherwood gsherwood added this to the 3.8.0 milestone Dec 22, 2022
The `Generic.WhiteSpace.IncrementDecrementSpacing` sniff, so far, only handled incrementors/decrementors when they were directly before/after the variable they apply to.

This commit enhances the sniff to also allow for finding superfluous whitespace when incrementing/decrementing a property or an array item.

Includes unit tests.
@jrfnl jrfnl force-pushed the feature/generic-incrementdecrementspacing-handle-more-cases branch from 2ed3d67 to 80a2d06 Compare March 2, 2023 04:12
Copy link

@DannyvdSluijs DannyvdSluijs left a comment

Choose a reason for hiding this comment

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

Seems all to be working okay, just had a small comment about adding test cases for the pre increment instead of only testing post increment.

@@ -15,3 +15,23 @@ $i /*comment*/ --;
$i++;
$i ++;
$i /*comment*/ ++;

// Handle properties and array access too.

Choose a reason for hiding this comment

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

Should we expand the test case with pre-increments of the array and object properties?

++$i['key'];
++ $i['key'];
++$i['key']['id'];
++ $i['key']['id'];

++$obj->prop;
++ $obj->prop;
++ $obj?->prop;

++$obj->obj->prop;
++ $obj->obj->prop;
++ $obj?->obj->prop;

++$obj->prop['key'];
++ $obj->prop['key'];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DannyvdSluijs Good question. In my opinion: no, those tests are not needed as they wouldn't be testing anything (new).

The difference in this case for pre vs post in/decrement is that to identify whether something is a "pre" vs "post" increment (i.e. to identify whether there should be no whitespace after or no whitespace before, the sniff needs to look at different tokens. These extra tests cover the changes I made to that logic.

Adding the same tests for pre-increment wouldn't actually test anything new as it was already handled correctly (as whether something is a plain variable or a non-static property access or array key access on a variable doesn't make a difference in the identification of pre vs post).

For pre-increment, I can think of a further/future iteration for the sniff - checking whether a pre-increment is used on a static property with a fully qualified classname or namespace relative classname, but that is something I choose not to handle (yet) when I made this change last year. When that change would be added, then, yes, extra tests would be needed for pre-in/decrement.

++\ClassName::$prop;
++Relative\ClassName::$prop;
--namespace\Relative\ClassName::$prop;

The reason I did not make that change (yet) is that this would need a different patch for PHPCS 3.x vs PHPCS 4.x, which would make the merge more complex. Also see #3041.

Choose a reason for hiding this comment

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

That all makes sense to me. Which makes this PR ready to merge as far as I'm concerned.

@jrfnl
Copy link
Contributor Author

jrfnl commented Dec 2, 2023

Closing as replaced by PHPCSStandards/PHP_CodeSniffer#46

@jrfnl jrfnl closed this Dec 2, 2023
@jrfnl jrfnl deleted the feature/generic-incrementdecrementspacing-handle-more-cases branch December 2, 2023 02:09
@jrfnl
Copy link
Contributor Author

jrfnl commented Dec 8, 2023

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Ready for Release
Development

Successfully merging this pull request may close these issues.

3 participants