Check the hook callback parameter count ?
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 25/100
Direção de pesquisa
Comece revisando o sniff AlwaysReturnInFilter existente e os pontos de entrada add_filter() e add_action() documentados nas referências vinculadas do WordPress. Esclareça como as declarações de callback e accepted_args devem ser comparadas, incluindo parâmetros opcionais e closures, antes de definir o escopo do sniff. O trabalho estará concluído quando o comportamento estiver especificado para os exemplos fornecidos de violação e não violação.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
- Linguagem predominante
- PHP
- Estrelas
- 261
- Forks
- 44
- Merge médio
- 3d 9h
- PRs com merge (30d)
- 10
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de Automattic/VIP-Coding-Standards
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 48/100
Automattic/VIP-Coding-Standards#876 ·
Mantenedores costumam responder em até 1 dia
-
Breaking Change Type: Maintenance
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 35/100
Automattic/VIP-Coding-Standards#849 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 45/100
Automattic/VIP-Coding-Standards#832 ·
Mantenedores costumam responder em até 1 dia
-
WordPress.WP.AlternativeFunctions.file_system_operations error messages cannot be fixed anymoreAberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 48/100
Automattic/VIP-Coding-Standards#823 ·
Mantenedores costumam responder em até 1 dia
-
Status: Awaiting feedback
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 45/100
Automattic/VIP-Coding-Standards#754 · 3 comentários ·
Mantenedores costumam responder em até 1 dia
Todas as issues de Automattic/VIP-Coding-Standards
Issues semelhantes
-
bug ui
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
hashtopolis/server#2614 ·
Mantenedores costumam responder em até 1 dia
-
sync-en
Dificuldade 1/5 1-3 horas Facilidade para iniciantes 84/100
Mantenedores costumam responder em até 1 dia
-
sync-en
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 76/100
Mantenedores costumam responder em até 1 dia
-
Перевод устарел
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 62/100
-
bug-report bug-report-triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 69/100
Codeinwp/wp-maintenance-mode#596 · 1 comentário ·
Mantenedores costumam responder em até 1 dia