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

Core: add sniffs to check formatting of increment/decrement operators #2130

Merged
merged 2 commits into from
Dec 7, 2022

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 7, 2022

Core: add sniffs to check formatting of increment/decrement operators

  1. There should be no space between an increment/decrement operator and the variable it applies to.
  2. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements.
    “Pre” will in/decrement and then return, “post” will return and then in/decrement.
    Using the “pre” version is slightly more performant and can prevent future bugs when code gets moved around.

Refs:

Closes #1511

CS: clean up codebase for new rules / pre-increment

> 1. There should be no space between an increment/decrement operator and the variable it applies to.
> 2. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements.
>    “Pre” will in/decrement and then return, “post” will return and then in/decrement.
>    Using the “pre” version is slightly more performant and can prevent future bugs when code gets moved around.

Refs:
* https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Increment/decrement operators section
* https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#space-usage
* https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#increment-decrement-operators
* WordPress/wpcs-docs 112
* WordPress/WordPress-Coding-Standards 1511
* squizlabs/PHP_CodeSniffer 2172
* squizlabs/PHP_CodeSniffer 2174
* PHPCSStandards/PHPCSExtra 65

Closes 1511
Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

✅ Could be one of the noisier changes for 3.0.0.

@GaryJones GaryJones merged commit 409122a into develop Dec 7, 2022
@GaryJones GaryJones deleted the feature/core-add-increment-decrement-sniffs branch December 7, 2022 19:37
@jrfnl
Copy link
Member Author

jrfnl commented Dec 7, 2022

True enough, but as it has an auto-fixer, it's a one time switch-over for which I have the commit ready already.

@jrfnl
Copy link
Member Author

jrfnl commented Dec 8, 2022

For the record: in WP Core, the Universal.Operators.DisallowStandalonePostIncrementDecrement sniff yielded ~193 issues, which considering the total size of the codebase isn't actually that much ;-)

github-actions bot pushed a commit to gilzow/wordpress-performance that referenced this pull request Sep 9, 2023
…nts.

Note: This is enforced by WPCS 3.0.0:

1. There should be no space between an increment/decrement operator and the variable it applies to.
2. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements. “Pre” will in/decrement and then return, “post” will return and then in/decrement. Using the “pre” version is slightly more performant and can prevent future bugs when code gets moved around.

References:
* [https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#increment-decrement-operators WordPress PHP Coding Standards: Increment/decrement operators]
* [WordPress/WordPress-Coding-Standards#2130 WPCS: PR #2130 Core: add sniffs to check formatting of increment/decrement operators]

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56549


git-svn-id: https://core.svn.wordpress.org/trunk@56061 1a063a9b-81f0-0310-95a4-ce76da25c4cd
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Sep 9, 2023
…nts.

Note: This is enforced by WPCS 3.0.0:

1. There should be no space between an increment/decrement operator and the variable it applies to.
2. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements. “Pre” will in/decrement and then return, “post” will return and then in/decrement. Using the “pre” version is slightly more performant and can prevent future bugs when code gets moved around.

References:
* [https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#increment-decrement-operators WordPress PHP Coding Standards: Increment/decrement operators]
* [WordPress/WordPress-Coding-Standards#2130 WPCS: PR #2130 Core: add sniffs to check formatting of increment/decrement operators]

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56549


git-svn-id: http://core.svn.wordpress.org/trunk@56061 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PHPCS 3.4.0: Add new Generic.WhiteSpace.IncrementDecrementSpacing sniff
3 participants