Skip to content

Commit d866d5d

Browse files
authored
Add PHPStan template annotations for WP_Widget class (#160)
- Support passing the shape for the widget settings. - Add type for the widget arguments registered from `register_sidebar`.
1 parent 379f17a commit d866d5d

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

functionMap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,8 @@
131131
'get_bookmark' => ["null|(\$output is 'ARRAY_A' ? array<string, mixed> : (\$output is 'ARRAY_N' ? array<int, mixed> : \stdClass))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'"],
132132
'get_category' => ["(\$category is object ? array<array-key, mixed>|\WP_Term : array<array-key, mixed>|\WP_Term|\WP_Error|null) & (\$output is 'ARRAY_A' ? array<string, mixed>|\WP_Error|null : (\$output is 'ARRAY_N' ? array<int, mixed>|\WP_Error|null : \WP_Term|\WP_Error|null))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'"],
133133
'get_category_by_path' => ["(\$output is 'ARRAY_A' ? array<string, mixed>|\WP_Error|null : (\$output is 'ARRAY_N' ? array<int, mixed>|\WP_Error|null : \WP_Term|\WP_Error|null))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'"],
134+
'WP_Widget' => [null, '@phpstan-template' => 'T of array<string, mixed>'],
135+
'WP_Widget::form' => [null, 'instance' => 'T'],
136+
'WP_Widget::update' => [null, 'new_instance' => 'T', 'old_instance' => 'T'],
137+
'WP_Widget::widget' => [null, 'instance' => 'T', 'args' => 'array{name:string,id:string,description:string,class:string,before_widget:string,after_widget:string,before_title:string,after_title:string,before_sidebar:string,after_sidebar:string,show_in_rest:boolean,widget_id:string,widget_name:string}'],
134138
];

wordpress-stubs.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55912,6 +55912,7 @@ public function get_widget_key($id_base)
5591255912
*
5591355913
* @since 2.8.0
5591455914
* @since 4.4.0 Moved to its own file from wp-includes/widgets.php
55915+
* @phpstan-template T of array<string, mixed>
5591555916
*/
5591655917
#[\AllowDynamicProperties]
5591755918
class WP_Widget
@@ -55995,6 +55996,8 @@ class WP_Widget
5599555996
* @param array $args Display arguments including 'before_title', 'after_title',
5599655997
* 'before_widget', and 'after_widget'.
5599755998
* @param array $instance The settings for the particular instance of the widget.
55999+
* @phpstan-param T $instance
56000+
* @phpstan-param array{name:string,id:string,description:string,class:string,before_widget:string,after_widget:string,before_title:string,after_title:string,before_sidebar:string,after_sidebar:string,show_in_rest:boolean,widget_id:string,widget_name:string} $args
5599856001
*/
5599956002
public function widget($args, $instance)
5600056003
{
@@ -56012,6 +56015,8 @@ public function widget($args, $instance)
5601256015
* WP_Widget::form().
5601356016
* @param array $old_instance Old settings for this instance.
5601456017
* @return array Settings to save or bool false to cancel saving.
56018+
* @phpstan-param T $new_instance
56019+
* @phpstan-param T $old_instance
5601556020
*/
5601656021
public function update($new_instance, $old_instance)
5601756022
{
@@ -56023,6 +56028,7 @@ public function update($new_instance, $old_instance)
5602356028
*
5602456029
* @param array $instance Current settings.
5602556030
* @return string Default return is 'noform'.
56031+
* @phpstan-param T $instance
5602656032
*/
5602756033
public function form($instance)
5602856034
{

0 commit comments

Comments
 (0)