Skip to content

Conversation

@jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Jul 30, 2025

Performance/CacheValueOverride: rename test case file

Preliminary commit to allow for adding additional test case files.

Performance/CacheValueOverride: extend AbstractFunctionRestrictionsSniff

As things were, the determination of whether or not a T_STRING is a call to the global WP native wp_cache_get() function was severely flawed.

By switching the sniff over to be based on the WordPressCS AbstractFunctionRestrictionsSniff class, this flaw is mitigated.

This flaw did not lead to false positive due to the subsequent token walking being very rigid.

Includes adding a slew of additional tests to document the sniff behaviour.

Additionally, the tests have been made more comprehensive and varied by:

  • Testing against false positives for calls to methods or namespaced function calls (= the issue being addressed in this PR).
  • Testing against false positives for attribute class using the same name as the function.
  • Ensure function import use statements are not flagged. We're not interested in those.
  • Adding more variations to the pre-existing tests:
    • Non-lowercase function call(s).

Performance/CacheValueOverride: bug fix - false negative with fully qualified function call

The isVariableAssignment() method walks back from the function call to see if the result is being assigned, but did not take fully qualified function calls into account.

Fixed now.

Includes test.

Performance/CacheValueOverride: bug fix - false positive for PHP 8.1+ first class callable assignment

If a PHP 8.1+ first class callable is assigned to a variable, the assignment captures the callable, not the result of calling the callable.

This commit adds some dedicated code to handle this better and prevent the false positive.

Includes tests.

Performance/CacheValueOverride: bug fix - false positives for variables in different scopes

As things were, the searching for the "next use of the variable to which the cache was assigned" did not limit itself to the current variable scope.

In effect, that meant that "function local" variables could be mistaken for global variables and visa versa, as demonstrated by the new tests.

Fixed now.

Includes tests.

Performance/CacheValueOverride: add some more defensive coding

... to guard against the sniff throwing PHP notices/warnings due to parse errors/live coding.

Any such PHP notice/warning stops the PHPCS run for a file, so should be avoided.

Includes tests.

Related to #530

jrfnl added 6 commits July 30, 2025 04:20
Preliminary commit to allow for adding additional test case files.
As things were, the determination of whether or not a `T_STRING` is a call to the global WP native `wp_cache_get()` function was severely flawed.

By switching the sniff over to be based on the WordPressCS `AbstractFunctionRestrictionsSniff` class, this flaw is mitigated.

This flaw did not lead to false positive due to the subsequent token walking being very rigid.

Includes adding a slew of additional tests to document the sniff behaviour.

Additionally, the tests have been made more comprehensive and varied by:
* Testing against false positives for calls to methods or namespaced function calls (= the issue being addressed in this PR).
* Testing against false positives for attribute class using the same name as the function.
* Ensure function import `use` statements are not flagged. We're not interested in those.
* Adding more variations to the pre-existing tests:
    - Non-lowercase function call(s).
…ualified function call

The `isVariableAssignment()` method walks back from the function call to see if the result is being assigned, but did not take fully qualified function calls into account.

Fixed now.

Includes test.
… first class callable assignment

If a PHP 8.1+ first class callable is assigned to a variable, the assignment captures the callable, not the result of calling the callable.

This commit adds some dedicated code to handle this better and prevent the false positive.

Includes tests.
…es in different scopes

As things were, the searching for the "next use of the variable to which the cache was assigned" did not limit itself to the current variable scope.

In effect, that meant that "function local" variables could be mistaken for global variables and visa versa, as demonstrated by the new tests.

Fixed now.

Includes tests.
... to guard against the sniff throwing PHP notices/warnings due to parse errors/live coding.

Any such PHP notice/warning stops the PHPCS run for a file, so should be avoided.

Includes tests.
@jrfnl jrfnl added this to the 3.1.0 milestone Jul 30, 2025
@jrfnl jrfnl requested a review from a team as a code owner July 30, 2025 02:23
@jrfnl jrfnl added Type: Bug Type: Maintenance Type: False positive PHPCSUtils The addition and utilisation of PHPCSUtils package labels Jul 30, 2025
@GaryJones GaryJones merged commit 45bf583 into develop Jul 30, 2025
42 checks passed
@GaryJones GaryJones deleted the feature/performance-cachevalueoverride-initial-improvements branch July 30, 2025 07:05
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: False positive Type: Maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants