-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
hook-callback.php
95 lines (77 loc) · 2.32 KB
/
hook-callback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace SzepeViktor\PHPStan\WordPress\Tests;
use function add_filter;
use function add_action;
// phpcs:disable Squiz.NamingConventions.ValidFunctionName.NotCamelCaps,Squiz.NamingConventions.ValidVariableName.NotCamelCaps,Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
/**
* Incorrect usage:
*/
// Not a core filter, but callback is missing a return value
add_filter('not_a_core_filter', function() {});
// Core filter, callback is missing a return value
add_filter('post_class', function() {});
add_filter('post_class', function(array $classes) {});
// Not a core filter, accepted args are incorrect
add_filter('not_a_core_filter', function($value) {
return 123;
}, 10, 0);
add_filter('not_a_core_filter', function($value) {
return 123;
}, 10, 2);
add_filter('not_a_core_filter', function($value1, $value2) {
return 123;
});
// Core filter, callback is missing a return value
add_filter('post_class', function() {});
add_filter('post_class', function(array $classes) {});
/**
* Incorrect usage that's handled by PHPStan:
*
* These are here to ensure the rule doesn't trigger unwanted errors.
*/
// Too few parameters:
add_filter('post_class');
add_filter();
// Invalid callback:
add_filter('post_class','i_do_not_exist');
// Invalid parameters:
add_filter('post_class', function() {
return 123;
}, false);
add_filter('post_class', function() {
return 123;
}, 10, false);
/**
* Correct usage:
*/
// Not a core filter, but callback is ok
add_filter('not_a_core_filter', function() {
return 123;
}, 10, 0);
add_filter('not_a_core_filter', function() {
// We're allowing 0 parameters when `$accepted_args` is default value.
// This might change in the future to get more strict.
return 123;
});
add_filter('not_a_core_filter', function($value) {
return 123;
});
add_filter('not_a_core_filter', function($value) {
return 123;
}, 10, 1);
add_filter('not_a_core_filter', function($value1, $value2) {
return 123;
}, 10, 2);
// Various callback types
add_filter('not_a_core_filter', '__return_false');
add_filter('not_a_core_filter', __NAMESPACE__ . '\\filter_callback');
add_filter('not_a_core_filter', new TestInvokable(), 10, 2);
function filter_callback() {
return 123;
}
class TestInvokable {
public function __invoke($one, $two) {
return 123;
}
}