Make WordPress Core


Ignore:
Timestamp:
08/27/2026 03:45:40 AM (3 days ago)
Author:
westonruter
Message:

Code Quality: Mark have_posts() and have_comments() as "impure" for PHPStan.

Both WP_Query::have_posts() and WP_Query::have_comments() advance the loop as a side effect, so two identical calls do not return the same value. PHPStan treated them as pure, concluded that a while ( have_posts() ) condition could never change, and reported the loops as always-true or always-false with the code past them unreachable. Marking those methods and their procedural wrappers in wp-includes/query.php as @phpstan-impure empties the while.alwaysTrue and while.alwaysFalse baselines, which are deleted along with their entries in phpstan.neon.dist, and trims deadCode.unreachable and if.alwaysTrue.

Two of the removed while.alwaysTrue entries were in Twenty_Fourteen_Ephemera_Widget::widget(), whose loop carries a real bug: $tmp_more was assigned inside the loop whose effects it exists to undo, so on every iteration after the first it captured the value the widget itself had just written, and the restore afterwards put back the secondary query's 0 rather than the caller's value. The assignment moves above the loop, while also guarding against the variable possibly being undefined.

Developed in https://github.com/WordPress/wordpress-develop/pull/13069.
Follow-up to r27124, r30085, r63023.

Props dpantazis, westonruter.
See #65817.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpstan.neon.dist

    r63351 r63357  
    8686        - tests/phpstan/baselines/varTag.noVariable.neon
    8787        - tests/phpstan/baselines/variable.undefined.neon
    88         - tests/phpstan/baselines/while.alwaysFalse.neon
    89         - tests/phpstan/baselines/while.alwaysTrue.neon
    9088        # phpstan:baselines end
    9189
Note: See TracChangeset for help on using the changeset viewer.