Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
'add_users_page' => [null, 'callback' => "''|callable"],
'addslashes_gpc' => ['($gpc is string ? string : array)', '@phpstan-pure' => ''],
'antispambot' => [null, 'hex_encoding' => '0|1'],
'apply_filters' => [null, 'hook_name' => 'non-empty-string'],
'apply_filters_ref_array' => [null, 'hook_name' => 'non-empty-string'],
'apply_filters_deprecated' => [null, 'hook_name' => 'non-empty-string'],
'backslashit' => [null, '@phpstan-pure' => ''],
'block_version' => ["(\$content is '' ? 0 : 0|1)", '@phpstan-pure' => ''],
'bool_from_yn' => ["(\$yn is 'y' ? true : false)", '@phpstan-pure' => ''],
Expand All @@ -67,6 +70,9 @@
'current_time' => ["(\$type is 'timestamp'|'U' ? int : string)"],
'did_action' => ['int<0, max>'],
'did_filter' => ['int<0, max>'],
'do_action' => ['void', 'hook_name' => 'non-empty-string'],
'do_action_ref_array' => ['void', 'hook_name' => 'non-empty-string'],
'do_action_deprecated' => ['void', 'hook_name' => 'non-empty-string'],
'edit_link' => ['int<0, max>'],
'edit_term_link' => ['($display is true ? void : string|void)'],
'get_approved_comments' => ["(\$args is array{count: true}&array ? int : (\$args is array{fields: 'ids'}&array ? array<int, int> : array<int, \WP_Comment>))"],
Expand Down
30 changes: 30 additions & 0 deletions tests/ParameterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public function testAntispambot(): void
);
}

public function testApplyFilters(): void
{
$this->analyse(
__DIR__ . '/data/param/apply-filters.php',
[
["Parameter #1 \$hook_name of function apply_filters expects non-empty-string, '' given.", 8],
["Parameter #1 \$hook_name of function apply_filters_ref_array expects non-empty-string, '' given.", 9],
["Parameter #1 \$hook_name of function apply_filters_deprecated expects non-empty-string, '' given.", 10],
['Parameter #1 $hook_name of function apply_filters expects non-empty-string, string given.', 13],
['Parameter #1 $hook_name of function apply_filters_ref_array expects non-empty-string, string given.', 14],
['Parameter #1 $hook_name of function apply_filters_deprecated expects non-empty-string, string given.', 15],
]
);
}

public function testBookmarks(): void
{
$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'";
Expand Down Expand Up @@ -124,6 +139,21 @@ public function testCheckAjaxReferer(): void
);
}

public function testDoAction(): void
{
$this->analyse(
__DIR__ . '/data/param/do-action.php',
[
["Parameter #1 \$hook_name of function do_action expects non-empty-string, '' given.", 8],
["Parameter #1 \$hook_name of function do_action_ref_array expects non-empty-string, '' given.", 9],
["Parameter #1 \$hook_name of function do_action_deprecated expects non-empty-string, '' given.", 10],
['Parameter #1 $hook_name of function do_action expects non-empty-string, string given.', 13],
['Parameter #1 $hook_name of function do_action_ref_array expects non-empty-string, string given.', 14],
['Parameter #1 $hook_name of function do_action_deprecated expects non-empty-string, string given.', 15],
]
);
}

public function testRegisterNavMenus(): void
{
$this->analyse(
Expand Down
20 changes: 20 additions & 0 deletions tests/data/param/apply-filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

// Incorrect $hook_name
apply_filters('', Faker::mixed(), Faker::mixed());
apply_filters_ref_array('', []);
apply_filters_deprecated('', [], '');

// Maybe incorrect $hook_name
apply_filters(Faker::string(), Faker::mixed(), Faker::mixed());
apply_filters_ref_array(Faker::string(), []);
apply_filters_deprecated(Faker::string(), [], '');

// Correct $hook_name
apply_filters(Faker::nonEmptyString(), Faker::mixed());
apply_filters_ref_array(Faker::nonFalsyString(), []);
apply_filters_deprecated('hook_name', [], '');
20 changes: 20 additions & 0 deletions tests/data/param/do-action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

// Incorrect $hook_name
do_action('', Faker::mixed());
do_action_ref_array('', []);
do_action_deprecated('', [], '');

// Maybe incorrect $hook_name
do_action(Faker::string(), Faker::mixed());
do_action_ref_array(Faker::string(), []);
do_action_deprecated(Faker::string(), [], '');

// Correct $hook_name
do_action(Faker::nonEmptyString(), Faker::mixed());
do_action_ref_array(Faker::nonFalsyString(), []);
do_action_deprecated('hook_name', [], '');
6 changes: 6 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -129415,6 +129415,7 @@ function add_filter($hook_name, $callback, $priority = 10, $accepted_args = 1)
* @param mixed $value The value to filter.
* @param mixed ...$args Optional. Additional parameters to pass to the callback functions.
* @return mixed The filtered value after all hooked functions are applied to it.
* @phpstan-param non-empty-string $hook_name
*/
function apply_filters($hook_name, $value, ...$args)
{
Expand All @@ -129434,6 +129435,7 @@ function apply_filters($hook_name, $value, ...$args)
* @param string $hook_name The name of the filter hook.
* @param array $args The arguments supplied to the functions hooked to `$hook_name`.
* @return mixed The filtered value after all hooked functions are applied to it.
* @phpstan-param non-empty-string $hook_name
*/
function apply_filters_ref_array($hook_name, $args)
{
Expand Down Expand Up @@ -129610,6 +129612,7 @@ function add_action($hook_name, $callback, $priority = 10, $accepted_args = 1)
* @param string $hook_name The name of the action to be executed.
* @param mixed ...$arg Optional. Additional arguments which are passed on to the
* functions hooked to the action. Default empty.
* @phpstan-param non-empty-string $hook_name
* @phpstan-return void
*/
function do_action($hook_name, ...$arg)
Expand All @@ -129629,6 +129632,7 @@ function do_action($hook_name, ...$arg)
*
* @param string $hook_name The name of the action to be executed.
* @param array $args The arguments supplied to the functions hooked to `$hook_name`.
* @phpstan-param non-empty-string $hook_name
* @phpstan-return void
*/
function do_action_ref_array($hook_name, $args)
Expand Down Expand Up @@ -129766,6 +129770,7 @@ function did_action($hook_name)
* @param string $replacement Optional. The hook that should have been used. Default empty.
* @param string $message Optional. A message regarding the change. Default empty.
* @return mixed The filtered value after all hooked functions are applied to it.
* @phpstan-param non-empty-string $hook_name
*/
function apply_filters_deprecated($hook_name, $args, $version, $replacement = '', $message = '')
{
Expand All @@ -129786,6 +129791,7 @@ function apply_filters_deprecated($hook_name, $args, $version, $replacement = ''
* @param string $version The version of WordPress that deprecated the hook.
* @param string $replacement Optional. The hook that should have been used. Default empty.
* @param string $message Optional. A message regarding the change. Default empty.
* @phpstan-param non-empty-string $hook_name
* @phpstan-return void
*/
function do_action_deprecated($hook_name, $args, $version, $replacement = '', $message = '')
Expand Down