/
hook-callback.php
299 lines (238 loc) · 7.6 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?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:
*/
// Filter callback return statement is missing.
add_filter('filter', function() {});
// Filter callback return statement is missing.
add_filter('filter', function() {});
// Filter callback return statement is missing.
add_filter('filter', function(array $classes) {});
// Callback expects 1 parameter, $accepted_args is set to 0.
add_filter('filter', function($value) {
return 123;
}, 10, 0);
// Callback expects 1 parameter, $accepted_args is set to 2.
add_filter('filter', function($value) {
return 123;
}, 10, 2);
// Callback expects 0-1 parameters, $accepted_args is set to 2.
add_filter('filter', function($value = null) {
return 123;
}, 10, 2);
// Callback expects 2 parameters, $accepted_args is set to 1.
add_filter('filter', function($value1, $value2) {
return 123;
});
// Callback expects 2-4 parameter, $accepted_args is set to 1.
add_filter('filter', function($value1, $value2, $value3 = null, $value4 = null) {
return 123;
});
// Callback expects 2-3 parameter, $accepted_args is set to 4.
add_filter('filter', function($value1, $value2, $value3 = null) {
return 123;
}, 10, 4);
// Action callback returns true but should not return anything.
add_action('action', function() {
return true;
});
// Action callback returns true but should not return anything.
add_action('action', function($value) {
if ($value) {
return true;
}
});
// Filter callback return statement is missing.
add_filter('filter', __NAMESPACE__ . '\\no_return_value_typed');
// Action callback returns false but should not return anything.
add_action('action', '__return_false');
// Action callback returns int but should not return anything.
add_action('action', new TestInvokableTyped(), 10, 2);
// Callback expects at least 1 parameter, $accepted_args is set to 0.
add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed', 10, 0);
// Callback expects at least 1 parameter, $accepted_args is set to 0.
add_filter('filter', function( $one, ...$two ) {
return 123;
}, 10, 0);
// Callback expects 0-3 parameters, $accepted_args is set to 4.
add_filter('filter', function($one = null, $two = null, $three = null) {
return 123;
}, 10, 4);
// Action callback returns int but should not return anything.
add_action('action', __NAMESPACE__ . '\\return_value_typed');
// Callback expects 0 parameters, $accepted_args is set to 2.
add_filter('filter', '__return_false', 10, 2);
// Action callback returns false but should not return anything (with incorrect number of accepted args).
add_action('action', '__return_false', 10, 2);
// Filter callback return statement is missing.
add_filter('filter', __NAMESPACE__ . '\\no_return_value_untyped');
// Action callback returns mixed but should not return anything.
add_action('action', __NAMESPACE__ . '\\return_value_mixed');
// Action callback returns mixed but should not return anything.
add_action('action', __NAMESPACE__ . '\\return_value_implicit_mixed');
/**
* Incorrect usage that's handled by PHPStan:
*
* These are here to ensure the rule doesn't trigger unwanted errors.
*/
// Too few parameters:
add_filter('filter');
add_filter();
// Invalid callback:
add_filter('filter', 'i_do_not_exist');
// Invalid callback:
add_filter('filter', __NAMESPACE__ . '\\i_do_not_exist');
// Invalid callback:
add_filter('filter', [new TestClassTyped, 'i_do_not_exist']);
// Invalid callback:
add_filter('filter', new TestClassTyped);
// Unknown callback:
add_filter('filter', $_GET['callback']);
// Valid callback but with invalid parameters:
add_filter('filter', function() {
return 123;
}, false);
add_filter('filter', function() {
return 123;
}, 10, false);
// Filter callback return statement may be missing.
add_filter('filter', function($class) {
if ($class) {
return [];
}
});
// Incorrect function return type:
add_filter('filter', function(array $classes): array {
return 123;
});
// Callback function with no declared return type.
add_action('action', __NAMESPACE__ . '\\return_value_untyped');
/**
* Correct usage:
*/
add_filter('filter', function() {
return 123;
}, 10, 0);
add_filter('filter', function() {
// We're allowing 0 parameters when `$accepted_args` is default value of 1.
// This might change in the future to get more strict.
return 123;
});
add_filter('filter', function($value) {
return 123;
});
add_filter('filter', function($value) {
return 123;
}, 10, 1);
add_filter('filter', function($value1, $value2) {
return 123;
}, 10, 2);
add_filter('filter', function($value1, $value2, $value3 = null) {
return 123;
}, 10, 2);
add_filter('filter', function($value1, $value2, $value3 = null) {
return 123;
}, 10, 3);
add_filter('filter', function($value = null) {
return 123;
});
add_filter('filter', function($value = null) {
return 123;
}, 10, 0);
add_filter('filter', function($value = null) {
return 123;
}, 10, 1);
add_filter('filter', function($one = null, $two = null, $three = null) {
return 123;
});
// Action callbacks must return void
add_action('action', function() {
return;
});
add_action('action', function() {
});
add_action('action', function(): void {
});
add_action('action', __NAMESPACE__ . '\\no_return_value_typed');
// Filter callback may exit, unfortunately
add_filter('filter', function(array $classes) {
if ($classes) {
exit('Goodbye');
}
return $classes;
});
// Action callback may exit, unfortunately
add_action('action', function($result) {
if (! $result) {
exit('Goodbye');
}
});
// Various callback types
add_filter('filter', '__return_false');
add_filter('filter', __NAMESPACE__ . '\\return_value_mixed');
add_filter('filter', __NAMESPACE__ . '\\return_value_implicit_mixed');
add_filter('filter', __NAMESPACE__ . '\\return_value_mixed_union');
add_filter('filter', __NAMESPACE__ . '\\return_value_documented');
add_filter('filter', __NAMESPACE__ . '\\return_value_untyped');
add_filter('filter', __NAMESPACE__ . '\\return_value_typed');
add_filter('filter', new TestInvokableTyped(), 10, 2);
add_filter('filter', [new TestClassTyped, 'foo']);
$test_class = new TestClassTyped();
add_filter('filter', [$test_class, 'foo']);
// Variadic callbacks
add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed');
add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed', 10, 1);
add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed', 10, 2);
add_filter('filter', __NAMESPACE__ . '\\filter_variadic_typed', 10, 999);
/**
* Symbol definitions for use in these tests.
*/
function no_return_value_typed( $value ) : void {}
function return_value_typed() : int {
return 123;
}
function no_return_value_untyped( $value ) {}
/**
* @return mixed
*/
function return_value_mixed() {
return 123;
}
function return_value_implicit_mixed( $value ) {
return $value;
}
/**
* Return type documented as a union that includes mixed.
*
* @return int|mixed
*/
function return_value_mixed_union() {
return 123;
}
/**
* @return int
*/
function return_value_documented() {
return 123;
}
function return_value_untyped() {
return 123;
}
function filter_variadic_typed( $one, ...$two ) : int {
return 123;
}
class TestInvokableTyped {
public function __invoke($one, $two) : int {
return 123;
}
}
class TestClassTyped {
public function foo() : int {
return 123;
}
}