Skip to content

Commit 0a0bd47

Browse files
authored
Narrow parameter type for check_admin_referer and check_ajax_referer (#366)
1 parent 6bc5cb4 commit 0a0bd47

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

functionMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
'block_version' => ["(\$content is '' ? 0 : 0|1)", '@phpstan-pure' => ''],
5757
'bool_from_yn' => ["(\$yn is 'y' ? true : false)", '@phpstan-pure' => ''],
5858
'build_dropdown_script_block_core_categories' => ['non-falsy-string'],
59-
'check_admin_referer' => ['1|2|false', 'action' => '-1|string'],
60-
'check_ajax_referer' => ['1|2|false', 'action' => '-1|string'],
59+
'check_admin_referer' => ['1|2|false', 'action' => 'string'],
60+
'check_ajax_referer' => ['1|2|false', 'action' => 'string'],
6161
'comment_class' => ['($display is true ? void : string)'],
6262
'current_time' => ["(\$type is 'timestamp'|'U' ? int : string)"],
6363
'did_action' => ['int<0, max>'],

tests/ParameterTypeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ public function testBookmarks(): void
6969
);
7070
}
7171

72+
public function testCheckAdminReferer(): void
73+
{
74+
$this->analyse(
75+
__DIR__ . '/data/param/check_admin_referer.php',
76+
[
77+
['Parameter #1 $action of function check_admin_referer expects string, int given.', 10],
78+
['Parameter #1 $action of function check_admin_referer expects string, int given.', 11],
79+
]
80+
);
81+
}
82+
83+
public function testCheckAjaxReferer(): void
84+
{
85+
$this->analyse(
86+
__DIR__ . '/data/param/check_ajax_referer.php',
87+
[
88+
['Parameter #1 $action of function check_ajax_referer expects string, int given.', 10],
89+
['Parameter #1 $action of function check_ajax_referer expects string, int given.', 11],
90+
]
91+
);
92+
}
93+
7294
public function testWpdbGetRow(): void
7395
{
7496
$this->analyse(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function check_admin_referer;
8+
9+
// Incorrect action
10+
check_admin_referer(-1, Faker::string());
11+
check_admin_referer(Faker::int(), Faker::string());
12+
13+
// Correct action
14+
check_admin_referer(Faker::string(), Faker::string());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function check_ajax_referer;
8+
9+
// Incorrect action
10+
check_ajax_referer(-1, Faker::string());
11+
check_ajax_referer(Faker::int(), Faker::string());
12+
13+
// Correct action
14+
check_ajax_referer(Faker::string(), Faker::string());

wordpress-stubs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128746,7 +128746,7 @@ function auth_redirect()
128746128746
* @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
128747128747
* 2 if the nonce is valid and generated between 12-24 hours ago.
128748128748
* False if the nonce is invalid.
128749-
* @phpstan-param -1|string $action
128749+
* @phpstan-param string $action
128750128750
* @phpstan-return 1|2|false
128751128751
*/
128752128752
function check_admin_referer($action = -1, $query_arg = '_wpnonce')
@@ -128766,7 +128766,7 @@ function check_admin_referer($action = -1, $query_arg = '_wpnonce')
128766128766
* @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
128767128767
* 2 if the nonce is valid and generated between 12-24 hours ago.
128768128768
* False if the nonce is invalid.
128769-
* @phpstan-param -1|string $action
128769+
* @phpstan-param string $action
128770128770
* @phpstan-return 1|2|false
128771128771
*/
128772128772
function check_ajax_referer($action = -1, $query_arg = \false, $stop = \true)

0 commit comments

Comments
 (0)