fix(NO-TASK): Repair SlowMetaQuery sniff under WPCS 3 - #41
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SlowMetaQuerySniffcalls three helpers thatWordPressCS\WordPress\Sniffprovided in WPCS 2 and that WPCS 3 removed in favour of PHPCSUtils.composer.jsonrequireswp-coding-standards/wpcs: ^3.3.0, so all three are undefined at runtime and the sniff raises a fatal error the moment it sees ameta_queryinstead of reporting anything:Net effect: the sniff currently provides zero protection, and any consuming project that adds a
meta_querygets 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):
find_array_open_close()Arrays::getOpenClose()— sameopener/closershape; itsfalsereturn is now handledstrip_quotes()TextStrings::stripQuotes()addMessage()MessageHelper::addMessage()The last two are kept as thin
protectedshims so the sniff's existing call sites and message codes are untouched, keeping the diff small.Also makes
$stackPtrexplicitly nullable incheck_compare_value(). Implicit nullable parameters are deprecated as of PHP 8.4, and the notice fires on everyphpcsrun:Beyond the noise, that notice is written to stdout ahead of the report, which corrupts machine-readable formats — it breaks
--report=jsonparsing.Verification
tests/cannot currently loadWordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff, socomposer testerrors 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.phpexists 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.
After —
compare => 'LIKE':Nested
relationclauses 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