Skip to content

Commit 8335bba

Browse files
authored
Narrow $callback type for register_activation/deactivation/uninstall_hook() (#401)
* Narrow $callback type for register_activation/deactivation/uninstall_hook() * Update ParameterTypeTest.php
1 parent 6ca7f78 commit 8335bba

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

functionMap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@
137137
'prep_atom_text_construct' => ["array{'html'|'text'|'xhtml', string}"],
138138
'previous_posts' => ['($display is true ? void : string)'],
139139
'rawurlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
140+
'register_activation_hook' => ['void', 'callback' => 'callable(bool): void'],
141+
'register_deactivation_hook' => ['void', 'callback' => 'callable(bool): void'],
140142
'register_nav_menus' => [null, 'locations' => 'array<string, string>'],
141143
'register_post_type' => [null, 'post_type' => 'lowercase-string&non-empty-string'],
144+
'register_uninstall_hook' => ['void', 'callback' => 'callable(): void'],
142145
'render_block_core_categories' => ['non-falsy-string'],
143146
'rest_authorization_required_code' => ['401|403'],
144147
'rest_sanitize_boolean' => ["(T is bool ? T : (T is ''|'false'|'FALSE'|'0'|0 ? false : true))", '@phpstan-template T' => 'of bool|string|int', 'value' => 'T', '@phpstan-pure' => ''],

tests/ParameterTypeTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ public function testCheckAjaxReferer(): void
155155
);
156156
}
157157

158+
public function testRegisterActivationHook(): void
159+
{
160+
$this->analyse(
161+
__DIR__ . '/data/param/register-activation-hook.php',
162+
[
163+
['Parameter #2 $callback of function register_activation_hook expects callable(bool): void, Closure(string): void given.', 10],
164+
['Parameter #2 $callback of function register_activation_hook expects callable(bool): void, Closure(bool): int given.', 11],
165+
]
166+
);
167+
}
168+
169+
public function testRegisterDeactivationHook(): void
170+
{
171+
$this->analyse(
172+
__DIR__ . '/data/param/register-deactivation-hook.php',
173+
[
174+
['Parameter #2 $callback of function register_deactivation_hook expects callable(bool): void, Closure(string): void given.', 10],
175+
['Parameter #2 $callback of function register_deactivation_hook expects callable(bool): void, Closure(bool): int given.', 11],
176+
]
177+
);
178+
}
179+
158180
public function testDoAction(): void
159181
{
160182
$this->analyse(
@@ -199,6 +221,17 @@ public function testRegisterPostType(): void
199221
);
200222
}
201223

224+
public function testRegisterUninstallHook(): void
225+
{
226+
$this->analyse(
227+
__DIR__ . '/data/param/register-uninstall-hook.php',
228+
[
229+
['Parameter #2 $callback of function register_uninstall_hook expects callable(): void, Closure(bool): void given.', 10],
230+
['Parameter #2 $callback of function register_uninstall_hook expects callable(): void, Closure(): int given.', 11],
231+
]
232+
);
233+
}
234+
202235
public function testWpdbGetRow(): void
203236
{
204237
$this->analyse(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // phpcs:disable
2+
3+
declare(strict_types=1);
4+
5+
use PhpStubs\WordPress\Core\Tests\Faker;
6+
7+
$file = Faker::string();
8+
9+
// Incorrect $callback
10+
register_activation_hook($file, static function (string $incorrect): void {}); // Incorrect parameter type
11+
register_activation_hook($file, static function (bool $correct): int {return Faker::int();}); // Incorrect return type
12+
13+
// Correct $callback
14+
register_activation_hook($file, static function (): void {});
15+
register_activation_hook($file, static function (bool $correct): void {});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // phpcs:disable
2+
3+
declare(strict_types=1);
4+
5+
use PhpStubs\WordPress\Core\Tests\Faker;
6+
7+
$file = Faker::string();
8+
9+
// Incorrect $callback
10+
register_deactivation_hook($file, static function (string $incorrect): void {}); // Incorrect parameter type
11+
register_deactivation_hook($file, static function (bool $correct): int {return Faker::int();}); // Incorrect return type
12+
13+
// Correct $callback
14+
register_deactivation_hook($file, static function (): void {});
15+
register_deactivation_hook($file, static function (bool $correct): void {});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php // phpcs:disable
2+
3+
declare(strict_types=1);
4+
5+
use PhpStubs\WordPress\Core\Tests\Faker;
6+
7+
$file = Faker::string();
8+
9+
// Incorrect $callback
10+
register_uninstall_hook($file, static function (bool $incorrect): void {}); // Incorrect number of parameters
11+
register_uninstall_hook($file, static function (): int {return Faker::int();}); // Incorrect return type
12+
13+
// Correct $callback
14+
register_uninstall_hook($file, static function (): void {});

wordpress-stubs.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129868,6 +129868,8 @@ function plugin_dir_url($file)
129868129868
*
129869129869
* @param string $file The filename of the plugin including the path.
129870129870
* @param callable $callback The function hooked to the 'activate_PLUGIN' action.
129871+
* @phpstan-param callable(bool): void $callback
129872+
* @phpstan-return void
129871129873
*/
129872129874
function register_activation_hook($file, $callback)
129873129875
{
@@ -129889,6 +129891,8 @@ function register_activation_hook($file, $callback)
129889129891
*
129890129892
* @param string $file The filename of the plugin including the path.
129891129893
* @param callable $callback The function hooked to the 'deactivate_PLUGIN' action.
129894+
* @phpstan-param callable(bool): void $callback
129895+
* @phpstan-return void
129892129896
*/
129893129897
function register_deactivation_hook($file, $callback)
129894129898
{
@@ -129918,6 +129922,7 @@ function register_deactivation_hook($file, $callback)
129918129922
* @param string $file Plugin file.
129919129923
* @param callable $callback The callback to run when the hook is called. Must be
129920129924
* a static method or function.
129925+
* @phpstan-param callable(): void $callback
129921129926
* @phpstan-return void
129922129927
*/
129923129928
function register_uninstall_hook($file, $callback)

0 commit comments

Comments
 (0)