Skip to content

Conversation

@jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Jul 29, 2025

UserExperience/AdminBarRemoval: add additional tests

  • Testing against false positives for calls to methods or namespaced function calls (= the issue being addressed in this PR).
  • Safeguarding that function calls using PHP 5.6+ argument unpacking are flagged.
  • Safeguarding handling of PHP 8.0+ attribute class using the same name as a target function.
  • Safeguarding that the function is not flagged when used as a PHP 8.1+ first class callable.
  • Adding tests with more variations:
    • Non-lowercase function call(s).
    • Fully qualified function calls for the escaping functions.
    • Use PHP 7.3+ trailing comma's in a few function calls.
    • Text strings using single quotes and double quotes.

UserExperience/AdminBarRemoval: bug fix - disregard comments in parameter values

The PHPCSUtils PassedParameters::getParameters() return value includes a 'clean' array index, which contains the contents of the parameter stripped of surrounding whitespace and comments.

The AdminBarRemoval sniff uses the parameter contents in a couple of places to compare against a specific text string, but would break if the parameter value would contain a comment.

Fixed now. Includes tests.

UserExperience/AdminBarRemoval: add support for handling PHP 8.0+ function calls using named parameters

Includes tests.

UserExperience/AdminBarRemoval: bug fix - recognize add_action() as alias

... for add_filter().

Includes test.

UserExperience/AdminBarRemoval: add support for recognizing PHP 8.1+ first class callables

... when used as the $callback parameter for add_filter()/add_action().

Includes tests.

UserExperience/AdminBarRemoval: bug fix - function names are case-insensitive

... even when passed as callbacks.

Includes test.

UserExperience/AdminBarRemoval: remove some redundant logic

No need for a switch here.

UserExperience/AdminBarRemoval: bug fix - CSS file might be handled as PHP file

A number of IDEs will use STDIN to run PHPCS and will pass the stdin_path to PHPCS to set the file name, which often results in the filename being quoted.

In that case, the original logic in this sniff would break as the $file_extension would end up being 'css"', which doesn't match the expected 'css'.

The new PHPCSUtils 1.1.0 FilePath::getName() method will strip quotes from the file name, as well as normalize the slashes to forward (*nix) slashes.

Applying that method fixes the bug.

UserExperience/AdminBarRemoval: minor simplification

Let PHP sort out splitting the extension off the file name.

jrfnl added 9 commits July 29, 2025 21:53
* Testing against false positives for calls to methods or namespaced function calls (= the issue being addressed in this PR).
* Safeguarding that function calls using PHP 5.6+ argument unpacking are flagged.
* Safeguarding handling of PHP 8.0+ attribute class using the same name as a target function.
* Safeguarding that the function is not flagged when used as a PHP 8.1+ first class callable.
* Adding tests with more variations:
    - Non-lowercase function call(s).
    - Fully qualified function calls for the escaping functions.
    - Use PHP 7.3+ trailing comma's in a few function calls.
    - Text strings using single quotes and double quotes.
…eter values

The PHPCSUtils `PassedParameters::getParameters()` return value includes a `'clean'` array index, which contains the contents of the parameter stripped of surrounding whitespace and comments.

The `AdminBarRemoval` sniff uses the parameter contents in a couple of places to compare against a specific text string, but would break if the parameter value would contain a comment.

Fixed now. Includes tests.
…ction calls using named parameters

Includes tests.
… alias

... for `add_filter()`.

Includes test.
…first class callables

... when used as the `$callback` parameter for `add_filter()`/`add_action()`.

Includes tests.
…ensitive

... even when passed as callbacks.

Includes test.
…s PHP file

A number of IDEs will use STDIN to run PHPCS and will pass the `stdin_path` to PHPCS to set the file name, which often results in the filename being quoted.

In that case, the original logic in this sniff would break as the `$file_extension` would end up being `'css"'`, which doesn't match the expected `'css'`.

The new PHPCSUtils 1.1.0 `FilePath::getName()` method will strip quotes from the file name, as well as normalize the slashes to forward (*nix) slashes.

Applying that method fixes the bug.
Let PHP sort out splitting the extension off the file name.
@jrfnl jrfnl added this to the 3.1.0 milestone Jul 29, 2025
@jrfnl jrfnl requested a review from a team as a code owner July 29, 2025 19:57
@jrfnl jrfnl added Type: Bug Type: Enhancement Type: Maintenance PHPCSUtils The addition and utilisation of PHPCSUtils package labels Jul 29, 2025
@GaryJones GaryJones merged commit d9eb263 into develop Jul 30, 2025
42 checks passed
@GaryJones GaryJones deleted the feature/userexperience-adminbarremoval-various-tweaks branch July 30, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PHPCSUtils The addition and utilisation of PHPCSUtils package Type: Bug Type: Enhancement Type: Maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants