From 91f90a03d1b69e53eb0a6024fd30d40ed59eedcb Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Tue, 17 Dec 2024 10:32:46 +0100 Subject: [PATCH] Fix PHPStan errors after updating WordPress Stubs [MAILPOET-6356] --- mailpoet/lib/Automation/Engine/WordPress.php | 2 + .../Endpoints/AutomationFlowEndpoint.php | 2 +- .../Analytics/Endpoints/OverviewEndpoint.php | 2 +- .../Integrations/WordPress/ContextFactory.php | 4 +- mailpoet/lib/Form/Widget.php | 6 ++- mailpoet/lib/WP/Functions.php | 2 +- mailpoet/tasks/phpstan/phpstan.neon | 2 +- mailpoet/tests/_support/DefaultsExtension.php | 48 +++++++++---------- 8 files changed, 36 insertions(+), 32 deletions(-) diff --git a/mailpoet/lib/Automation/Engine/WordPress.php b/mailpoet/lib/Automation/Engine/WordPress.php index 1e3f510e85..759355166f 100644 --- a/mailpoet/lib/Automation/Engine/WordPress.php +++ b/mailpoet/lib/Automation/Engine/WordPress.php @@ -145,6 +145,7 @@ class WordPress { } /** + * @param 'names'|'objects' $output * @return string[]|\WP_Post_Type[] */ public function getPostTypes(array $args = [], string $output = 'names', string $operator = 'and'): array { @@ -156,6 +157,7 @@ class WordPress { } /** + * @param 'names'|'objects' $output * @param 'and'|'or' $operator * @return string[]|\WP_Taxonomy[] */ diff --git a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php index 3269373495..f98b3caf9f 100644 --- a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php +++ b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php @@ -47,7 +47,7 @@ class AutomationFlowEndpoint extends Endpoint { } public function handle(Request $request): Response { - $id = absint($request->getParam('id')); + $id = absint(is_numeric($request->getParam('id')) ? $request->getParam('id') : 0); $automation = $this->automationStorage->getAutomation($id); if (!$automation) { throw Exceptions::automationNotFound($id); diff --git a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php index 8cf7224f9a..1956b8a92e 100644 --- a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php +++ b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php @@ -28,7 +28,7 @@ class OverviewEndpoint extends Endpoint { } public function handle(Request $request): Response { - $id = absint($request->getParam('id')); + $id = absint(is_numeric($request->getParam('id')) ? $request->getParam('id') : 0); $automation = $this->automationStorage->getAutomation($id); if (!$automation) { throw Exceptions::automationNotFound($id); diff --git a/mailpoet/lib/Automation/Integrations/WordPress/ContextFactory.php b/mailpoet/lib/Automation/Integrations/WordPress/ContextFactory.php index 8bb571afcd..32722c7edb 100644 --- a/mailpoet/lib/Automation/Integrations/WordPress/ContextFactory.php +++ b/mailpoet/lib/Automation/Integrations/WordPress/ContextFactory.php @@ -44,7 +44,7 @@ class ContextFactory { */ private function getPostTypes(): array { /** @var \WP_Post_Type[] $postTypes */ - $postTypes = $this->wp->getPostTypes([], 'object'); + $postTypes = $this->wp->getPostTypes([], 'objects'); return array_values(array_map(function(\WP_Post_Type $type): array { $supports = ['comments' => false]; @@ -74,7 +74,7 @@ class ContextFactory { private function getTaxonomies(): array { /** @var \WP_Taxonomy[] $taxonomies */ $taxonomies = array_filter( - $this->wp->getTaxonomies([], 'object'), + $this->wp->getTaxonomies([], 'objects'), function($object): bool { return $object instanceof \WP_Taxonomy; } diff --git a/mailpoet/lib/Form/Widget.php b/mailpoet/lib/Form/Widget.php index 18c687ab2f..e76503e700 100644 --- a/mailpoet/lib/Form/Widget.php +++ b/mailpoet/lib/Form/Widget.php @@ -111,8 +111,8 @@ class Widget extends \WP_Widget { */ public function update($newInstance, $oldInstance) { $instance = $oldInstance; - $instance['title'] = strip_tags($newInstance['title']); - $instance['form'] = (int)$newInstance['form']; + $instance['title'] = strip_tags(is_string($newInstance['title']) ? $newInstance['title'] : ''); + $instance['form'] = is_numeric($newInstance['form']) ? (int)$newInstance['form'] : null; return $instance; } @@ -168,6 +168,8 @@ class Widget extends \WP_Widget { } /** + * @phpstan-ignore-next-line $args are not passed to parent and our rendering is custom so it is ok that $args doesn't match parent's $arg shape. + * @param array{form?: int|string, form_type?: string, before_widget?: string, after_widget?: string, before_title?: string, after_title?: string } $args Widget arguments. * Output the widget itself. */ public function widget($args, $instance = null) { diff --git a/mailpoet/lib/WP/Functions.php b/mailpoet/lib/WP/Functions.php index c4bdbba920..c204948ab5 100644 --- a/mailpoet/lib/WP/Functions.php +++ b/mailpoet/lib/WP/Functions.php @@ -691,7 +691,7 @@ class Functions { } /** - * @param 'hot_categories'|'hot_tags'|'plugin_information'|'query_plugins' $action + * @param 'hot_tags'|'plugin_information'|'query_plugins' $action * @param array|object $args * @return object|array|WP_Error */ diff --git a/mailpoet/tasks/phpstan/phpstan.neon b/mailpoet/tasks/phpstan/phpstan.neon index 18fe6e4da4..1d593b57dc 100644 --- a/mailpoet/tasks/phpstan/phpstan.neon +++ b/mailpoet/tasks/phpstan/phpstan.neon @@ -69,7 +69,7 @@ parameters: - # WP annotates parameter as callable, but passes empty string as a default. message: '/function add_(sub)?menu_page expects callable\(\): mixed, ''''\|\(callable\(\): mixed\) given/' - count: 2 + count: 1 path: ../../lib/WP/Functions.php - # We don't allow seting properties for read-only entity WpPostEntity diff --git a/mailpoet/tests/_support/DefaultsExtension.php b/mailpoet/tests/_support/DefaultsExtension.php index 0942370e74..98151a3ae8 100644 --- a/mailpoet/tests/_support/DefaultsExtension.php +++ b/mailpoet/tests/_support/DefaultsExtension.php @@ -29,14 +29,14 @@ class DefaultsExtension extends Extension { } private function setupWordPress() { - update_option('siteurl', 'http://test.local', 'yes'); - update_option('home', 'http://test.local', 'yes'); - update_option('blogname', 'MP Dev', 'yes'); - update_option('admin_email', 'test@example.com', 'yes'); - update_option('gmt_offset', '0', 'yes'); - update_option('users_can_register', '1', 'yes'); + update_option('siteurl', 'http://test.local', true); + update_option('home', 'http://test.local', true); + update_option('blogname', 'MP Dev', true); + update_option('admin_email', 'test@example.com', true); + update_option('gmt_offset', '0', true); + update_option('users_can_register', '1', true); update_site_option('registration', 'user'); - update_option('permalink_structure', '/%year%/%monthnum%/%day%/%postname%/', 'yes'); + update_option('permalink_structure', '/%year%/%monthnum%/%day%/%postname%/', true); // posts & pages $this->createPost('post', 'hello-world', 'Hello world!', 'Hello from WordPress.'); @@ -58,17 +58,17 @@ class DefaultsExtension extends Extension { private function setupWooCommerce() { global $wpdb; // address - update_option('woocommerce_store_address', 'Address', 'yes'); - update_option('woocommerce_store_address_2', '', 'yes'); - update_option('woocommerce_store_city', 'Paris', 'yes'); - update_option('woocommerce_default_country', 'FR:*', 'yes'); - update_option('woocommerce_store_postcode', '75000', 'yes'); + update_option('woocommerce_store_address', 'Address', true); + update_option('woocommerce_store_address_2', '', true); + update_option('woocommerce_store_city', 'Paris', true); + update_option('woocommerce_default_country', 'FR:*', true); + update_option('woocommerce_store_postcode', '75000', true); // currency - update_option('woocommerce_currency', 'EUR', 'yes'); - update_option('woocommerce_currency_pos', 'right', 'yes'); - update_option('woocommerce_price_thousand_sep', ' ', 'yes'); - update_option('woocommerce_price_decimal_sep', ',', 'yes'); + update_option('woocommerce_currency', 'EUR', true); + update_option('woocommerce_currency_pos', 'right', true); + update_option('woocommerce_price_thousand_sep', ' ', true); + update_option('woocommerce_price_decimal_sep', ',', true); // pages // block-based pages are created automatically by WooCommerce @@ -77,16 +77,16 @@ class DefaultsExtension extends Extension { $checkoutPageId = $this->createPage('shortcode-checkout', 'Shortcode Checkout', '[woocommerce_checkout]'); $myAccountPageId = $this->createPage('shortcode-my-account', 'Shortcode My account', '[woocommerce_my_account]'); - update_option('woocommerce_shop_page_id', $shopPageId, 'yes'); - update_option('woocommerce_cart_page_id', $cartPageId, 'yes'); - update_option('woocommerce_checkout_page_id', $checkoutPageId, 'yes'); - update_option('woocommerce_myaccount_page_id', $myAccountPageId, 'yes'); + update_option('woocommerce_shop_page_id', $shopPageId, true); + update_option('woocommerce_cart_page_id', $cartPageId, true); + update_option('woocommerce_checkout_page_id', $checkoutPageId, true); + update_option('woocommerce_myaccount_page_id', $myAccountPageId, true); // other - update_option('woocommerce_bacs_settings', ['enabled' => 'yes'], 'yes'); - update_option('woocommerce_cod_settings', ['enabled' => 'yes', 'enable_for_virtual' => 'yes'], 'yes'); - update_option('woocommerce_enable_signup_and_login_from_checkout', 'yes', 'no'); - update_option('woocommerce_enable_myaccount_registration', 'yes', 'no'); + update_option('woocommerce_bacs_settings', ['enabled' => 'yes'], true); + update_option('woocommerce_cod_settings', ['enabled' => 'yes', 'enable_for_virtual' => 'yes'], true); + update_option('woocommerce_enable_signup_and_login_from_checkout', 'yes', false); + update_option('woocommerce_enable_myaccount_registration', 'yes', false); // don't send customer/order emails, the mail() function is not configured and outputs warning, // these lines can be removed when https://github.com/lucatume/wp-browser/issues/316 is solved