Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 76/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- PHP
- Estrellas
- 261
- Forks
- 44
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Automattic/VIP-Coding-Standards
-
AlwaysReturnInFilter: isInsideIfConditonal() guards the conditions array after reading itPosiblemente ocupada @tomjn la tomó hace 6 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Suppress filters in get_posts false positivePosiblemente ocupada @tomjn la tomó hace 7 días. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
-
Breaking Change Type: Maintenance
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
Automattic/VIP-Coding-Standards#849 · 1 comentario ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
Todos los issues de Automattic/VIP-Coding-Standards
Issues similares
-
extension/Commercial needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 2 días
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
crazy-goat/rabbit-stream#753 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
opensourcepos/opensourcepos#4743 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
OpenConext/OpenConext-engineblock#2129 ·
Los mantenedores suelen responder en 3 días
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
grokability/snipe-it#19786 ·
Los mantenedores suelen responder en 1 día