Skip to content

fix(NO-TASK): Repair SlowMetaQuery sniff under WPCS 3 - #41

Merged
aaronware merged 1 commit into
mainfrom
fix/implicit-nullable-stackptr
Aug 1, 2026
Merged

fix(NO-TASK): Repair SlowMetaQuery sniff under WPCS 3#41
aaronware merged 1 commit into
mainfrom
fix/implicit-nullable-stackptr

Conversation

@aaronware

Copy link
Copy Markdown
Contributor

Problem

SlowMetaQuerySniff calls three helpers that WordPressCS\WordPress\Sniff provided in WPCS 2 and that WPCS 3 removed in favour of PHPCSUtils. composer.json requires wp-coding-standards/wpcs: ^3.3.0, so all three are undefined at runtime and the sniff raises a fatal error the moment it sees a meta_query instead of reporting anything:

PHP Fatal error:  Uncaught Error: Call to undefined method
Linchpin\Sniffs\Performance\SlowMetaQuerySniff::find_array_open_close()
  at Linchpin/Sniffs/Performance/SlowMetaQuerySniff.php:129

Net effect: the sniff currently provides zero protection, and any consuming project that adds a meta_query gets a hard PHPCS crash rather than a warning.

Fix

Port the three removed calls to their PHPCSUtils equivalents (PHPCSUtils already ships as a WPCS 3 dependency):

WPCS 2 (removed) Replacement
find_array_open_close() Arrays::getOpenClose() — same opener/closer shape; its false return is now handled
strip_quotes() TextStrings::stripQuotes()
addMessage() MessageHelper::addMessage()

The last two are kept as thin protected shims so the sniff's existing call sites and message codes are untouched, keeping the diff small.

Also makes $stackPtr explicitly nullable in check_compare_value(). Implicit nullable parameters are deprecated as of PHP 8.4, and the notice fires on every phpcs run:

Deprecated: Linchpin\Sniffs\Performance\SlowMetaQuerySniff::check_compare_value():
Implicitly marking parameter $stackPtr as nullable is deprecated ... on line 283

Beyond the noise, that notice is written to stdout ahead of the report, which corrupts machine-readable formats — it breaks --report=json parsing.

Verification

⚠️ Verified with a real PHPCS run, not the test suite. tests/ cannot currently load WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff, so composer test errors with 2 errors / 0 assertions identically before and after this change. That broken harness is pre-existing and out of scope here, but it is how the WPCS 3 breakage went unnoticed — tests/fixtures/fail/meta-queries.php exists but never actually exercises the sniff. Worth a follow-up.

Running the sniff through a project with a working WPCS install:

Before — fatal, run aborted.

Aftercompare => 'LIKE':

 14 | WARNING | meta_query is using LIKE comparison, which is non-performant.
    |         | (Linchpin.Performance.SlowMetaQuery.nonperformant_comparison)

Nested relation clauses recurse and report per-clause as intended, and the deprecation notice is gone.

Note for reviewers

This restores a sniff that has been dead, so consuming projects may see new warnings on merge. The sniff warns on any comparison other than EXISTS/NOT EXISTS — including = — which is its existing design, not a change made here. Worth a heads-up in the release notes.

🤖 Generated with Claude Code

The sniff still calls three helpers that WordPressCS\WordPress\Sniff provided
in WPCS 2 and that WPCS 3 removed in favour of PHPCSUtils. Since composer.json
requires wp-coding-standards/wpcs ^3.3.0, every one of them is undefined at
runtime, so the sniff raises a fatal error the moment it encounters a
meta_query rather than reporting anything:

    PHP Fatal error: Uncaught Error: Call to undefined method
    Linchpin\Sniffs\Performance\SlowMetaQuerySniff::find_array_open_close()

Port the three calls to their PHPCSUtils equivalents:

- find_array_open_close() -> Arrays::getOpenClose(), which returns the same
  opener/closer shape. Its false return is now handled.
- strip_quotes()          -> TextStrings::stripQuotes()
- addMessage()            -> MessageHelper::addMessage()

The last two are kept as thin protected shims so the sniff's own call sites
and message-code arguments stay unchanged.

Also make $stackPtr explicitly nullable in check_compare_value(). Implicit
nullable parameters are deprecated as of PHP 8.4, and the notice was being
emitted on every phpcs run, which corrupts machine-readable report formats
that write to stdout.

Verified against a real PHPCS run rather than the test suite: tests/ cannot
currently load WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff,
so `composer test` errors identically before and after this change, which is
how the WPCS 3 breakage went unnoticed. Running the sniff through a project
with a working WPCS install, a meta_query using a LIKE comparison now reports

    meta_query is using LIKE comparison, which is non-performant.
    (Linchpin.Performance.SlowMetaQuery.nonperformant_comparison)

where it previously aborted the run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@aaronware
aaronware merged commit 6817e74 into main Aug 1, 2026
1 check passed
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.

1 participant