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

Move is_use_of_global_constant() utility method to dedicated ConstantsHelper #2248

Merged
merged 3 commits into from
Jun 14, 2023

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 14, 2023

Move is_use_of_global_constant() utility method to dedicated ConstantsHelper

The is_use_of_global_constant() utility method is only used by a small set of sniffs, so is better placed in a dedicated class.

This commit moves the is_use_of_global_constant() method to the new WordPressCS\WordPress\Helpers\ConstantsHelper class and starts using that class in the relevant sniff.

Related to #1465

The functionality in this new class will very likely be replaced at some point in the future by functions from PHPCSUtils.

This method will be tested via the WordPress.Security.EscapeOutput sniff (via pre-existing tests).

ConstantsHelper::is_use_of_global_constant(): simplify/implement PHPCSUtils

The $tokens_to_ignore array was set up using token types (strings) instead of token constants as some of the tokens would not always be available in all supported PHP/PHPCS combinations.

This is no longer needed now PHPCS 3.7.1 is the minimum supported PHPCS version.

This commit:

  • Changes the token types to token codes.
  • Simplifies the array by leveraging pre-existing token arrays.
    • T_CLASS, T_TRAIT and T_INTERFACE are replaced by Tokens::$ooScopeTokens, which also includes the PHP 8.1 T_ENUM token.
    • T_DOUBLE_COLON and T_OBJECT_OPERATOR are replaced by Collections::objectOperators(), which also includes the PHP 8.0 T_NULLSAFE_OBJECT_OPERATOR token.
    • T_PUBLIC, T_PROTECTED and T_PRIVATE are replaced by Tokens::$scopeModifiers.

The PHPCompatibility standard has the same method and the method is expected to be moved at some point to PHPCSUtils.
PHPCompatibility also has dedicated tests for this method, so I'm not adding tests at this time for T_ENUM and T_NULLSAFE_OBJECT_OPERATOR support.

See: PHPCompatibility/PHPCompatibility#1439

ConstantsHelper::is_use_of_global_constant(): minor simplification

A non-inline HTML token will always have another non-empty token before it, if nothing else, the PHP open tag, so checking if $prev is false is redundant.

…antsHelper`

The `is_use_of_global_constant()` utility method is only used by a small set of sniffs, so is better placed in a dedicated class.

This commit moves the `is_use_of_global_constant()` method to the new `WordPressCS\WordPress\Helpers\ConstantsHelper` class and starts using that class in the relevant sniff.

Related to 1465

The functionality in this new class will very likely be replaced at some point in the future by functions from PHPCSUtils.

This method will be tested via the `WordPress.Security.EscapeOutput` sniff (via pre-existing tests).
…SUtils

The `$tokens_to_ignore` array was set up using token types (strings) instead of token constants as some of the tokens would not always be available in all supported PHP/PHPCS combinations.

This is no longer needed now PHPCS 3.7.1 is the minimum supported PHPCS version.

This commit:
* Changes the token types to token codes.
* Simplifies the array by leveraging pre-existing token arrays.
    - `T_CLASS`, `T_TRAIT` and `T_INTERFACE` are replaced by `Tokens::$ooScopeTokens`, which also includes the PHP 8.1 `T_ENUM` token.
    - `T_DOUBLE_COLON` and `T_OBJECT_OPERATOR` are replaced by `Collections::objectOperators()`, which also includes the PHP 8.0 `T_NULLSAFE_OBJECT_OPERATOR` token.
    - `T_PUBLIC`, `T_PROTECTED` and `T_PRIVATE` are replaced by `Tokens::$scopeModifiers`.

The PHPCompatibility standard has the same method and the method is expected to be moved at some point to PHPCSUtils.
PHPCompatibility also has dedicated tests for this method, so I'm not adding tests at this time for `T_ENUM` and `T_NULLSAFE_OBJECT_OPERATOR` support.

See: PHPCompatibility/PHPCompatibility 1439
A non-inline HTML token will always have another non-empty token before it, if nothing else, the PHP open tag, so checking if `$prev` is `false` is redundant.
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.

@GaryJones GaryJones merged commit 5429cb0 into develop Jun 14, 2023
@GaryJones GaryJones deleted the feature/new-constantshelper-class branch June 14, 2023 21:18
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.

3 participants