Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Check the hook callback parameter count ?

オープン
#567 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
php
領域
tooling

調査の方向性

まず、既存の AlwaysReturnInFilter sniff と、リンクされた WordPress リファレンスに記載されている add_filter() および add_action() のエントリーポイントを確認します。sniff の範囲を定義する前に、オプションのパラメーターやクロージャを含め、コールバック宣言と accepted_args をどのように比較すべきかを明確にします。提供された違反例および非違反例について動作が仕様化されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
PHP
スター
261
フォーク
44
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Automattic/VIP-Coding-Standards のほかの issue

Automattic/VIP-Coding-Standards の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。