Check the hook callback parameter count ?
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
Bắt đầu bằng việc xem xét sniff AlwaysReturnInFilter hiện có và các entry point add_filter() và add_action() được ghi lại trong các tài liệu tham chiếu WordPress được liên kết. Làm rõ cách so sánh các khai báo callback và accepted_args, bao gồm các tham số tùy chọn và closure, trước khi xác định phạm vi của sniff. Công việc được hoàn tất khi hành vi đã được đặc tả cho các ví dụ vi phạm và không vi phạm được cung cấp.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- PHP
- Star
- 261
- Fork
- 44
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Automattic/VIP-Coding-Standards
-
AlwaysReturnInFilter: isInsideIfConditonal() guards the conditions array after reading itCó thể đã có người làm @tomjn đã nhận 4 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its ifCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 76/100
-
Suppress filters in get_posts false positiveCó thể đã có người làm @tomjn đã nhận 5 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
-
Breaking Change Type: Maintenance
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Automattic/VIP-Coding-Standards#849 · 1 bình luận ·
Tất cả issue của Automattic/VIP-Coding-Standards
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
customer-support needs-triage Platform(Default)
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
grokability/snipe-it#19767 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Code Quality] SfxDownloader extract path has no destination-writability pre-check for cached zipsĐang mởpriority:low type:refactor
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
crazy-goat/workerman-bundle#1052 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
sync-en
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày