Check the hook callback parameter count ?

Aberta
#567 0 comentários 1 reação 0 responsáveis Ver no GitHub

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
Tipo de issue
Funcionalidade
Clareza
Precisa de esclarecimento
Status de atividade
Estagnada
Stack de tecnologia
php
Domínio
tooling

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_args parameter 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_args parameter.
  • 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

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de Automattic/VIP-Coding-Standards

Todas as issues de Automattic/VIP-Coding-Standards

Issues semelhantes

Mais issues de PHP

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.