Skip to content

Commit 8aa9d83

Browse files
authored
PHPStan level 9 (#80)
1 parent ce571c3 commit 8aa9d83

3 files changed

Lines changed: 49 additions & 13 deletions

File tree

phpstan.neon.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- widget-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
scanFiles:
9+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
treatPhpDocTypesAsCertain: false
11+
ignoreErrors:
12+
- identifier: missingType.parameter
13+
- identifier: missingType.return

src/Sidebar_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
class Sidebar_Command extends WP_CLI_Command {
2020

21+
/**
22+
* @var string[]
23+
*/
2124
private $fields = [
2225
'name',
2326
'id',

src/Widget_Command.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
*/
3535
class Widget_Command extends WP_CLI_Command {
3636

37+
/**
38+
* @var string[]
39+
*/
3740
private $fields = [
3841
'name',
3942
'id',
@@ -156,7 +159,7 @@ public function add( $args, $assoc_args ) {
156159
}
157160
unset( $option_keys['_multiwidget'] );
158161
$option_keys = array_keys( $option_keys );
159-
$last_key = array_pop( $option_keys );
162+
$last_key = (int) array_pop( $option_keys );
160163
$option_index = $last_key + 1;
161164
$widget_options[ $option_index ] = $this->sanitize_widget_options( $name, $assoc_args, array() );
162165
$this->update_widget_options( $name, $widget_options );
@@ -624,7 +627,7 @@ private function validate_sidebar_widget( $widget_id ) {
624627
* Gets the widgets (and their associated data) for a given sidebar
625628
*
626629
* @param string $sidebar_id
627-
* @return array
630+
* @return list<object{name: string, id: string, position: int, options: array<string, mixed>}&\stdClass>
628631
*/
629632
private function get_sidebar_widgets( $sidebar_id ) {
630633

@@ -640,24 +643,33 @@ private function get_sidebar_widgets( $sidebar_id ) {
640643
$prepared_widget = new stdClass();
641644

642645
$parts = explode( '-', $widget_id );
643-
$option_index = array_pop( $parts );
646+
$option_index = (string) array_pop( $parts );
644647
$widget_name = implode( '-', $parts );
645648

646649
$prepared_widget->name = $widget_name;
647650
$prepared_widget->id = $widget_id;
648651
$prepared_widget->position = $key + 1;
649-
$widget_options = get_option( 'widget_' . $widget_name );
650-
$prepared_widget->options = $widget_options[ $option_index ];
652+
$widget_options = get_option( 'widget_' . $widget_name, [] );
653+
/**
654+
* @var array<string, mixed> $widget_options
655+
*/
656+
$prepared_widget->options = $widget_options[ $option_index ];
651657

652658
$prepared_widgets[] = $prepared_widget;
653659
}
654660

661+
/**
662+
* @phpstan-var list<object{name: string, id: string, position: int, options: array<string, mixed>}&\stdClass> $prepared_widgets
663+
*/
664+
655665
return $prepared_widgets;
656666
}
657667

658668
/**
659669
* Re-implementation of wp_get_sidebars_widgets()
660-
* because the original has a nasty global component
670+
* because the original has a nasty global component.
671+
*
672+
* @return array<string, array<int, string>>
661673
*/
662674
private function wp_get_sidebars_widgets() {
663675
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
@@ -666,14 +678,18 @@ private function wp_get_sidebars_widgets() {
666678
unset( $sidebars_widgets['array_version'] );
667679
}
668680

681+
/**
682+
* @var array<string, array<int, string>> $sidebars_widgets
683+
*/
684+
669685
return $sidebars_widgets;
670686
}
671687

672688
/**
673689
* Gets the widget's name, option index, sidebar, and sidebar index from its ID
674690
*
675691
* @param string $widget_id
676-
* @return array
692+
* @return array{0: string, 1: string, 2: string, 3: int}
677693
*/
678694
private function get_widget_data( $widget_id ) {
679695

@@ -694,24 +710,28 @@ private function get_widget_data( $widget_id ) {
694710
}
695711
}
696712

697-
return array( $name, $option_index, $sidebar_id, $sidebar_index );
713+
return array( $name, $option_index, (string) $sidebar_id, (int) $sidebar_index );
698714
}
699715

700716
/**
701717
* Gets the options for a given widget
702718
*
703719
* @param string $name
704-
* @return array
720+
* @return array<string, mixed>
705721
*/
706722
private function get_widget_options( $name ) {
707-
return get_option( 'widget_' . $name, array() );
723+
$options = get_option( 'widget_' . $name, array() );
724+
/**
725+
* @var array<string,mixed> $options
726+
*/
727+
return $options;
708728
}
709729

710730
/**
711731
* Updates the options for a given widget
712732
*
713733
* @param string $name
714-
* @param mixed
734+
* @param mixed $value
715735
*/
716736
private function update_widget_options( $name, $value ) {
717737
update_option( 'widget_' . $name, $value );
@@ -785,7 +805,7 @@ private function get_widget_obj( $id_base ) {
785805
* @param string $id_base Name of the widget
786806
* @param mixed $dirty_options
787807
* @param mixed $old_options
788-
* @return mixed
808+
* @return array<string, mixed>
789809
*/
790810
private function sanitize_widget_options( $id_base, $dirty_options, $old_options ) {
791811

@@ -797,6 +817,6 @@ private function sanitize_widget_options( $id_base, $dirty_options, $old_options
797817
// No easy way to determine expected array keys for $dirty_options
798818
// because Widget API dependent on the form fields
799819
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Whitelisting due to above reason.
800-
return @$widget->update( $dirty_options, $old_options );
820+
return @$widget->update( $dirty_options, $old_options ); // @phpstan-ignore argument.type, argument.type
801821
}
802822
}

0 commit comments

Comments
 (0)