Skip to content

Commit aa7533a

Browse files
authored
Narrow parameter type for register_widget() (#419)
1 parent 4d89b3a commit aa7533a

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
'register_nav_menus' => [null, 'locations' => 'array<string, string>'],
144144
'register_post_type' => [null, 'post_type' => 'lowercase-string&non-empty-string'],
145145
'register_uninstall_hook' => ['void', 'callback' => 'callable(): void'],
146+
'register_widget' => ['void', 'widget' => 'class-string<\WP_Widget>|\WP_Widget'],
146147
'render_block_core_categories' => ['non-falsy-string'],
147148
'rest_authorization_required_code' => ['401|403'],
148149
'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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ public function testRegisterUninstallHook(): void
252252
);
253253
}
254254

255+
public function testRegisterWidget(): void
256+
{
257+
$this->analyse(
258+
__DIR__ . '/data/param/register-widget.php',
259+
[
260+
['Parameter #1 $widget of function register_widget expects class-string<WP_Widget>|WP_Widget, PhpStubs\WordPress\Core\Tests\NoWidget given.', 21],
261+
['Parameter #1 $widget of function register_widget expects class-string<WP_Widget>|WP_Widget, PhpStubs\WordPress\Core\Tests\NoWidget given.', 22],
262+
['Parameter #1 $widget of function register_widget expects class-string<WP_Widget>|WP_Widget, string given.', 23],
263+
['Parameter #1 $widget of function register_widget expects class-string<WP_Widget>|WP_Widget, string given.', 24],
264+
]
265+
);
266+
}
267+
255268
public function testWpdbGetRow(): void
256269
{
257270
$this->analyse(
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php // phpcs:disable
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use WP_Widget;
8+
use function register_widget;
9+
10+
class MyWidget extends WP_Widget {
11+
public function __construct(string $id_base, string $name)
12+
{
13+
parent::__construct($id_base, $name);
14+
}
15+
}
16+
17+
class NoWidget {}
18+
19+
// Incorrect
20+
$noWidget = new NoWidget();
21+
register_widget($noWidget);
22+
register_widget(new NoWidget());
23+
register_widget(NoWidget::class);
24+
register_widget('\PhpStubs\WordPress\Core\Tests\NoWidget');
25+
26+
// Correct
27+
$widget = new MyWidget('my_widget', 'My Widget');
28+
register_widget($widget);
29+
register_widget(new MyWidget('my_widget', 'My Widget'));
30+
register_widget(MyWidget::class);
31+
register_widget('\PhpStubs\WordPress\Core\Tests\MyWidget');

wordpress-stubs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149029,6 +149029,8 @@ function wp_is_mobile()
149029149029
* @global WP_Widget_Factory $wp_widget_factory
149030149030
*
149031149031
* @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
149032+
* @phpstan-param class-string<\WP_Widget>|\WP_Widget $widget
149033+
* @phpstan-return void
149032149034
*/
149033149035
function register_widget($widget)
149034149036
{

0 commit comments

Comments
 (0)