Make WordPress Core


Ignore:
Timestamp:
08/27/2026 03:45:40 AM (27 hours 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/src/wp-includes/query.php

    r62664 r63357  
    957957 * @global WP_Query $wp_query WordPress Query object.
    958958 *
     959 * @phpstan-impure
     960 *
    959961 * @return bool True if posts are available, false if end of the loop.
    960962 */
     
    10361038 *
    10371039 * @global WP_Query $wp_query WordPress Query object.
     1040 *
     1041 * @phpstan-impure
    10381042 *
    10391043 * @return bool True if comments are available, false if no more comments.
Note: See TracChangeset for help on using the changeset viewer.