Skip to content

Commit

Permalink
Add PHPStan template annotations for WP_Widget class (#160)
Browse files Browse the repository at this point in the history
- Support passing the shape for the widget settings.
- Add type for the widget arguments registered from `register_sidebar`.
  • Loading branch information
lipemat authored Apr 14, 2024
1 parent 379f17a commit d866d5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@
'get_bookmark' => ["null|(\$output is 'ARRAY_A' ? array<string, mixed> : (\$output is 'ARRAY_N' ? array<int, mixed> : \stdClass))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'"],
'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'"],
'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'"],
'WP_Widget' => [null, '@phpstan-template' => 'T of array<string, mixed>'],
'WP_Widget::form' => [null, 'instance' => 'T'],
'WP_Widget::update' => [null, 'new_instance' => 'T', 'old_instance' => 'T'],
'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}'],
];
6 changes: 6 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -55912,6 +55912,7 @@ public function get_widget_key($id_base)
*
* @since 2.8.0
* @since 4.4.0 Moved to its own file from wp-includes/widgets.php
* @phpstan-template T of array<string, mixed>
*/
#[\AllowDynamicProperties]
class WP_Widget
Expand Down Expand Up @@ -55995,6 +55996,8 @@ class WP_Widget
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance The settings for the particular instance of the widget.
* @phpstan-param T $instance
* @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
*/
public function widget($args, $instance)
{
Expand All @@ -56012,6 +56015,8 @@ public function widget($args, $instance)
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
* @return array Settings to save or bool false to cancel saving.
* @phpstan-param T $new_instance
* @phpstan-param T $old_instance
*/
public function update($new_instance, $old_instance)
{
Expand All @@ -56023,6 +56028,7 @@ public function update($new_instance, $old_instance)
*
* @param array $instance Current settings.
* @return string Default return is 'noform'.
* @phpstan-param T $instance
*/
public function form($instance)
{
Expand Down

0 comments on commit d866d5d

Please sign in to comment.