Skip to content

Commit

Permalink
Create phpstan types for Widget and Menu classes
Browse files Browse the repository at this point in the history
- Include basic definitions for widget settings.
- Fix inconsistencies found from the new types.

Note: If this pull request gets merged a bunch of ignored phpstan items can be
removed.

php-stubs/wordpress-stubs#160
  • Loading branch information
lipemat committed Feb 13, 2024
1 parent 579ff9d commit 5a8872d
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 243 deletions.
165 changes: 0 additions & 165 deletions dev/phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@
'count' => 1,
'path' => __DIR__ . '/../src/Core.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Core\\:\\:widget_documentation\\(\\) has parameter \\$_ with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Core.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Debug\\:\\:adjust_widget_settings\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
Expand Down Expand Up @@ -256,16 +251,6 @@
'count' => 1,
'path' => __DIR__ . '/../src/Menus/Menu_Abstract.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Menus\\\\Menu_Abstract\\:\\:factory\\(\\) has parameter \\$widget_args with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Menus/Menu_Abstract.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Menus\\\\Menu_Abstract\\:\\:factory\\(\\) has parameter \\$widget_instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Menus/Menu_Abstract.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Menus\\\\Menu_Abstract\\:\\:get_widget_args\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
Expand All @@ -291,11 +276,6 @@
'count' => 1,
'path' => __DIR__ . '/../src/Notice.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Notice\\:\\:info_panel\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Notice.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Notice\\:\\:plugin_action_link\\(\\) has parameter \\$actions with no value type specified in iterable type array\\.$#',
'count' => 1,
Expand All @@ -311,11 +291,6 @@
'count' => 1,
'path' => __DIR__ . '/../src/Notice.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Notice\\:\\:preview\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Notice.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Notice\\:\\:pro_version_warning\\(\\) has no return type specified\\.$#',
'count' => 1,
Expand Down Expand Up @@ -366,145 +341,5 @@
'count' => 1,
'path' => __DIR__ . '/../src/Walkers/Page_Walker.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:box_display\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:box_display_on_single_posts\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:box_exclude\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:form\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:get_display_each_options\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:get_taxonomy_label\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:update\\(\\) has parameter \\$new_instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:update\\(\\) has parameter \\$old_instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:update\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:widget\\(\\) has parameter \\$args with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:widget\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Property Advanced_Sidebar_Menu\\\\Widget\\\\Category\\:\\:\\$defaults type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Category.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:box_display\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:box_exclude\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:box_order\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:form\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:get_order_by_options\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:get_post_type_label\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:update\\(\\) has parameter \\$new_instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:update\\(\\) has parameter \\$old_instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:update\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:widget\\(\\) has parameter \\$args with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:widget\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Property Advanced_Sidebar_Menu\\\\Widget\\\\Page\\:\\:\\$defaults type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Page.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Widget_Abstract\\:\\:set_instance\\(\\) has parameter \\$defaults with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Widget_Abstract.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Widget_Abstract\\:\\:set_instance\\(\\) has parameter \\$instance with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Widget_Abstract.php',
];
$ignoreErrors[] = [
'message' => '#^Method Advanced_Sidebar_Menu\\\\Widget\\\\Widget_Abstract\\:\\:set_instance\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Widget_Abstract.php',
];
$ignoreErrors[] = [
'message' => '#^Property Advanced_Sidebar_Menu\\\\Widget\\\\Widget_Abstract\\:\\:\\$widget_settings type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/../src/Widget/Widget_Abstract.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
7 changes: 6 additions & 1 deletion src/Blocks/Block_Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
use Advanced_Sidebar_Menu\Menus\Menu_Abstract;
use Advanced_Sidebar_Menu\Scripts;
use Advanced_Sidebar_Menu\Utils;
use Advanced_Sidebar_Menu\Widget\Category;
use Advanced_Sidebar_Menu\Widget\Page;

/**
* Functionality shared by and required by all blocks.
*
* @since 9.0.0
*
* @phpstan-import-type PAGE_SETTINGS from Page
* @phpstan-import-type CATEGORY_SETTINGS from Category
*/
abstract class Block_Abstract {
public const NAME = 'block-abstract';
Expand Down Expand Up @@ -136,7 +141,7 @@ public function exclude_from_legacy_widgets( $blocks ) {
* the calls to output the wrap.
*
* @param false|array $instance - Contents of the block, before parsing.
* @param \WP_Widget $widget - Object representing a block based widget.
* @param \WP_Widget<PAGE_SETTINGS|CATEGORY_SETTINGS> $widget - Object representing a block based widget.
* @param array $args - Widget area arguments.
*
* @return false|array
Expand Down
13 changes: 9 additions & 4 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* Core functionality for Advanced Sidebar Menu Plugin
*
* @author OnPoint Plugins
*
* @phpstan-import-type PAGE_SETTINGS from Widget\Page
* @phpstan-import-type CATEGORY_SETTINGS from Widget\Category
*/
class Core {
use Singleton;
Expand Down Expand Up @@ -45,14 +48,16 @@ public function register_widgets() {
/**
* Display a link to a widget's documentation.
*
* @param array $_ - Widget settings.
* @param \WP_Widget $widget - Widget class.
*
* @since 9.0.0
*
* @phpstan-param array<PAGE_SETTINGS|CATEGORY_SETTINGS> $_
*
* @param array $_ - Widget settings.
* @param \WP_Widget<PAGE_SETTINGS|CATEGORY_SETTINGS> $widget - Widget class.
*
* @return void
*/
public function widget_documentation( $_, \WP_Widget $widget ) {
public function widget_documentation( array $_, \WP_Widget $widget ) {
?>
<p class="advanced-sidebar-widget-documentation">
<a
Expand Down
4 changes: 2 additions & 2 deletions src/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class Debug {
use Singleton;

const DEBUG_PARAM = 'asm_debug';
public const DEBUG_PARAM = 'asm_debug';


/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public function get_site_info(): array {
/**
* Print the widget settings as a JS variable.
*
* @param Menu_Abstract $menu - Menu class.
* @param Menu_Abstract<array<string,string>> $menu - Menu class.
* @param Page|Category $widget - Widget class.
*
* @return void
Expand Down
13 changes: 13 additions & 0 deletions src/Menus/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
* Category menu.
*
* @author OnPoint Plugins
*
* @phpstan-type CATEGORY_SETTINGS array{
* taxonomy?: string,
* exclude: string,
* include_childless_parent?: ''|'checked',
* include_parent?: ''|'checked',
* levels: numeric-string,
* new_widget: 'widget'|'list',
* single: ''|'checked',
* title?: string,
* }
*
* @extends Menu_Abstract<CATEGORY_SETTINGS>
*/
class Category extends Menu_Abstract {
use Memoize;
Expand Down
15 changes: 12 additions & 3 deletions src/Menus/Menu_Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace Advanced_Sidebar_Menu\Menus;

use Advanced_Sidebar_Menu\Widget\Widget_Abstract;

/**
* Base for Menu classes.
*
* @author OnPoint Plugins
*
* @phpstan-import-type PAGE_SETTINGS from Page
* @phpstan-import-type WIDGET_ARGS from Widget_Abstract
*
* @phpstan-template SETTINGS of array<string, string>
*/
abstract class Menu_Abstract {
public const WIDGET = 'menu-abstract';
Expand Down Expand Up @@ -58,7 +65,7 @@ abstract class Menu_Abstract {
* @param array $instance - Widget settings.
* @param array $args - Widget registration arguments.
*/
public function __construct( array $instance, array $args ) {
final public function __construct( array $instance, array $args ) {
$this->instance = apply_filters( 'advanced-sidebar-menu/menus/widget-instance', $instance, $args, $this );
$this->args = $args;

Expand Down Expand Up @@ -277,15 +284,17 @@ public static function get_current() {
/**
* Constructs a new instance of this class.
*
* @param array $widget_instance - Widget settings.
* @phpstan-param SETTINGS $widget_instance
* @phpstan-param WIDGET_ARGS $widget_args
*
* @param array<string, string> $widget_instance - Widget settings.
* @param array $widget_args - Widget registration args.
*
* @static
*
* @return static
*/
public static function factory( array $widget_instance, array $widget_args ) {
/* @phpstan-ignore-next-line */
$menu = new static( $widget_instance, $widget_args );
/* @phpstan-ignore-next-line */
static::$current = $menu;
Expand Down
15 changes: 15 additions & 0 deletions src/Menus/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@

use Advanced_Sidebar_Menu\Core;
use Advanced_Sidebar_Menu\List_Pages;
use Advanced_Sidebar_Menu\Widget\Widget_Abstract;

/**
* Page menu.
*
* @author OnPoint Plugins
*
* @phpstan-import-type WIDGET_ARGS from Widget_Abstract
* @phpstan-type PAGE_SETTINGS array{
* exclude: string,
* order_by: string,
* title?: string,
* display_all?: ''|'checked',
* include_childless_parent?: ''|'checked',
* include_parent?: ''|'checked',
* levels: numeric-string,
* post_type?: string,
* }
*
* @extends Menu_Abstract<PAGE_SETTINGS>
*/
class Page extends Menu_Abstract {
public const WIDGET = 'page';
Expand Down
Loading

0 comments on commit 5a8872d

Please sign in to comment.