Skip to content

Commit 6ca7f78

Browse files
authored
Narrow $hook_name type in do_action/apply_filters (#400)
* Narrow $hook_name type in do_action/apply_filters * Update ParameterTypeTest.php
1 parent 65f4953 commit 6ca7f78

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

functionMap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
'add_users_page' => [null, 'callback' => "''|callable"],
5757
'addslashes_gpc' => ['($gpc is string ? string : array)', '@phpstan-pure' => ''],
5858
'antispambot' => [null, 'hex_encoding' => '0|1'],
59+
'apply_filters' => [null, 'hook_name' => 'non-empty-string'],
60+
'apply_filters_ref_array' => [null, 'hook_name' => 'non-empty-string'],
61+
'apply_filters_deprecated' => [null, 'hook_name' => 'non-empty-string'],
5962
'backslashit' => [null, '@phpstan-pure' => ''],
6063
'block_version' => ["(\$content is '' ? 0 : 0|1)", '@phpstan-pure' => ''],
6164
'bool_from_yn' => ["(\$yn is 'y' ? true : false)", '@phpstan-pure' => ''],
@@ -68,6 +71,9 @@
6871
'current_time' => ["(\$type is 'timestamp'|'U' ? int : string)"],
6972
'did_action' => ['int<0, max>'],
7073
'did_filter' => ['int<0, max>'],
74+
'do_action' => ['void', 'hook_name' => 'non-empty-string'],
75+
'do_action_ref_array' => ['void', 'hook_name' => 'non-empty-string'],
76+
'do_action_deprecated' => ['void', 'hook_name' => 'non-empty-string'],
7177
'edit_link' => ['int<0, max>'],
7278
'edit_term_link' => ['($display is true ? void : string|void)'],
7379
'get_approved_comments' => ["(\$args is array{count: true}&array ? int : (\$args is array{fields: 'ids'}&array ? array<int, int> : array<int, \WP_Comment>))"],

tests/ParameterTypeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ public function testAntispambot(): void
9999
);
100100
}
101101

102+
public function testApplyFilters(): void
103+
{
104+
$this->analyse(
105+
__DIR__ . '/data/param/apply-filters.php',
106+
[
107+
["Parameter #1 \$hook_name of function apply_filters expects non-empty-string, '' given.", 8],
108+
["Parameter #1 \$hook_name of function apply_filters_ref_array expects non-empty-string, '' given.", 9],
109+
["Parameter #1 \$hook_name of function apply_filters_deprecated expects non-empty-string, '' given.", 10],
110+
['Parameter #1 $hook_name of function apply_filters expects non-empty-string, string given.', 13],
111+
['Parameter #1 $hook_name of function apply_filters_ref_array expects non-empty-string, string given.', 14],
112+
['Parameter #1 $hook_name of function apply_filters_deprecated expects non-empty-string, string given.', 15],
113+
]
114+
);
115+
}
116+
102117
public function testBookmarks(): void
103118
{
104119
$field = "'link_category'|'link_description'|'link_id'|'link_image'|'link_name'|'link_notes'|'link_owner'|'link_rating'|'link_rel'|'link_rss'|'link_target'|'link_updated'|'link_url'|'link_visible'";
@@ -140,6 +155,21 @@ public function testCheckAjaxReferer(): void
140155
);
141156
}
142157

