Fix #1311: Reduce false positives for loop variable prefix warnings#1349
Open
faisalahammad wants to merge 2 commits into
Open
Fix #1311: Reduce false positives for loop variable prefix warnings#1349faisalahammad wants to merge 2 commits into
faisalahammad wants to merge 2 commits into
Conversation
Filter out NonPrefixedVariableFound warnings for variables declared inside foreach as clauses and for loop initializers at top-level scope. WPCS treats these as global declarations but they are loop-local. Add multi-line loop header detection via line-content look-back. Covers foreach/for headers split across up to 5 lines. Props faisalahammad. Fixes WordPress#1311.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
faisalahammad
added a commit
to faisalahammad/plugin-check
that referenced
this pull request
Jun 5, 2026
Split the method into 3 smaller private helpers to stay under the phpmd NPath complexity threshold of 200. No behavioral change. - is_in_single_line_loop_header() handles single-line foreach/for - is_in_multiline_loop_header() handles multi-line look-back - loop_header_contains_variable() handles forward scan logic Refs WordPress#1311 Fixes WordPress#1349
Split is_variable_in_loop_control() into 3 smaller helpers to stay under phpmd NPath complexity threshold of 200. No behavioral change. - is_in_single_line_loop_header() handles single-line foreach/for - is_in_multiline_loop_header() handles multi-line look-back - loop_header_contains_variable() handles forward scan Refs WordPress#1311
243d666 to
13b0914
Compare
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.
Summary
Filter out
WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFoundfor variables declared insideforeachasclauses andforloop initializers at top-level scope. WPCS treats these as global declarations but they are loop-local.Fixes #1311
Changes
Prefixing_Check.php
Added
is_variable_in_loop_control()helper to detect when a variable sits inside a loop control structure. Covers:foreach ( ... as $var )andfor ( $i = 0; ... )foreach (/for (opens on previous line (up to 10-line look-back, walk-forward scan from opener to reported line)Uses file line cache for efficiency across multiple checks.
Test fixtures
Added
test-plugin-prefixing-foreach/load.phpwith 78 lines covering 7 cases:asand$varon different linesTests
Added
test_loop_variables_not_flagged()inPrefixing_Check_Tests.phpwith assertions for all 7 loop variable lines.Testing
Existing tests:
npm run test-php— 477 tests pass, 1355 assertions.Lint:
composer lint— clean.Static analysis:
composer phpstan— OK, no errors.