Check the hook callback parameter count ?
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
Línea de trabajo
Empieza revisando el sniff AlwaysReturnInFilter existente y los puntos de entrada add_filter() y add_action() documentados en las referencias de WordPress enlazadas. Aclara cómo deberían compararse las declaraciones de callbacks y accepted_args, incluidos los parámetros opcionales y las closures, antes de definir el alcance del sniff. Se considera terminado cuando el comportamiento está especificado para los ejemplos proporcionados de violación y no violación.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
What problem would the enhancement address for VIP?
Prevent potential fatal ArgumentCount errors.
Describe the solution you'd like
Inspired by the AlwaysReturnInFilter sniff, I was thinking that once the abstract hook/callback sniff exists, it wouldn't be that hard to create a sniff which would check synchronicity between the parameters requested from a hook-in and the callback function declaration.
This would apply to both add_filter() as well as add_action().
Such a sniff would also be a candidate for (eventually moving to) WPCS, but I wanted to discuss the general idea here first, largely to see if the potential issue described above actually occurs in real life situations.
What code should be reported as a violation?
// Error: Two parameters expected, one (implicit) requested.
add_filter( 'the_title', function( $title, $id ) {
// Do something.
return $title;
} );
// Warning: One parameter expected, two requested.
add_filter( 'the_title', function( $title ) {
// Do something.
return $title;
}, 10, 2 );
What code should not be reported as a violation?
// OK: Two parameters expected, two requested.
add_filter( 'the_title', function( $title, $id ) {
// Do something.
return $title;
}, 10, 2 );
// OK: Three parameters expected, two requested, but third parameter is optional,
// which can be done by design to allow direct calls to the function to pass it.
function prefix_title_filter( $title, $id, $optional = null ) {
// Do something.
return $title;
}
add_filter( 'the_title', 'prefix_title_filter', 10, 2 );
Additional context
Future scope
- Once a list of WP native hooks + the amount of parameters they pass is known (via a trait in WPCS for instance), an additional check could be added that the
$accepted_argsparameter value is never higher than the number of passed parameters when the hook is called.
function prefix_title_filter( $title, $id, $optional = null ) {
// Do something.
return $title;
}
// The `the_title` filter only passes two arguments max, so the code here is an error.
add_filter( 'the_title', 'prefix_title_filter', 10, 3 );
- For PHP native functions used in callbacks, reflection could be used to see how many (required) parameters these take and match that with the
$accepted_argsparameter. - For WP native functions a list based solution would be needed to do the same.
Relevant links
- 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 4 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its ifPosiblemente ocupada @tomjn la tomó hace 4 días. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 76/100
-
Suppress filters in get_posts false positivePosiblemente ocupada @tomjn la tomó hace 5 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 ·
Todos los issues de Automattic/VIP-Coding-Standards
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
Awaiting Triage bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
product / databases
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
VilnaCRM-Org/user-service#525 ·
Los mantenedores suelen responder en 21 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
shukiv/jabali-panel#2029 ·
Los mantenedores suelen responder en 1 día