158+
public function testDoAction(): void
159+
{
160+
$this->analyse(
161+
__DIR__ . '/data/param/do-action.php',
162+
[
163+
["Parameter #1 \$hook_name of function do_action expects non-empty-string, '' given.", 8],
164+
["Parameter #1 \$hook_name of function do_action_ref_array expects non-empty-string, '' given.", 9],
165+
["Parameter #1 \$hook_name of function do_action_deprecated expects non-empty-string, '' given.", 10],
166+
['Parameter #1 $hook_name of function do_action expects non-empty-string, string given.', 13],
167+
['Parameter #1 $hook_name of function do_action_ref_array expects non-empty-string, string given.', 14],
168+
['Parameter #1 $hook_name of function do_action_deprecated expects non-empty-string, string given.', 15],
169+
]
170+
);
171+
}
172+
143173
public function testRegisterNavMenus(): void
144174
{
145175
$this->analyse(

tests/data/param/apply-filters.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
// Incorrect $hook_name
8+
apply_filters('', Faker::mixed(), Faker::mixed());
9+
apply_filters_ref_array('', []);
10+
apply_filters_deprecated('', [], '');
11+
12+
// Maybe incorrect $hook_name
13+
apply_filters(Faker::string(), Faker::mixed(), Faker::mixed());
14+
apply_filters_ref_array(Faker::string(), []);
15+
apply_filters_deprecated(Faker::string(), [], '');
16+
17+
// Correct $hook_name
18+
apply_filters(Faker::nonEmptyString(), Faker::mixed());
19+
apply_filters_ref_array(Faker::nonFalsyString(), []);
20+
apply_filters_deprecated('hook_name', [], '');

tests/data/param/do-action.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
// Incorrect $hook_name
8+
do_action('', Faker::mixed());
9+
do_action_ref_array('', []);
10+
do_action_deprecated('', [], '');
11+
12+
// Maybe incorrect $hook_name
13+
do_action(Faker::string(), Faker::mixed());
14+
do_action_ref_array(Faker::string(), []);
15+
do_action_deprecated(Faker::string(), [], '');
16+
17+
// Correct $hook_name
18+
do_action(Faker::nonEmptyString(), Faker::mixed());
19+
do_action_ref_array(Faker::nonFalsyString(), []);
20+
do_action_deprecated('hook_name', [], '');

wordpress-stubs.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129415,6 +129415,7 @@ function add_filter($hook_name, $callback, $priority = 10, $accepted_args = 1)
129415129415
* @param mixed $value The value to filter.
129416129416
* @param mixed ...$args Optional. Additional parameters to pass to the callback functions.
129417129417
* @return mixed The filtered value after all hooked functions are applied to it.
129418+
* @phpstan-param non-empty-string $hook_name
129418129419
*/
129419129420
function apply_filters($hook_name, $value, ...$args)
129420129421
{
@@ -129434,6 +129435,7 @@ function apply_filters($hook_name, $value, ...$args)
129434129435
* @param string $hook_name The name of the filter hook.
129435129436
* @param array $args The arguments supplied to the functions hooked to `$hook_name`.
129436129437
* @return mixed The filtered value after all hooked functions are applied to it.
129438+
* @phpstan-param non-empty-string $hook_name
129437129439
*/
129438129440
function apply_filters_ref_array($hook_name, $args)
129439129441
{
@@ -129610,6 +129612,7 @@ function add_action($hook_name, $callback, $priority = 10, $accepted_args = 1)
129610129612
* @param string $hook_name The name of the action to be executed.
129611129613
* @param mixed ...$arg Optional. Additional arguments which are passed on to the
129612129614
* functions hooked to the action. Default empty.
129615+
* @phpstan-param non-empty-string $hook_name
129613129616
* @phpstan-return void
129614129617
*/
129615129618
function do_action($hook_name, ...$arg)
@@ -129629,6 +129632,7 @@ function do_action($hook_name, ...$arg)
129629129632
*
129630129633
* @param string $hook_name The name of the action to be executed.
129631129634
* @param array $args The arguments supplied to the functions hooked to `$hook_name`.
129635+
* @phpstan-param non-empty-string $hook_name
129632129636
* @phpstan-return void
129633129637
*/
129634129638
function do_action_ref_array($hook_name, $args)
@@ -129766,6 +129770,7 @@ function did_action($hook_name)
129766129770
* @param string $replacement Optional. The hook that should have been used. Default empty.
129767129771
* @param string $message Optional. A message regarding the change. Default empty.
129768129772
* @return mixed The filtered value after all hooked functions are applied to it.
129773+
* @phpstan-param non-empty-string $hook_name
129769129774
*/
129770129775
function apply_filters_deprecated($hook_name, $args, $version, $replacement = '', $message = '')
129771129776
{
@@ -129786,6 +129791,7 @@ function apply_filters_deprecated($hook_name, $args, $version, $replacement = ''
129786129791
* @param string $version The version of WordPress that deprecated the hook.
129787129792
* @param string $replacement Optional. The hook that should have been used. Default empty.
129788129793
* @param string $message Optional. A message regarding the change. Default empty.
129794+
* @phpstan-param non-empty-string $hook_name
129789129795
* @phpstan-return void
129790129796
*/
129791129797
function do_action_deprecated($hook_name, $args, $version, $replacement = '', $message = '')

0 commit comments

Comments
 (0)