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

New WordPress.WP.DiscouragedConstants sniff #1089

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Aug 5, 2017

This sniff is based on a sniff previously pulled to the TRT fork of WPCS as Theme.DeprecatedConstants.

Related issue: WPTT/WPThemeReview/issues/23
Original PR: WPTT/WPThemeReview/pull/30
Covers the list of constants found in: WordPress/theme-check/pull/162

Differences between that sniff and this one:

  • This sniff is a lot more comprehensive in preventing false positives.
    The original sniff was one of the first ones I wrote and it's kind of sweet to look back at that code which I wrote over a year ago and see how much I've learned about writing sniffs in the mean time.
  • The original sniff was called Theme.DeprecatedConstants, but in reality, most of these constants aren't officially deprecated, though their usage is discouraged and the constants being addressed are not 100% related to themes either. With that in mind, I've renamed the sniff to WP.DiscouragedConstants and changed the error level from Error to Warning.
    Also see: https://core.trac.wordpress.org/ticket/18298
  • This version of the sniff also addresses the concerns raised in Restrict defining deprecated define variables WPTT/WPThemeReview#110 about themes define-ing any of these constants and leverages the AbstractFunctionParameterSniff to do so.

Includes quite extensive unit tests. More are definitely welcome, especially to prevent false positives.

I've added the sniff to the WordPress-Extra ruleset.

Based on https://vip.wordpress.com/documentation/code-review-what-we-look-for/#using-theme-constants and https://core.trac.wordpress.org/ticket/18298#comment:2, I've also added the sniff to the WordPress-VIP ruleset. /cc @sboisvert @david-binda @ntwb

Fixes #97
Will also fix WPTT/WPThemeReview/issues/110 once the original sniff gets removed from the TRT fork and this sniff is added to the TRT ruleset instead.


Update: This PR also adds two new utility methods to the WordPress\Sniff class: is_class_constant() and valid_direct_scope().

This sniff is based on a sniff previously pulled to the TRT fork of WPCS as `Theme.DeprecatedConstants`.

Related issue: WPTRT/WordPress-Coding-Standards/issues/ 23
Original PR: WPTRT/WordPress-Coding-Standards/pull/ 30
Covers the list of constants found in: Otto42/theme-check/pull/ 162

Differences between that sniff and this one:
* This sniff is a **_lot_** more comprehensive in preventing false positives.
    The original sniff was one of the first ones I wrote and it's kind of sweet to look back at that code which I wrote over a year ago and see how much I've learned about writing sniffs in the mean time.
* The original sniff was called `Theme.DeprecatedConstants`, but in reality, most of these constants aren't officially deprecated, though their usage is discouraged and the constants being addressed are not 100% related to themes either. With that in mind, I've renamed the sniff to `WP.DiscouragedConstants` and changed the error level from Error to Warning.
    Also see: https://core.trac.wordpress.org/ticket/18298
* This version of the sniff also addresses the concerns raised in WPTRT/WordPress-Coding-Standards/issues/ 110 about themes `define`-ing any of these constants and leverages the `AbstractFunctionParameterSniff` to do so.

Includes quite extensive unit tests. More are definitely welcome, especially to prevent false positives.

I've added the sniff to the `WordPress-Extra` ruleset.

Based on https://vip.wordpress.com/documentation/code-review-what-we-look-for/#using-theme-constants and https://core.trac.wordpress.org/ticket/18298#comment:2, I've also added the sniff to the `WordPress-VIP` ruleset. /cc @sboisvert @david-binda @ntwb

Fixes 97
Will also fix WPTRT/WordPress-Coding-Standards/issues/ 110 once the original sniff gets removed from the TRT fork and this sniff is added to the TRT ruleset instead.
@jrfnl jrfnl added this to the 0.14.0 milestone Aug 5, 2017
@jrfnl jrfnl changed the title ✨ New WordPress.WP.DiscouragedConstants sniff New WordPress.WP.DiscouragedConstants sniff Aug 5, 2017
Copy link
Contributor

@JDGrimes JDGrimes left a comment

Choose a reason for hiding this comment

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

One question, other than that looks great.

class DEF implements STYLESHEETPATH {}
interface STYLESHEETPATH {}
trait STYLESHEETPATH {}
const STYLESHEETPATH = 'something';
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this OK, if calling define() is not OK? I'd think that if we sniff for define(), we ought to also sniff for const outside of a class or namespace.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I added a lot of code to prevent false positives and only addressed the define() bit after that. Clearly forgot to also change things for const.

I'm going to change this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, see separate commit.

…mespace

Distinguished between `const` defines in the global namespace and class constants.

To this end, two more utility functions I previously write for PHPCompatibility have been ported over.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restrict defining deprecated define variables Flag use of TEMPLATEPATH and STYLESHEETPATH
3 participants