Check the hook callback parameter count ?
还没有人认领这个 Issue。
评估
调研方向
首先检查现有的 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_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
- 主要语言
- PHP
- 星标
- 261
- 派生
- 44
- 平均合并
- 3 天 17 小时
- 30 天内合并 PR
- 9
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Automattic/VIP-Coding-Standards 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 48/100
-
Breaking Change Type: Maintenance
难度 4/5 3-5 天 新手友好度 35/100
Automattic/VIP-Coding-Standards#849 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 45/100
-
难度 3/5 1-2 天 新手友好度 48/100
-
Status: Awaiting feedback
难度 2/5 1-3 小时 新手友好度 45/100
Automattic/VIP-Coding-Standards#754 · 3 条评论 ·
查看 Automattic/VIP-Coding-Standards 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
Automattic/blocks-engine#2803 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
hestiacp/hestiacp#5798 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
woocommerce/woocommerce-google-analytics-integration#699 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
wp-media/imagify-plugin#1289 ·
维护者通常 5 天内回复
-
Sidebar highlights two items when plugin routes share a prefix (/plugin/foo and /plugin/foo-bar)未关闭
难度 2/5 1-3 小时 新手友好度 72/100
getgrav/grav-plugin-admin2#195 ·