|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -add_filter( 'upload_mime', 'good_example_function' ); // Ok. |
4 |
| -add_filter( 'upload_mimesX', 'good_example_function' ); // Ok. |
5 |
| - |
6 |
| -// Warnings. |
7 |
| -add_filter( 'upload_mimes', 'bad_example_function' ); // Simple string. |
8 |
| -add_filter('upload_mimes' ,'bad_example_function'); // Incorrect spacing. |
9 |
| -add_filter( 'upload_mimes','bad_example_function'); // Incorrect spacing. |
10 |
| -add_filter( "upload_mimes" ,'bad_example_function'); // Double quotes. |
11 |
| -add_filter( 'upLoad_mimeS' ,'bad_example_function'); // Uppercase characters. |
12 |
| -add_filter( 'upload_' . 'mimes' ,'bad_example_function'); // Single concatenation. |
13 |
| -add_filter( 'upl' . 'oad_' . 'mimes' ,'bad_example_function'); // Multiple concatenation. |
14 |
| -add_filter( "upload_" . 'mimes' ,'bad_example_function'); // Single concatenation with double and single quotes. |
15 |
| -add_filter( 'upl' . "oad_" . "mimes" ,'bad_example_function'); // Multiple concatenation with double and single quotes. |
16 |
| -add_filter( 'upload_mimes', function() { // Anonymous callback. |
| 3 | +/* |
| 4 | + * Not the sniff target. |
| 5 | + */ |
| 6 | +use add_filter; |
| 7 | + |
| 8 | +my\ns\add_filter($a, $b); |
| 9 | +$this->add_action($a, $b); |
| 10 | +$this?->add_filter($a, $b); |
| 11 | +MyClass::add_action($a, $b); |
| 12 | +echo ADD_FILTER; |
| 13 | +namespace\add_action($a, $b); |
| 14 | + |
| 15 | + |
| 16 | +/* |
| 17 | + * These should all be okay. |
| 18 | + */ |
| 19 | +add_filter( 'not_target_hook', 'good_example_function' ); |
| 20 | +\add_filter( 'upload_mimesX' , 'good_example_function' ); |
| 21 | + |
| 22 | +add_action(...$params); // PHP 5.6 argument unpacking. |
| 23 | + |
| 24 | +// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute. |
| 25 | +#[ADD_FILTER('text')] |
| 26 | +function foo() {} |
| 27 | + |
| 28 | +// PHP 8.1 first class callable. |
| 29 | +// As we have no knowledge about what parameters will be passed, we shouldn't flag this. |
| 30 | +array_walk($filters, add_filter(...)); |
| 31 | + |
| 32 | +// Ignore as undetermined. |
| 33 | +Add_Filter( $hook_name, 'undetermined' ); |
| 34 | +\add_action( $obj->get_filterName(), 'undetermined' ); |
| 35 | +add_filter( MyClass::FILTER_NAME, 'undetermined', ); |
| 36 | +\add_filter( "upload_$mimes", 'undetermined' ); |
| 37 | + |
| 38 | +// Incomplete function call, should be ignored by the sniff. |
| 39 | +$incorrect_but_ok = add_filter(); |
| 40 | +$incorrect_but_ok = add_action(); |
| 41 | + |
| 42 | + |
| 43 | +/* |
| 44 | + * These should all be flagged with a warning. |
| 45 | + */ |
| 46 | +add_filter( 'do_robotstxt', 'bad_example_function' ); // Simple string. |
| 47 | +add_action('upload_mimes' , [$obj, 'method']); // Incorrect spacing. |
| 48 | +add_filter( 'robots_txt','bad_example_function'); // Incorrect spacing. |
| 49 | +\add_filter( "http_request_timeout" , fn($param) => $param * 10); // Double quotes. |
| 50 | + |
| 51 | +ADD_FILTER( 'upload_' . 'mimes','bad_example_function'); // Single concatenation. |
| 52 | +add_filter( 'upl' . 'oad_' . 'mimes','bad_example_function'); // Multiple concatenation. |
| 53 | +add_filter( "upload_" . 'mimes' , bad_example_function(...)); // Single concatenation with double and single quotes. |
| 54 | +add_filter( 'upl' . "oad_" . "mimes",'bad_example_function'); // Multiple concatenation with double and single quotes. |
| 55 | +\add_action( 'http_request_args', function() { // Anonymous callback. |
17 | 56 | // Do stuff.
|
18 | 57 | });
|
19 | 58 | add_action( 'upload_mimes', 'bad_example_function' ); // Check `add_action()`, which is an alias for `add_filter()`.
|
20 | 59 | add_filter( 'http_request_timeout', 'bad_example_function' ); // Simple string.
|
21 | 60 | add_filter('http_request_args', 'bad_example_function' ); // Simple string + incorrect spacing.
|
22 |
| -add_action( 'do_robotstxt', 'my_do_robotstxt'); // Simple string. |
| 61 | +add_action( /*comment*/ 'do_robotstxt', 'my_do_robotstxt'); // Simple string. |
23 | 62 | add_filter( 'robots_txt', function() { // Anonymous callback.
|
24 | 63 | } );
|
| 64 | + |
| 65 | +// Safeguard correct handling of function calls using PHP 8.0+ named parameters. |
| 66 | +add_action(callback: 'invalid', priority: 10); // OK, well, not really, missing required $hook_name param, but that's not the concern of this sniff. |
| 67 | +add_action(callback: 'do_robotstxt', hook_name: 'not_our_target'); // OK. |
| 68 | +add_action(hookName: 'not_our_target', callback: 'do_robotstxt',); // OK, well, not really, typo in param name, but that's not the concern of the sniff. |
| 69 | + |
| 70 | +add_filter(priority: 10, hook_name: 'robots_txt', callback: some_function(...) ); // Warning. |
| 71 | + |
| 72 | +// Hook names are case-sensitive. |
| 73 | +add_filter( 'upLoad_mimeS' , $callback); // OK, not our target. |
| 74 | + |
| 75 | +// Bug fix - spacing vs concatenation. |
| 76 | +add_filter('do_' . 'robots' . 'txt', 'bad_example_function'); // Warning. |
| 77 | + |
| 78 | +// Ignore partially dynamic hook names. |
| 79 | +add_filter( 'robots_' . $something . 'txt' , $callback); // OK, ignored as undetermined. |
| 80 | +add_filter( 'http_request_timeout' . $something, $callback); // OK, ignored as undetermined. |
| 81 | + |
| 82 | +// Ensure quote stripping is done correctly. |
| 83 | +add_filter( 'upload"_mimes', 'bad_example_function' ); // OK, not a filter we're looking for. |
| 84 | +add_filter( "upload_'mimes", 'bad_example_function' ); // OK, not a filter we're looking for. |
0 commit comments