Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if
評価
調査の方向性
Start in PreGetPostsSniff::isEarlyMainQueryCheck(), where the issue identifies the local findNext() search as stopping at the first statement terminator. Reproduce the minimal callback with do_log() before return and confirm the warning on the set() line. Done means the callback produces no warning when the early return is not the first statement in the if body.
索引モデルが issue の本文から書いたものです。
説明
Bug Description
The PreGetPosts sniff warns when a pre_get_posts callback changes the query without checking is_main_query() first. A callback that returns early for every other query has made that check, so it should not get the warning.
It does get the warning when the early return is not the first statement inside the if. Logging a message before returning is enough to cause it.
Expected: no warning for the snippet below.
Actual: a warning on the $query->set() line. Remove the do_log() line and the warning goes away.
The cause is in PreGetPostsSniff::isEarlyMainQueryCheck(). It looks for the return with findNext() in local mode, which stops at the first statement terminator, so only the first statement in the if body is searched.
@GaryJones found this while reviewing #901.
Minimal Code Snippet
add_action( 'pre_get_posts', function( $query ) {
if ( ! $query->is_main_query() ) {
do_log( 'x' );
return;
}
$query->set( 'cat', '-5' );
} );
Error Code
WordPressVIPMinimum.Hooks.PreGetPosts.PreGetPosts
Main WP_Query is being modified without
$query->is_main_query()check. Needs manual inspection.
Environment
| Question | Answer |
|---|---|
| PHP version | 8.5.11 |
| PHP_CodeSniffer version | 3.13.6 |
| PHPCSUtils version | 1.2.3 |
| VIPCS version | main at 9c47cd0 (3.1.0) and develop at bdd2160 |
| WordPressCS version | 3.4.1 |
| PHPCSExtra version | 1.5.1 |
| VariableAnalysis version | 2.13.0 |
Tested Against main branch?
- I have verified the issue still exists in the
mainbranch of VIPCS. - I have verified the issue still exists in the
developbranch of VIPCS.
- 主要言語
- PHP
- スター
- 261
- フォーク
- 44
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Automattic/VIP-Coding-Standards のほかの issue
-
AlwaysReturnInFilter: isInsideIfConditonal() guards the conditions array after reading it対応中かも @tomjn が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
Suppress filters in get_posts false positive対応中かも @tomjn が 5 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
-
Breaking Change Type: Maintenance
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
Automattic/VIP-Coding-Standards#849 · コメント 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
Automattic/VIP-Coding-Standards の issue をすべて見る
似ている issue
-
sync-en
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
sync-en
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
メンテナーはふだん 3 日以内に返信
-
Settings Support Rotation
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
woocommerce/woocommerce#69424 ·
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
woocommerce/woocommerce-paypal-payments#4779 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
thephpleague/commonmark#1159 ·