Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 76/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- PHP
- Étoiles
- 261
- Forks
- 44
- Merge moyen
- 3 j 9 h
- PR mergées (30 j)
- 10
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de Automattic/VIP-Coding-Standards
-
Difficulté 3/5 1-2 jours Accessibilité débutants 48/100
Automattic/VIP-Coding-Standards#876 ·
Les mainteneurs répondent en général sous 1 jour
-
Breaking Change Type: Maintenance
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
Automattic/VIP-Coding-Standards#849 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 45/100
Automattic/VIP-Coding-Standards#832 ·
Les mainteneurs répondent en général sous 1 jour
-
WordPress.WP.AlternativeFunctions.file_system_operations error messages cannot be fixed anymoreOuverte
Difficulté 3/5 1-2 jours Accessibilité débutants 48/100
Automattic/VIP-Coding-Standards#823 ·
Les mainteneurs répondent en général sous 1 jour
-
Status: Awaiting feedback
Difficulté 2/5 1-3 heures Accessibilité débutants 45/100
Automattic/VIP-Coding-Standards#754 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de Automattic/VIP-Coding-Standards
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
-
0. Needs triage 36-feedback bug
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
Les mainteneurs répondent en général sous 1 jour
-
area:ci priority:low type:chore
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
crazy-goat/php-fpm-ng#1021 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
ProfessionalWiki/NeoWiki#1755 ·
Les mainteneurs répondent en général sous 1 jour