Skip to content

Fix #1311: Reduce false positives for loop variable prefix warnings#1349

Open
faisalahammad wants to merge 2 commits into
WordPress:trunkfrom
faisalahammad:fix/1311-loop-variable-false-positives
Open

Fix #1311: Reduce false positives for loop variable prefix warnings#1349
faisalahammad wants to merge 2 commits into
WordPress:trunkfrom
faisalahammad:fix/1311-loop-variable-false-positives

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Filter out WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound 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.

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:

  • Single-line foreach ( ... as $var ) and for ( $i = 0; ... )
  • Multi-line headers where 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.php with 78 lines covering 7 cases:

  • Single-line foreach (with and without key)
  • Single-line for
  • Multi-line foreach
  • Multi-line for
  • Multi-line foreach with split as and $var on different lines
  • Multi-line for with multi-init on separate lines
  • Prefixed helper function (still valid)

Tests

Added test_loop_variables_not_flagged() in Prefixing_Check_Tests.php with 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.

Open WordPress Playground Preview

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.
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: faisalahammad <faisalahammad@git.wordpress.org>
Co-authored-by: kmfoysal06 <kmfoysal06@git.wordpress.org>

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
@faisalahammad faisalahammad force-pushed the fix/1311-loop-variable-false-positives branch from 243d666 to 13b0914 Compare June 5, 2026 15:37
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.

Plugin check warning report should not Include global variable warning for a variable defined inside loop.

1 participant