From c986424d3e8be7dd5412bd808626bde40d29a41e Mon Sep 17 00:00:00 2001 From: Abdelmalek El Mellouki Date: Tue, 17 Mar 2026 23:57:40 +0000 Subject: [PATCH 1/4] Fix PHPStan level 2 errors. --- includes/Context.php | 2 +- includes/Core/Admin_Bar/Admin_Bar.php | 2 +- .../Exception/Insufficient_Scopes_Exception.php | 8 ++++---- includes/Core/Consent_Mode/Consent_Mode.php | 2 +- includes/Core/Modules/Datapoint.php | 4 +++- .../Core/Modules/Module_With_Settings_Trait.php | 7 ++++--- includes/Core/Modules/Module_With_Tag.php | 10 ++++++++++ includes/Core/Modules/Modules.php | 6 +++--- .../Core/Modules/REST_Modules_Controller.php | 17 ++++++++++------- includes/Core/Site_Health/Tag_Placement.php | 8 ++++---- includes/Core/Tags/Guards/Tag_Verify_Guard.php | 2 +- .../REST_Tracking_Consent_Controller.php | 2 +- includes/Core/User_Input/User_Input.php | 6 +++--- includes/Core/Util/Auto_Updates.php | 2 +- includes/Core/Util/Date.php | 2 +- includes/Core/Util/Feature_Flags.php | 10 +++++----- includes/Core/Util/Health_Checks.php | 1 + includes/Modules/AdSense.php | 3 ++- includes/Modules/AdSense/Tag_Guard.php | 2 +- includes/Modules/Analytics_4.php | 2 +- .../Email_Reporting/Report_Options.php | 2 +- includes/Modules/Analytics_4/Report/Request.php | 2 +- .../Resource_Data_Availability_Date.php | 4 ++-- .../Analytics_4/Synchronize_AdSenseLinked.php | 2 -- .../Analytics_4/Synchronize_AdsLinked.php | 2 -- includes/Modules/Analytics_4/Tag_Guard.php | 2 +- includes/Modules/PageSpeed_Insights.php | 2 ++ includes/Modules/Reader_Revenue_Manager.php | 4 ++++ .../Reader_Revenue_Manager/Tag_Guard.php | 2 +- includes/Modules/Search_Console.php | 1 + .../Datapoints/SearchAnalyticsBatch.php | 7 ++++--- includes/Modules/Sign_In_With_Google.php | 1 + .../Modules/Sign_In_With_Google/Tag_Guard.php | 2 +- includes/Modules/Tag_Manager/Tag_Guard.php | 2 +- phpstan.neon.dist | 4 +++- 35 files changed, 81 insertions(+), 56 deletions(-) diff --git a/includes/Context.php b/includes/Context.php index f8a37d1c632..a2dcb81d862 100644 --- a/includes/Context.php +++ b/includes/Context.php @@ -257,7 +257,7 @@ public function get_reference_entity_from_url( $url ) { * * @since 1.0.0 * - * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. + * @param int|\WP_Post $post Optional. Post ID or post object. Default is the global `$post`. * * @return string|false The reference permalink URL or false if post does not exist. */ diff --git a/includes/Core/Admin_Bar/Admin_Bar.php b/includes/Core/Admin_Bar/Admin_Bar.php index e7537f789e5..f37d06da166 100644 --- a/includes/Core/Admin_Bar/Admin_Bar.php +++ b/includes/Core/Admin_Bar/Admin_Bar.php @@ -383,7 +383,7 @@ private function get_rest_routes() { $this->admin_bar_enabled->set( ! empty( $data['enabled'] ) ); } - return $settings_callback( $request ); + return $settings_callback(); }, 'permission_callback' => $can_authenticate, 'args' => array( diff --git a/includes/Core/Authentication/Exception/Insufficient_Scopes_Exception.php b/includes/Core/Authentication/Exception/Insufficient_Scopes_Exception.php index dd0d3561bdf..e54a797e593 100644 --- a/includes/Core/Authentication/Exception/Insufficient_Scopes_Exception.php +++ b/includes/Core/Authentication/Exception/Insufficient_Scopes_Exception.php @@ -39,10 +39,10 @@ class Insufficient_Scopes_Exception extends Exception implements WP_Errorable { * * @since 1.9.0 * - * @param string $message Optional. Exception message. - * @param int $code Optional. Exception code. - * @param Throwable $previous Optional. Previous exception used for chaining. - * @param array $scopes Optional. Scopes that are missing. + * @param string $message Optional. Exception message. + * @param int $code Optional. Exception code. + * @param \Throwable $previous Optional. Previous exception used for chaining. + * @param array $scopes Optional. Scopes that are missing. */ public function __construct( $message = '', $code = 0, $previous = null, $scopes = array() ) { parent::__construct( $message, $code, $previous ); diff --git a/includes/Core/Consent_Mode/Consent_Mode.php b/includes/Core/Consent_Mode/Consent_Mode.php index e70739c9d32..1303bdef2f2 100644 --- a/includes/Core/Consent_Mode/Consent_Mode.php +++ b/includes/Core/Consent_Mode/Consent_Mode.php @@ -144,7 +144,7 @@ public function install_activate_wp_consent_api() { require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // For Plugin_Upgrader and Plugin_Installer_Skin. require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // For plugins_api. - $api = plugins_api( + $api = (object) plugins_api( 'plugin_information', array( 'slug' => $slug, diff --git a/includes/Core/Modules/Datapoint.php b/includes/Core/Modules/Datapoint.php index ba0999ef9fc..f7892cce0a9 100644 --- a/includes/Core/Modules/Datapoint.php +++ b/includes/Core/Modules/Datapoint.php @@ -10,6 +10,8 @@ namespace Google\Site_Kit\Core\Modules; +use Google\Site_Kit_Dependencies\Google\Service as Google_Service; + /** * Class representing a datapoint definition. * @@ -97,7 +99,7 @@ public function is_shareable() { * * @since 1.77.0 * - * @return string + * @return string|Google_Service */ protected function get_service() { $service = $this->service; diff --git a/includes/Core/Modules/Module_With_Settings_Trait.php b/includes/Core/Modules/Module_With_Settings_Trait.php index 644f2bf89c7..dee0411bd60 100644 --- a/includes/Core/Modules/Module_With_Settings_Trait.php +++ b/includes/Core/Modules/Module_With_Settings_Trait.php @@ -14,6 +14,7 @@ * Trait for a module that includes a screen. * * @since 1.2.0 + * @template T of Module_Settings * @access private * @ignore */ @@ -24,7 +25,7 @@ trait Module_With_Settings_Trait { * * @since 1.2.0 * - * @var Module_Settings + * @var T */ protected $settings; @@ -33,7 +34,7 @@ trait Module_With_Settings_Trait { * * @since 1.2.0 * - * @return Module_Settings + * @return T */ abstract protected function setup_settings(); @@ -42,7 +43,7 @@ abstract protected function setup_settings(); * * @since 1.2.0 * - * @return Module_Settings Module_Settings instance. + * @return T Module_Settings instance. */ public function get_settings() { if ( ! $this->settings instanceof Module_Settings ) { diff --git a/includes/Core/Modules/Module_With_Tag.php b/includes/Core/Modules/Module_With_Tag.php index c0859054012..68668a7a7dc 100644 --- a/includes/Core/Modules/Module_With_Tag.php +++ b/includes/Core/Modules/Module_With_Tag.php @@ -29,4 +29,14 @@ public function register_tag(); * @return Module_Tag_Matchers Module_Tag_Matchers instance. */ public function get_tag_matchers(); + + /** + * Checks if the module tag is found in the provided content. + * + * @since n.e.x.t + * + * @param string $content Content to search for the tags. + * @return bool TRUE if tag is found, FALSE if not. + */ + public function has_placed_tag_in_content( $content ); } diff --git a/includes/Core/Modules/Modules.php b/includes/Core/Modules/Modules.php index dd42f996e8f..db52b391a96 100644 --- a/includes/Core/Modules/Modules.php +++ b/includes/Core/Modules/Modules.php @@ -318,7 +318,7 @@ function ( $value, $module_slug ) use ( $old_values ) { if ( ! $module instanceof Module_With_Service_Entity ) { // If the option was just added, set the ownerID directly and bail. - if ( empty( $old_values ) ) { + if ( empty( $old_values ) && $module instanceof Module_With_Settings ) { $module->get_settings()->merge( array( 'ownerID' => get_current_user_id(), @@ -357,7 +357,7 @@ function ( $setting, $setting_key ) use ( $old_values, $module_slug, &$changed_s ); } - if ( $changed_settings ) { + if ( $changed_settings && $module instanceof Module_With_Settings ) { $module->get_settings()->merge( array( 'ownerID' => get_current_user_id(), @@ -432,7 +432,7 @@ public function get_module_sharing_settings() { * @since 1.0.0 * @since 1.85.0 Filter out modules which are missing any of the dependencies specified in `depends_on`. * - * @return array Available modules as $slug => $module pairs. + * @return Module[] Available modules as $slug => $module pairs. */ public function get_available_modules() { if ( empty( $this->modules ) ) { diff --git a/includes/Core/Modules/REST_Modules_Controller.php b/includes/Core/Modules/REST_Modules_Controller.php index a849b5d3358..e010e67cd4d 100644 --- a/includes/Core/Modules/REST_Modules_Controller.php +++ b/includes/Core/Modules/REST_Modules_Controller.php @@ -705,14 +705,17 @@ private function get_rest_routes() { continue; } - // Update the module's ownerID to the ID of the user making the request. - $module_setting_updates = array( - 'ownerID' => get_current_user_id(), - ); - $recovered_module = $module->get_settings()->merge( $module_setting_updates ); + if ( $module instanceof Module_With_Settings ) { + // Update the module's ownerID to the ID of the user making the request. + $module_setting_updates = array( + 'ownerID' => get_current_user_id(), + ); - if ( $recovered_module ) { - $response['success'][ $slug ] = true; + $recovered_module = $module->get_settings()->merge( $module_setting_updates ); + + if ( $recovered_module ) { + $response['success'][ $slug ] = true; + } } } diff --git a/includes/Core/Site_Health/Tag_Placement.php b/includes/Core/Site_Health/Tag_Placement.php index bddae7ead93..4f801c114b1 100644 --- a/includes/Core/Site_Health/Tag_Placement.php +++ b/includes/Core/Site_Health/Tag_Placement.php @@ -10,13 +10,13 @@ namespace Google\Site_Kit\Core\Site_Health; +use Google\Site_Kit\Core\Modules\Module; use Google\Site_Kit\Core\Modules\Modules; use Google\Site_Kit\Core\Modules\Module_With_Tag; use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Matchers; use Google\Site_Kit\Core\REST_API\REST_Routes; use Google\Site_Kit\Core\Tags\Guards\Tag_Environment_Type_Guard; use Google\Site_Kit\Core\Util\Method_Proxy_Trait; -use Google\Site_Kit\Modules\Analytics_4; /** * Class for integrating status tab information with Site Health. @@ -212,9 +212,9 @@ function ( $module ) { * * @since 1.119.0 * - * @param Module_With_Tag $module Module instance. - * @param string $content Content to search for the tags. - * @param string $module_label Content URL page name appended to the module name to identify multiple tags for a module. + * @param Module_With_Tag|Module $module Module instance. + * @param string $content Content to search for the tags. + * @param string $module_label Content URL page name appended to the module name to identify multiple tags for a module. * * @return bool TRUE if tag is found, FALSE if not. */ diff --git a/includes/Core/Tags/Guards/Tag_Verify_Guard.php b/includes/Core/Tags/Guards/Tag_Verify_Guard.php index 8e49323596d..d2209e90887 100644 --- a/includes/Core/Tags/Guards/Tag_Verify_Guard.php +++ b/includes/Core/Tags/Guards/Tag_Verify_Guard.php @@ -46,7 +46,7 @@ public function __construct( Input $input ) { * * @since 1.24.0 * - * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. + * @return bool|\WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { return ! $this->input->filter( INPUT_GET, 'tagverify', FILTER_VALIDATE_BOOLEAN ); diff --git a/includes/Core/Tracking/REST_Tracking_Consent_Controller.php b/includes/Core/Tracking/REST_Tracking_Consent_Controller.php index 5206aef4777..15e8a7a8ff5 100644 --- a/includes/Core/Tracking/REST_Tracking_Consent_Controller.php +++ b/includes/Core/Tracking/REST_Tracking_Consent_Controller.php @@ -120,7 +120,7 @@ private function get_rest_routes( $routes ) { $this->consent->set( $enabled ); - return $tracking_callback( $request ); + return $tracking_callback(); }, 'permission_callback' => $can_access_tracking, 'args' => array( diff --git a/includes/Core/User_Input/User_Input.php b/includes/Core/User_Input/User_Input.php index 2e2505aad17..3f6779fc93b 100644 --- a/includes/Core/User_Input/User_Input.php +++ b/includes/Core/User_Input/User_Input.php @@ -131,7 +131,7 @@ public function register() { * @return array The user input questions. */ public static function get_questions() { - return static::$questions; + return self::$questions; } /** @@ -142,7 +142,7 @@ public static function get_questions() { * @return array|WP_Error User input answers. */ public function get_answers() { - $questions = static::$questions; + $questions = self::$questions; $site_answers = $this->site_specific_answers->get(); $user_answers = $this->user_specific_answers->get(); @@ -238,7 +238,7 @@ public function set_answers( $settings ) { foreach ( $settings as $setting_key => $answers ) { $setting_data = array(); $setting_data['values'] = $answers; - $setting_data['scope'] = static::$questions[ $setting_key ]['scope']; + $setting_data['scope'] = self::$questions[ $setting_key ]['scope']; if ( 'site' === $setting_data['scope'] ) { $existing_answers = $this->get_answers(); diff --git a/includes/Core/Util/Auto_Updates.php b/includes/Core/Util/Auto_Updates.php index b9985ebc8ce..f86b40895ba 100644 --- a/includes/Core/Util/Auto_Updates.php +++ b/includes/Core/Util/Auto_Updates.php @@ -41,7 +41,7 @@ class Auto_Updates { * Auto updated not forced. * * @since 1.93.0 - * @var false + * @var null */ const AUTO_UPDATE_NOT_FORCED = null; diff --git a/includes/Core/Util/Date.php b/includes/Core/Util/Date.php index ee2bc059439..106ac9174c7 100644 --- a/includes/Core/Util/Date.php +++ b/includes/Core/Util/Date.php @@ -26,7 +26,7 @@ class Date { * * @param string $range Date range string. Either 'last-7-days', 'last-14-days', 'last-90-days', or * 'last-28-days' (default). - * @param string $multiplier Optional. How many times the date range to get. This value can be specified if the + * @param int $multiplier Optional. How many times the date range to get. This value can be specified if the * range should be request multiple times back. Default 1. * @param int $offset Days the range should be offset by. Default 1. Used by Search Console where * data is delayed by two days. diff --git a/includes/Core/Util/Feature_Flags.php b/includes/Core/Util/Feature_Flags.php index 35d1a77c7dd..7918388572e 100644 --- a/includes/Core/Util/Feature_Flags.php +++ b/includes/Core/Util/Feature_Flags.php @@ -38,7 +38,7 @@ class Feature_Flags { * @return bool */ public static function enabled( $feature ) { - if ( ! $feature || ! is_string( $feature ) || empty( static::$features ) ) { + if ( ! $feature || ! is_string( $feature ) || empty( self::$features ) ) { return false; } @@ -66,8 +66,8 @@ public static function enabled( $feature ) { public static function get_enabled_features() { $enabled_features = array(); - foreach ( static::$features as $feature_name ) { - if ( static::enabled( $feature_name ) ) { + foreach ( self::$features as $feature_name ) { + if ( self::enabled( $feature_name ) ) { $enabled_features[] = $feature_name; } } @@ -84,7 +84,7 @@ public static function get_enabled_features() { */ public static function set_features( $features ) { if ( is_array( $features ) || $features instanceof ArrayAccess ) { - static::$features = $features; + self::$features = $features; } } @@ -96,6 +96,6 @@ public static function set_features( $features ) { * @return array An array of all available features. */ public static function get_available_features() { - return static::$features; + return self::$features; } } diff --git a/includes/Core/Util/Health_Checks.php b/includes/Core/Util/Health_Checks.php index b32a55ad96c..d2c4981867c 100644 --- a/includes/Core/Util/Health_Checks.php +++ b/includes/Core/Util/Health_Checks.php @@ -12,6 +12,7 @@ use Exception; use Google\Site_Kit\Core\Authentication\Authentication; +use Google\Site_Kit\Core\Authentication\Google_Proxy; use Google\Site_Kit\Core\Permissions\Permissions; use Google\Site_Kit\Core\REST_API\REST_Route; use Google\Site_Kit_Dependencies\Google\Service\SearchConsole as Google_Service_SearchConsole; diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index cbd5265674b..2371295f29c 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -46,6 +46,7 @@ use Google\Site_Kit\Modules\AdSense\Web_Tag; use Google\Site_Kit_Dependencies\Google\Model as Google_Model; use Google\Site_Kit_Dependencies\Google\Service\Adsense as Google_Service_Adsense; +use Google\Site_Kit_Dependencies\Google\Service\Adsense\Account; use Google\Site_Kit_Dependencies\Google\Service\Adsense\Alert as Google_Service_Adsense_Alert; use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use Exception; @@ -546,7 +547,7 @@ protected function parse_data_response( Data_Request $data, $response ) { * * @since 1.73.0 * - * @param Google_Model $account Account model. + * @param Account $account Account model. * @return bool Whether the account is not closed. */ public static function is_account_not_closed( $account ) { diff --git a/includes/Modules/AdSense/Tag_Guard.php b/includes/Modules/AdSense/Tag_Guard.php index f782e84900d..2ce61f0a1b6 100644 --- a/includes/Modules/AdSense/Tag_Guard.php +++ b/includes/Modules/AdSense/Tag_Guard.php @@ -28,7 +28,7 @@ class Tag_Guard extends Module_Tag_Guard { * @since 1.30.0 Update to return FALSE on 404 pages deliberately. * @since 1.105.0 Extract the check for 404 pages to dedicated Guard. * - * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. + * @return bool|\WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { $settings = $this->settings->get(); diff --git a/includes/Modules/Analytics_4.php b/includes/Modules/Analytics_4.php index ef2df12c007..d4d43195be2 100644 --- a/includes/Modules/Analytics_4.php +++ b/includes/Modules/Analytics_4.php @@ -94,7 +94,7 @@ use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdminV1alpha\GoogleAnalyticsAdminV1alphaAudience; use Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdminV1alpha\GoogleAnalyticsAdminV1alphaEnhancedMeasurementSettings; use Google\Site_Kit_Dependencies\Google\Service\TagManager as Google_Service_TagManager; -use Google\Site_Kit_Dependencies\Google_Service_TagManager_Container; +use Google\Site_Kit_Dependencies\Google\Service\TagManager\Container as Google_Service_TagManager_Container; use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use Google\Site_Kit\Core\REST_API\REST_Routes; use Google\Site_Kit\Core\Tracking\Feature_Metrics_Trait; diff --git a/includes/Modules/Analytics_4/Email_Reporting/Report_Options.php b/includes/Modules/Analytics_4/Email_Reporting/Report_Options.php index 7fb003259de..534ec4ced5a 100644 --- a/includes/Modules/Analytics_4/Email_Reporting/Report_Options.php +++ b/includes/Modules/Analytics_4/Email_Reporting/Report_Options.php @@ -164,7 +164,7 @@ public function is_audience_segmentation_enabled() { return (bool) $this->audience_segmentation_enabled; } - $settings = $this->audience_config->get_module_settings(); + $settings = $this->audience_config->get_module_settings(); // @phpstan-ignore method.notFound return ! empty( $settings['audienceSegmentationSetupCompletedBy'] ); } diff --git a/includes/Modules/Analytics_4/Report/Request.php b/includes/Modules/Analytics_4/Report/Request.php index 0318d5eef5e..263cc35ece9 100644 --- a/includes/Modules/Analytics_4/Report/Request.php +++ b/includes/Modules/Analytics_4/Report/Request.php @@ -33,7 +33,7 @@ class Request extends Report { * * @param Data_Request $data Data request object. * @param bool $is_shared_request Determines whether the current request is shared or not. - * @return RequestInterface|WP_Error Request object on success, or WP_Error on failure. + * @return Google_Service_AnalyticsData_RunReportRequest|WP_Error Request object on success, or WP_Error on failure. */ public function create_request( Data_Request $data, $is_shared_request ) { $request_helpers = new RequestHelpers( $this->context ); diff --git a/includes/Modules/Analytics_4/Resource_Data_Availability_Date.php b/includes/Modules/Analytics_4/Resource_Data_Availability_Date.php index 7711b20ac4c..0d3ce58e6b2 100644 --- a/includes/Modules/Analytics_4/Resource_Data_Availability_Date.php +++ b/includes/Modules/Analytics_4/Resource_Data_Availability_Date.php @@ -167,8 +167,8 @@ function ( $custom_dimension_data_availability_dates, $custom_dimension ) { * * @since 1.127.0 * - * @param array/null $available_audience_names Optional. List of available audience resource names. If not provided, it will be fetched from settings. - * @param string/null $property_id Optional. Property ID. If not provided, it will be fetched from settings. + * @param array|null $available_audience_names Optional. List of available audience resource names. If not provided, it will be fetched from settings. + * @param string|null $property_id Optional. Property ID. If not provided, it will be fetched from settings. */ public function reset_all_resource_dates( $available_audience_names = null, $property_id = null ) { diff --git a/includes/Modules/Analytics_4/Synchronize_AdSenseLinked.php b/includes/Modules/Analytics_4/Synchronize_AdSenseLinked.php index 6afa9fd1811..b67f6ba7147 100644 --- a/includes/Modules/Analytics_4/Synchronize_AdSenseLinked.php +++ b/includes/Modules/Analytics_4/Synchronize_AdSenseLinked.php @@ -125,8 +125,6 @@ public function maybe_schedule_synchronize_adsense_linked() { * Synchronize the AdSenseLinked status. * * @since 1.123.0 - * - * @return null */ protected function synchronize_adsense_linked_status() { $settings_ga4 = $this->analytics_4->get_settings()->get(); diff --git a/includes/Modules/Analytics_4/Synchronize_AdsLinked.php b/includes/Modules/Analytics_4/Synchronize_AdsLinked.php index 44ceb74a889..a0fc4e3d5dc 100644 --- a/includes/Modules/Analytics_4/Synchronize_AdsLinked.php +++ b/includes/Modules/Analytics_4/Synchronize_AdsLinked.php @@ -94,8 +94,6 @@ protected function synchronize_ads_linked_data() { * Synchronize the adsLinked status. * * @since 1.124.0 - * - * @return null */ protected function synchronize_ads_linked_status() { $settings_ga4 = $this->analytics_4->get_settings()->get(); diff --git a/includes/Modules/Analytics_4/Tag_Guard.php b/includes/Modules/Analytics_4/Tag_Guard.php index 93d2ae00ea7..01bb53da4eb 100644 --- a/includes/Modules/Analytics_4/Tag_Guard.php +++ b/includes/Modules/Analytics_4/Tag_Guard.php @@ -26,7 +26,7 @@ class Tag_Guard extends Module_Tag_Guard { * * @since 1.31.0 * - * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. + * @return bool|\WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { $settings = $this->settings->get(); diff --git a/includes/Modules/PageSpeed_Insights.php b/includes/Modules/PageSpeed_Insights.php index e2d849ef055..61a03a84932 100644 --- a/includes/Modules/PageSpeed_Insights.php +++ b/includes/Modules/PageSpeed_Insights.php @@ -10,8 +10,10 @@ namespace Google\Site_Kit\Modules; +use Google\Site_Kit\Core\Assets\Asset; use Google\Site_Kit\Core\Assets\Script; use Google\Site_Kit\Core\Modules\Module; +use Google\Site_Kit\Core\Modules\Module_Settings; use Google\Site_Kit\Core\Modules\Module_With_Assets; use Google\Site_Kit\Core\Modules\Module_With_Assets_Trait; use Google\Site_Kit\Core\Modules\Module_With_Deactivation; diff --git a/includes/Modules/Reader_Revenue_Manager.php b/includes/Modules/Reader_Revenue_Manager.php index 9b3c69a9c27..332450e9fc2 100644 --- a/includes/Modules/Reader_Revenue_Manager.php +++ b/includes/Modules/Reader_Revenue_Manager.php @@ -20,6 +20,7 @@ use Google\Site_Kit\Core\Authentication\Clients\Google_Site_Kit_Client; use Google\Site_Kit\Core\Dismissals\Dismissed_Items; use Google\Site_Kit\Core\Modules\Module; +use Google\Site_Kit\Core\Modules\Module_Settings; use Google\Site_Kit\Core\Modules\Module_With_Assets; use Google\Site_Kit\Core\Modules\Module_With_Assets_Trait; use Google\Site_Kit\Core\Modules\Module_With_Deactivation; @@ -33,8 +34,10 @@ use Google\Site_Kit\Core\Modules\Module_With_Settings_Trait; use Google\Site_Kit\Core\Modules\Module_With_Tag; use Google\Site_Kit\Core\Modules\Module_With_Tag_Trait; +use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Matchers; use Google\Site_Kit\Core\Permissions\Permissions; use Google\Site_Kit\Core\REST_API\Data_Request; +use Google\Site_Kit\Core\REST_API\Exception\Invalid_Datapoint_Exception; use Google\Site_Kit\Core\REST_API\Exception\Missing_Required_Param_Exception; use Google\Site_Kit\Core\Site_Health\Debug_Data; use Google\Site_Kit\Core\Storage\Options; @@ -60,6 +63,7 @@ use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle as Google_Service_SubscribewithGoogle; use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle\PaymentOptions; use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle\Publication; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; /** diff --git a/includes/Modules/Reader_Revenue_Manager/Tag_Guard.php b/includes/Modules/Reader_Revenue_Manager/Tag_Guard.php index db8990608bf..ae28db9355d 100644 --- a/includes/Modules/Reader_Revenue_Manager/Tag_Guard.php +++ b/includes/Modules/Reader_Revenue_Manager/Tag_Guard.php @@ -51,7 +51,7 @@ public function __construct( Module_Settings $settings, $post_product_id ) { * * @since 1.132.0 * - * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. + * @return bool|\WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { $settings = $this->settings->get(); diff --git a/includes/Modules/Search_Console.php b/includes/Modules/Search_Console.php index 738e7629eab..b77b005560f 100644 --- a/includes/Modules/Search_Console.php +++ b/includes/Modules/Search_Console.php @@ -10,6 +10,7 @@ namespace Google\Site_Kit\Modules; +use Google\Site_Kit\Core\Assets\Asset; use Google\Site_Kit\Core\Assets\Script; use Google\Site_Kit\Core\Authentication\Clients\Google_Site_Kit_Client; use Google\Site_Kit\Core\Modules\Module; diff --git a/includes/Modules/Search_Console/Datapoints/SearchAnalyticsBatch.php b/includes/Modules/Search_Console/Datapoints/SearchAnalyticsBatch.php index 09317b96fc0..280b6563549 100644 --- a/includes/Modules/Search_Console/Datapoints/SearchAnalyticsBatch.php +++ b/includes/Modules/Search_Console/Datapoints/SearchAnalyticsBatch.php @@ -16,6 +16,7 @@ use Google\Site_Kit\Core\REST_API\Data_Request; use Google\Site_Kit\Core\REST_API\Exception\Missing_Required_Param_Exception; use Google\Site_Kit_Dependencies\Google\Service\Exception as Google_Service_Exception; +use Google\Site_Kit_Dependencies\Google\Service\SearchConsole as Google_Service_SearchConsole; use Google\Site_Kit_Dependencies\Google\Service\SearchConsole\SearchAnalyticsQueryResponse; use WP_Error; @@ -36,7 +37,7 @@ class SearchAnalyticsBatch extends Datapoint implements Executable_Datapoint { * Callback to obtain the Search Console service. * * @since 1.170.0 - * @var callable|Closure + * @var callable|\Closure */ private $get_service; @@ -44,7 +45,7 @@ class SearchAnalyticsBatch extends Datapoint implements Executable_Datapoint { * Callback to prepare single search analytics request arguments. * * @since 1.170.0 - * @var callable|Closure + * @var callable|\Closure */ private $prepare_args; @@ -52,7 +53,7 @@ class SearchAnalyticsBatch extends Datapoint implements Executable_Datapoint { * Callback to build a search analytics request. * * @since 1.170.0 - * @var callable|Closure + * @var callable|\Closure */ private $create_request; diff --git a/includes/Modules/Sign_In_With_Google.php b/includes/Modules/Sign_In_With_Google.php index f23cc98a2b6..d91baddbb52 100644 --- a/includes/Modules/Sign_In_With_Google.php +++ b/includes/Modules/Sign_In_With_Google.php @@ -66,6 +66,7 @@ final class Sign_In_With_Google extends Module implements Module_With_Inline_Dat use Method_Proxy_Trait; use Module_With_Assets_Trait; + /** @use Module_With_Settings_Trait */ use Module_With_Settings_Trait; use Module_With_Tag_Trait; use Module_With_Inline_Data_Trait; diff --git a/includes/Modules/Sign_In_With_Google/Tag_Guard.php b/includes/Modules/Sign_In_With_Google/Tag_Guard.php index 9c4eece4988..7be4750469f 100644 --- a/includes/Modules/Sign_In_With_Google/Tag_Guard.php +++ b/includes/Modules/Sign_In_With_Google/Tag_Guard.php @@ -26,7 +26,7 @@ class Tag_Guard extends Module_Tag_Guard { * * @since 1.159.0 * - * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. + * @return bool|\WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { $settings = $this->settings->get(); diff --git a/includes/Modules/Tag_Manager/Tag_Guard.php b/includes/Modules/Tag_Manager/Tag_Guard.php index 4ba904b0411..13966779222 100644 --- a/includes/Modules/Tag_Manager/Tag_Guard.php +++ b/includes/Modules/Tag_Manager/Tag_Guard.php @@ -48,7 +48,7 @@ public function __construct( Module_Settings $settings, $is_amp ) { * * @since 1.24.0 * - * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. + * @return bool|\WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { $settings = $this->settings->get(); diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 31dd64b7da0..0fdc4ad9fe6 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,7 +3,7 @@ includes: - vendor/szepeviktor/phpstan-wordpress/extension.neon parameters: - level: 1 + level: 2 treatPhpDocTypesAsCertain: false inferPrivatePropertyTypeFromConstructor: true paths: @@ -17,3 +17,5 @@ parameters: scanDirectories: - %currentWorkingDirectory%/gtg - %currentWorkingDirectory%/third-party + ignoreErrors: + - '#property.*Google\\Site_Kit_Dependencies\\Google\\Service.*#' From b1106f7b13be2d9e6013e472da5cabc17616424c Mon Sep 17 00:00:00 2001 From: Abdelmalek El Mellouki Date: Wed, 18 Mar 2026 15:25:40 +0000 Subject: [PATCH 2/4] Fix linting. --- includes/Modules/Sign_In_With_Google.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/includes/Modules/Sign_In_With_Google.php b/includes/Modules/Sign_In_With_Google.php index d91baddbb52..48312803c1f 100644 --- a/includes/Modules/Sign_In_With_Google.php +++ b/includes/Modules/Sign_In_With_Google.php @@ -66,7 +66,11 @@ final class Sign_In_With_Google extends Module implements Module_With_Inline_Dat use Method_Proxy_Trait; use Module_With_Assets_Trait; - /** @use Module_With_Settings_Trait */ + /** + * This module has a custom implementation of Module_Settings. + * + * @use Module_With_Settings_Trait + */ use Module_With_Settings_Trait; use Module_With_Tag_Trait; use Module_With_Inline_Data_Trait; From b421950f51b8d5e3f972674c918a9d97699a0fc6 Mon Sep 17 00:00:00 2001 From: Abdelmalek El Mellouki Date: Wed, 18 Mar 2026 15:27:59 +0000 Subject: [PATCH 3/4] Bump to level 3 and add baseline. --- phpstan.neon.dist | 3 +- tests/phpstan/phpstan-baseline.neon | 193 ++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 tests/phpstan/phpstan-baseline.neon diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 0fdc4ad9fe6..9fd07fd88fd 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,9 +1,10 @@ includes: - vendor/phpstan/phpstan/conf/bleedingEdge.neon - vendor/szepeviktor/phpstan-wordpress/extension.neon + - tests/phpstan/phpstan-baseline.neon parameters: - level: 2 + level: 3 treatPhpDocTypesAsCertain: false inferPrivatePropertyTypeFromConstructor: true paths: diff --git a/tests/phpstan/phpstan-baseline.neon b/tests/phpstan/phpstan-baseline.neon new file mode 100644 index 00000000000..238dea91c6c --- /dev/null +++ b/tests/phpstan/phpstan-baseline.neon @@ -0,0 +1,193 @@ +parameters: + ignoreErrors: + - + rawMessage: 'Property Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\WooCommerce::$add_to_cart (WC_Product) does not accept array.' + identifier: assign.propertyType + count: 1 + path: ../../includes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.php + + - + rawMessage: 'Method Google\Site_Kit\Core\Email\Email::send() should return bool|WP_Error but returns null.' + identifier: return.type + count: 1 + path: ../../includes/Core/Email/Email.php + + - + rawMessage: 'Method Google\Site_Kit\Core\Email_Reporting\Email_Report_Section_Builder::build_sections() should return array but returns WP_Error.' + identifier: return.type + count: 1 + path: ../../includes/Core/Email_Reporting/Email_Report_Section_Builder.php + + - + rawMessage: 'Method Google\Site_Kit\Core\Site_Health\Tag_Placement::check_if_tag_exists() should return bool but returns string.' + identifier: return.type + count: 4 + path: ../../includes/Core/Site_Health/Tag_Placement.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\AdSense::has_placed_tag_in_content() should return bool but returns int.' + identifier: return.type + count: 3 + path: ../../includes/Modules/AdSense.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Web_Tag::get_dns_prefetch_hints_callback() should return array but returns Closure.' + identifier: return.type + count: 1 + path: ../../includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\AdSense\Web_Tag::get_dns_prefetch_hints_callback() should return array but returns Closure.' + identifier: return.type + count: 1 + path: ../../includes/Modules/AdSense/Web_Tag.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Ads::has_placed_tag_in_content() should return bool but returns int.' + identifier: return.type + count: 3 + path: ../../includes/Modules/Ads.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4::get_analyticsadminv1alpha_service() should return Google\Site_Kit_Dependencies\Google\Service\GoogleAnalyticsAdminV1alpha but returns Google\Site_Kit_Dependencies\Google\Service.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4::get_analyticsdata_service() should return Google\Site_Kit_Dependencies\Google\Service\AnalyticsData but returns Google\Site_Kit_Dependencies\Google\Service.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4::get_tagmanager_service() should return Google\Site_Kit_Dependencies\Google\Service\TagManager but returns Google\Site_Kit_Dependencies\Google\Service.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4::has_placed_tag_in_content() should return bool but returns int.' + identifier: return.type + count: 3 + path: ../../includes/Modules/Analytics_4.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4::parse_webdatastreams_batch() should return array but returns array>.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4.php + + - + rawMessage: 'Property Google\Site_Kit\Modules\Analytics_4\AMP_Tag::$custom_dimensions (array) does not accept string.' + identifier: assign.propertyType + count: 1 + path: ../../includes/Modules/Analytics_4/AMP_Tag.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4\Report\RequestHelpers::get_metric_filter_expression() should return Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\FilterExpression but returns null.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4/Report/RequestHelpers.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4\Report\RequestHelpers::parse_dimension_filter() should return Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\FilterExpression but returns null.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4/Report/RequestHelpers.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4\Report\RequestHelpers::parse_metric_filter() should return Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\FilterExpression but returns null.' + identifier: return.type + count: 5 + path: ../../includes/Modules/Analytics_4/Report/RequestHelpers.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4\Report\RequestHelpers::parse_metric_filters() should return Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\FilterExpression but returns null.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Analytics_4/Report/RequestHelpers.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Analytics_4\Report\RequestHelpers::shared_create_request() should return Google\Site_Kit_Dependencies\Google\Service\AnalyticsData\RunReportRequest but returns WP_Error.' + identifier: return.type + count: 2 + path: ../../includes/Modules/Analytics_4/Report/RequestHelpers.php + + - + rawMessage: 'Property Google\Site_Kit\Modules\Analytics_4\Web_Tag::$custom_dimensions (array) does not accept string.' + identifier: assign.propertyType + count: 1 + path: ../../includes/Modules/Analytics_4/Web_Tag.php + + - + rawMessage: 'Argument of an invalid type Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle\PaymentOptions supplied for foreach, only iterables are supported.' + identifier: foreach.nonIterable + count: 1 + path: ../../includes/Modules/Reader_Revenue_Manager.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Reader_Revenue_Manager::has_placed_tag_in_content() should return bool but returns int.' + identifier: return.type + count: 3 + path: ../../includes/Modules/Reader_Revenue_Manager.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Reader_Revenue_Manager\Web_Tag::get_dns_prefetch_hints_callback() should return array but returns Closure.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Reader_Revenue_Manager/Web_Tag.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Search_Console::get_searchconsole_service() should return Google\Site_Kit_Dependencies\Google\Service\SearchConsole but returns Google\Site_Kit_Dependencies\Google\Service.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Search_Console.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Sign_In_With_Google::has_placed_tag_in_content() should return bool but returns int.' + identifier: return.type + count: 2 + path: ../../includes/Modules/Sign_In_With_Google.php + + - + rawMessage: 'Return type (bool) of method Google\Site_Kit\Modules\Sign_In_With_Google\Compatibility_Checks\WP_COM_Check::run() should be compatible with return type (array) of method Google\Site_Kit\Modules\Sign_In_With_Google\Compatibility_Checks\Compatibility_Check::run()' + identifier: method.childReturnType + count: 1 + path: ../../includes/Modules/Sign_In_With_Google/Compatibility_Checks/WP_COM_Check.php + + - + rawMessage: 'Return type (bool) of method Google\Site_Kit\Modules\Sign_In_With_Google\Compatibility_Checks\WP_Login_Accessible_Check::run() should be compatible with return type (array) of method Google\Site_Kit\Modules\Sign_In_With_Google\Compatibility_Checks\Compatibility_Check::run()' + identifier: method.childReturnType + count: 1 + path: ../../includes/Modules/Sign_In_With_Google/Compatibility_Checks/WP_Login_Accessible_Check.php + + - + rawMessage: 'Property Google\Site_Kit\Modules\Sign_In_With_Google\Web_Tag::$settings (Google\Site_Kit\Modules\Sign_In_With_Google\Settings) does not accept array.' + identifier: assign.propertyType + count: 1 + path: ../../includes/Modules/Sign_In_With_Google/Web_Tag.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Site_Verification::get_siteverification_service() should return Google\Site_Kit_Dependencies\Google\Service\SiteVerification but returns Google\Site_Kit_Dependencies\Google\Service.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Site_Verification.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Tag_Manager::get_tagmanager_service() should return Google\Site_Kit_Dependencies\Google\Service\TagManager but returns Google\Site_Kit_Dependencies\Google\Service.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Tag_Manager.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Tag_Manager::has_placed_tag_in_content() should return bool but returns int.' + identifier: return.type + count: 3 + path: ../../includes/Modules/Tag_Manager.php + + - + rawMessage: 'Method Google\Site_Kit\Modules\Tag_Manager\Web_Tag::get_dns_prefetch_hints_callback() should return array but returns Closure.' + identifier: return.type + count: 1 + path: ../../includes/Modules/Tag_Manager/Web_Tag.php From 0bd7a4293981ed8167491c02bc22f16a027d845e Mon Sep 17 00:00:00 2001 From: Abdelmalek El Mellouki Date: Mon, 23 Mar 2026 07:39:01 +0100 Subject: [PATCH 4/4] Refactor setup_services + get_service methods. --- includes/Core/Modules/Module.php | 13 ++-- includes/Modules/AdSense.php | 16 ++-- includes/Modules/Analytics_4.php | 85 ++++++++++----------- includes/Modules/PageSpeed_Insights.php | 6 +- includes/Modules/Reader_Revenue_Manager.php | 10 +-- includes/Modules/Search_Console.php | 4 +- includes/Modules/Site_Verification.php | 12 +-- includes/Modules/Tag_Manager.php | 14 ++-- phpstan.neon.dist | 2 - 9 files changed, 80 insertions(+), 82 deletions(-) diff --git a/includes/Core/Modules/Module.php b/includes/Core/Modules/Module.php index 7f67eef8c36..3eb55325f11 100644 --- a/includes/Core/Modules/Module.php +++ b/includes/Core/Modules/Module.php @@ -530,12 +530,13 @@ private function get_oauth_client_for_datapoint( Datapoint $datapoint ) { * * @since 1.0.0 * - * @param string $identifier Identifier for the service. - * @return Google_Service Google service instance. + * @template T of Google_Service + * @param class-string $class_name Identifier for the service. + * @return T Google service instance. * * @throws Exception Thrown when the module did not correctly set up the services or when the identifier is invalid. */ - final protected function get_service( $identifier ) { + final protected function get_service( $class_name ) { if ( null === $this->google_services ) { $services = $this->setup_services( $this->get_client() ); if ( ! is_array( $services ) ) { @@ -549,12 +550,12 @@ final protected function get_service( $identifier ) { $this->google_services = $services; } - if ( ! isset( $this->google_services[ $identifier ] ) ) { + if ( ! isset( $this->google_services[ $class_name ] ) ) { /* translators: %s: service identifier */ - throw new Exception( sprintf( __( 'Google service identified by %s does not exist.', 'google-site-kit' ), $identifier ) ); + throw new Exception( sprintf( __( 'Google service identified by %s does not exist.', 'google-site-kit' ), $class_name ) ); } - return $this->google_services[ $identifier ]; + return $this->google_services[ $class_name ]; } /** diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index 2371295f29c..234144d75d3 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -336,7 +336,7 @@ protected function get_datapoint_definitions() { protected function create_data_request( Data_Request $data ) { switch ( "{$data->method}:{$data->datapoint}" ) { case 'GET:accounts': - $service = $this->get_service( 'adsense' ); + $service = $this->get_service( Google_Service_Adsense::class ); return $service->accounts->listAccounts(); case 'GET:adunits': if ( ! isset( $data['accountID'] ) || ! isset( $data['clientID'] ) ) { @@ -352,14 +352,14 @@ protected function create_data_request( Data_Request $data ) { return new WP_Error( 'missing_required_param', sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'clientID' ), array( 'status' => 400 ) ); } } - $service = $this->get_service( 'adsense' ); + $service = $this->get_service( Google_Service_Adsense::class ); return $service->accounts_adclients_adunits->listAccountsAdclientsAdunits( self::normalize_client_id( $data['accountID'], $data['clientID'] ) ); case 'GET:alerts': if ( ! isset( $data['accountID'] ) ) { /* translators: %s: Missing parameter name */ return new WP_Error( 'missing_required_param', sprintf( __( 'Request parameter is empty: %s.', 'google-site-kit' ), 'accountID' ), array( 'status' => 400 ) ); } - $service = $this->get_service( 'adsense' ); + $service = $this->get_service( Google_Service_Adsense::class ); return $service->accounts_alerts->listAccountsAlerts( self::normalize_account_id( $data['accountID'] ) ); case 'GET:clients': if ( ! isset( $data['accountID'] ) ) { @@ -370,7 +370,7 @@ protected function create_data_request( Data_Request $data ) { array( 'status' => 400 ) ); } - $service = $this->get_service( 'adsense' ); + $service = $this->get_service( Google_Service_Adsense::class ); return $service->accounts_adclients->listAccountsAdclients( self::normalize_account_id( $data['accountID'] ) ); case 'GET:notifications': return function () { @@ -482,14 +482,14 @@ function ( Google_Service_Adsense_Alert $alert ) { array( 'status' => 400 ) ); } - $service = $this->get_service( 'adsense' ); + $service = $this->get_service( Google_Service_Adsense::class ); return $service->accounts_sites->listAccountsSites( self::normalize_account_id( $data['accountID'] ) ); case 'POST:sync-ad-blocking-recovery-tags': $settings = $this->get_settings()->get(); if ( empty( $settings['accountID'] ) ) { return new WP_Error( 'module_not_connected', __( 'Module is not connected.', 'google-site-kit' ), array( 'status' => 500 ) ); } - $service = $this->get_service( 'adsense' ); + $service = $this->get_service( Google_Service_Adsense::class ); return $service->accounts->getAdBlockingRecoveryTag( self::normalize_account_id( $settings['accountID'] ) ); } @@ -730,7 +730,7 @@ function ( $hostname ) { ) ); - return $this->get_service( 'adsense' ) + return $this->get_service( Google_Service_Adsense::class ) ->accounts_reports ->generate( self::normalize_account_id( $account_id ), @@ -774,7 +774,7 @@ protected function setup_info() { */ protected function setup_services( Google_Site_Kit_Client $client ) { return array( - 'adsense' => new Google_Service_Adsense( $client ), + Google_Service_Adsense::class => new Google_Service_Adsense( $client ), ); } diff --git a/includes/Modules/Analytics_4.php b/includes/Modules/Analytics_4.php index d4d43195be2..2387e3b9213 100644 --- a/includes/Modules/Analytics_4.php +++ b/includes/Modules/Analytics_4.php @@ -682,24 +682,24 @@ public function get_feature_metrics() { */ protected function get_datapoint_definitions() { $datapoints = array( - 'GET:account-summaries' => array( 'service' => 'analyticsadmin' ), - 'GET:accounts' => array( 'service' => 'analyticsadmin' ), - 'GET:ads-links' => array( 'service' => 'analyticsadmin' ), - 'GET:adsense-links' => array( 'service' => 'analyticsadmin-v1alpha' ), + 'GET:account-summaries' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:accounts' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:ads-links' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:adsense-links' => array( 'service' => GoogleAnalyticsAdminV1alpha::class ), 'GET:container-lookup' => array( - 'service' => 'tagmanager', + 'service' => Google_Service_TagManager::class, 'scopes' => array( 'https://www.googleapis.com/auth/tagmanager.readonly', ), ), 'GET:container-destinations' => array( - 'service' => 'tagmanager', + 'service' => Google_Service_TagManager::class, 'scopes' => array( 'https://www.googleapis.com/auth/tagmanager.readonly', ), ), 'GET:key-events' => array( - 'service' => 'analyticsadmin', + 'service' => Google_Service_GoogleAnalyticsAdmin::class, 'shareable' => true, ), 'POST:create-account-ticket' => new Create_Account_Ticket( @@ -707,14 +707,14 @@ protected function get_datapoint_definitions() { 'credentials' => $this->authentication->credentials()->get(), 'provisioning_redirect_uri' => $this->get_provisioning_redirect_uri(), 'service' => function () { - return $this->get_service( 'analyticsprovisioning' ); + return $this->get_service( AccountProvisioningService::class ); }, 'scopes' => array( self::EDIT_SCOPE ), 'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to create a new Analytics account on your behalf.', 'google-site-kit' ), ), ), 'GET:google-tag-settings' => array( - 'service' => 'tagmanager', + 'service' => Google_Service_TagManager::class, 'scopes' => array( 'https://www.googleapis.com/auth/tagmanager.readonly', ), @@ -723,7 +723,7 @@ protected function get_datapoint_definitions() { array( 'reference_site_url' => $this->context->get_reference_site_url(), 'service' => function () { - return $this->get_service( 'analyticsadmin' ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); }, 'scopes' => array( self::EDIT_SCOPE ), 'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to create a new Analytics property on your behalf.', 'google-site-kit' ), @@ -733,35 +733,35 @@ protected function get_datapoint_definitions() { array( 'reference_site_url' => $this->context->get_reference_site_url(), 'service' => function () { - return $this->get_service( 'analyticsadmin' ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); }, 'scopes' => array( self::EDIT_SCOPE ), 'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to create a new Analytics web data stream for this site on your behalf.', 'google-site-kit' ), ) ), - 'GET:properties' => array( 'service' => 'analyticsadmin' ), - 'GET:property' => array( 'service' => 'analyticsadmin' ), - 'GET:has-property-access' => array( 'service' => 'analyticsdata' ), + 'GET:properties' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:property' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:has-property-access' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), 'GET:report' => array( - 'service' => 'analyticsdata', + 'service' => Google_Service_GoogleAnalyticsAdmin::class, 'shareable' => true, ), 'GET:batch-report' => array( - 'service' => 'analyticsdata', + 'service' => Google_Service_GoogleAnalyticsAdmin::class, 'shareable' => true, ), - 'GET:webdatastreams' => array( 'service' => 'analyticsadmin' ), - 'GET:webdatastreams-batch' => array( 'service' => 'analyticsadmin' ), - 'GET:enhanced-measurement-settings' => array( 'service' => 'analyticsadmin-v1alpha' ), + 'GET:webdatastreams' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:webdatastreams-batch' => array( 'service' => Google_Service_GoogleAnalyticsAdmin::class ), + 'GET:enhanced-measurement-settings' => array( 'service' => GoogleAnalyticsAdminV1alpha::class ), 'POST:enhanced-measurement-settings' => array( - 'service' => 'analyticsadmin-v1alpha', + 'service' => GoogleAnalyticsAdminV1alpha::class, 'scopes' => array( self::EDIT_SCOPE ), 'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to update enhanced measurement settings for this Analytics web data stream on your behalf.', 'google-site-kit' ), ), 'POST:create-custom-dimension' => new Create_Custom_Dimension( array( 'service' => function () { - return $this->get_service( 'analyticsadmin' ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); }, 'scopes' => array( self::EDIT_SCOPE ), 'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to create a new Analytics custom dimension on your behalf.', 'google-site-kit' ), @@ -770,7 +770,7 @@ protected function get_datapoint_definitions() { 'POST:sync-custom-dimensions' => new Sync_Custom_Dimensions( array( 'service' => function () { - return $this->get_service( 'analyticsadmin' ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); }, 'settings' => $this->get_settings(), 'custom_dimensions_data_available' => $this->custom_dimensions_data_available, @@ -788,7 +788,7 @@ protected function get_datapoint_definitions() { 'service' => '', ), 'POST:create-audience' => array( - 'service' => 'analyticsadmin-v1alpha', + 'service' => GoogleAnalyticsAdminV1alpha::class, 'scopes' => array( self::EDIT_SCOPE ), 'request_scopes_message' => __( 'You’ll need to grant Site Kit permission to create new audiences for your Analytics property on your behalf.', 'google-site-kit' ), ), @@ -796,7 +796,7 @@ protected function get_datapoint_definitions() { 'service' => '', ), 'POST:sync-audiences' => array( - 'service' => 'analyticsadmin-v1alpha', + 'service' => GoogleAnalyticsAdminV1alpha::class, 'shareable' => true, ), 'GET:audience-settings' => array( @@ -844,7 +844,7 @@ private function create_property( $account_id, $options = array() ) { $property->setDisplayName( $display_name ); $property->setTimeZone( $timezone ); - return $this->get_service( 'analyticsadmin' )->properties->create( $property ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class )->properties->create( $property ); } /** @@ -878,8 +878,7 @@ private function create_webdatastream( $property_id, $options = array() ) { $datastream->setType( 'WEB_DATA_STREAM' ); $datastream->setWebStreamData( $data ); - /* @var Google_Service_GoogleAnalyticsAdmin $analyticsadmin phpcs:ignore Squiz.PHP.CommentedOutCode.Found */ - $analyticsadmin = $this->get_service( 'analyticsadmin' ); + $analyticsadmin = $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); return $analyticsadmin ->properties_dataStreams // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase @@ -1179,9 +1178,9 @@ protected function sync_google_tag_settings() { protected function create_data_request( Data_Request $data ) { switch ( "{$data->method}:{$data->datapoint}" ) { case 'GET:accounts': - return $this->get_service( 'analyticsadmin' )->accounts->listAccounts(); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class )->accounts->listAccounts(); case 'GET:account-summaries': - return $this->get_service( 'analyticsadmin' )->accountSummaries->listAccountSummaries( + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class )->accountSummaries->listAccountSummaries( array( 'pageSize' => 200, 'pageToken' => $data['pageToken'], @@ -1194,7 +1193,7 @@ protected function create_data_request( Data_Request $data ) { $parent = self::normalize_property_id( $data['propertyID'] ); - return $this->get_service( 'analyticsadmin' )->properties_googleAdsLinks->listPropertiesGoogleAdsLinks( $parent ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class )->properties_googleAdsLinks->listPropertiesGoogleAdsLinks( $parent ); case 'GET:adsense-links': if ( empty( $data['propertyID'] ) ) { throw new Missing_Required_Param_Exception( 'propertyID' ); @@ -1262,7 +1261,7 @@ protected function create_data_request( Data_Request $data ) { ); } - return $this->get_service( 'analyticsadmin' )->properties->listProperties( + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class )->properties->listProperties( array( 'filter' => 'parent:' . self::normalize_account_id( $data['accountID'] ), 'pageSize' => 200, @@ -1278,7 +1277,7 @@ protected function create_data_request( Data_Request $data ) { ); } - return $this->get_service( 'analyticsadmin' )->properties->get( self::normalize_property_id( $data['propertyID'] ) ); + return $this->get_service( Google_Service_GoogleAnalyticsAdmin::class )->properties->get( self::normalize_property_id( $data['propertyID'] ) ); case 'GET:has-property-access': if ( ! isset( $data['propertyID'] ) ) { throw new Missing_Required_Param_Exception( 'propertyID' ); @@ -1577,7 +1576,7 @@ protected function create_data_request( Data_Request $data ) { ); } - $analyticsadmin = $this->get_service( 'analyticsadmin' ); + $analyticsadmin = $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); return $analyticsadmin ->properties_dataStreams // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase @@ -1603,7 +1602,7 @@ protected function create_data_request( Data_Request $data ) { ); } - $analyticsadmin = $this->get_service( 'analyticsadmin' ); + $analyticsadmin = $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); $batch_request = $analyticsadmin->createBatch(); foreach ( $data['propertyIDs'] as $property_id ) { @@ -1672,7 +1671,7 @@ protected function create_data_request( Data_Request $data ) { ); } - $analyticsadmin = $this->get_service( 'analyticsadmin' ); + $analyticsadmin = $this->get_service( Google_Service_GoogleAnalyticsAdmin::class ); $property_id = self::normalize_property_id( $settings['propertyID'] ); return $analyticsadmin @@ -1776,7 +1775,7 @@ protected function parse_data_response( Data_Request $data, $response ) { * @throws Exception Thrown if the module did not correctly set up the service. */ private function get_tagmanager_service() { - return $this->get_service( 'tagmanager' ); + return $this->get_service( Google_Service_TagManager::class ); } /** @@ -1804,7 +1803,7 @@ protected function setup_info() { * @return GoogleAnalyticsAdminV1alpha */ protected function get_analyticsadminv1alpha_service(): GoogleAnalyticsAdminV1alpha { - return $this->get_service( 'analyticsadmin-v1alpha' ); + return $this->get_service( GoogleAnalyticsAdminV1alpha::class ); } /** @@ -1815,7 +1814,7 @@ protected function get_analyticsadminv1alpha_service(): GoogleAnalyticsAdminV1al * @return Google_Service_AnalyticsData The Analytics Data API service. */ protected function get_analyticsdata_service() { - return $this->get_service( 'analyticsdata' ); + return $this->get_service( Google_Service_AnalyticsData::class ); } /** @@ -1834,11 +1833,11 @@ protected function setup_services( Google_Site_Kit_Client $client ) { $google_proxy = $this->authentication->get_google_proxy(); return array( - 'analyticsadmin' => new Google_Service_GoogleAnalyticsAdmin( $client ), - 'analyticsadmin-v1alpha' => new GoogleAnalyticsAdminV1alpha( $client ), - 'analyticsdata' => new Google_Service_AnalyticsData( $client ), - 'analyticsprovisioning' => new AccountProvisioningService( $client, $google_proxy->url() ), - 'tagmanager' => new Google_Service_TagManager( $client ), + Google_Service_GoogleAnalyticsAdmin::class => new Google_Service_GoogleAnalyticsAdmin( $client ), + GoogleAnalyticsAdminV1alpha::class => new GoogleAnalyticsAdminV1alpha( $client ), + Google_Service_AnalyticsData::class => new Google_Service_AnalyticsData( $client ), + AccountProvisioningService::class => new AccountProvisioningService( $client, $google_proxy->url() ), + Google_Service_TagManager::class => new Google_Service_TagManager( $client ), ); } diff --git a/includes/Modules/PageSpeed_Insights.php b/includes/Modules/PageSpeed_Insights.php index 61a03a84932..8195e7c4e1b 100644 --- a/includes/Modules/PageSpeed_Insights.php +++ b/includes/Modules/PageSpeed_Insights.php @@ -75,7 +75,7 @@ public function on_deactivation() { protected function get_datapoint_definitions() { return array( 'GET:pagespeed' => array( - 'service' => 'pagespeedonline', + 'service' => Google_Service_PagespeedInsights::class, 'shareable' => true, ), ); @@ -127,7 +127,7 @@ protected function create_data_request( Data_Request $data ) { $page_url = $this->context->get_reference_site_url(); } - $service = $this->get_service( 'pagespeedonline' ); + $service = $this->get_service( Google_Service_PagespeedInsights::class ); return $service->pagespeedapi->runpagespeed( $page_url, @@ -213,7 +213,7 @@ protected function setup_settings() { */ protected function setup_services( Google_Site_Kit_Client $client ) { return array( - 'pagespeedonline' => new Google_Service_PagespeedInsights( $client ), + Google_Service_PagespeedInsights::class => new Google_Service_PagespeedInsights( $client ), ); } diff --git a/includes/Modules/Reader_Revenue_Manager.php b/includes/Modules/Reader_Revenue_Manager.php index 332450e9fc2..30e37cce840 100644 --- a/includes/Modules/Reader_Revenue_Manager.php +++ b/includes/Modules/Reader_Revenue_Manager.php @@ -259,7 +259,7 @@ public function get_scopes() { */ public function setup_services( Google_Site_Kit_Client $client ) { return array( - 'subscribewithgoogle' => new Google_Service_SubscribewithGoogle( $client ), + Google_Service_SubscribewithGoogle::class => new Google_Service_SubscribewithGoogle( $client ), ); } @@ -314,7 +314,7 @@ public function check_service_entity_access() { * * @var Google_Service_SubscribewithGoogle */ - $subscribewithgoogle = $this->get_service( 'subscribewithgoogle' ); + $subscribewithgoogle = $this->get_service( Google_Service_SubscribewithGoogle::class ); try { $response = $subscribewithgoogle->publications->listPublications(); @@ -353,10 +353,10 @@ public function check_service_entity_access() { protected function get_datapoint_definitions() { return array( 'GET:publications' => array( - 'service' => 'subscribewithgoogle', + 'service' => Google_Service_SubscribewithGoogle::class, ), 'POST:sync-publication-onboarding-state' => array( - 'service' => 'subscribewithgoogle', + 'service' => Google_Service_SubscribewithGoogle::class, ), ); } @@ -379,7 +379,7 @@ protected function create_data_request( Data_Request $data ) { * * @var Google_Service_SubscribewithGoogle */ - $subscribewithgoogle = $this->get_service( 'subscribewithgoogle' ); + $subscribewithgoogle = $this->get_service( Google_Service_SubscribewithGoogle::class ); return $subscribewithgoogle->publications->listPublications( array( 'filter' => $this->get_publication_filter() ) ); case 'POST:sync-publication-onboarding-state': diff --git a/includes/Modules/Search_Console.php b/includes/Modules/Search_Console.php index b77b005560f..65d7dbdea26 100644 --- a/includes/Modules/Search_Console.php +++ b/includes/Modules/Search_Console.php @@ -557,7 +557,7 @@ protected function setup_info() { * @return Google_Service_SearchConsole The Search Console API service. */ private function get_searchconsole_service() { - return $this->get_service( 'searchconsole' ); + return $this->get_service( Google_Service_SearchConsole::class ); } /** @@ -575,7 +575,7 @@ private function get_searchconsole_service() { */ protected function setup_services( Google_Site_Kit_Client $client ) { return array( - 'searchconsole' => new Google_Service_SearchConsole( $client ), + Google_Service_SearchConsole::class => new Google_Service_SearchConsole( $client ), ); } diff --git a/includes/Modules/Site_Verification.php b/includes/Modules/Site_Verification.php index ab8e3ac5061..0ccd3efa7f2 100644 --- a/includes/Modules/Site_Verification.php +++ b/includes/Modules/Site_Verification.php @@ -146,10 +146,10 @@ public function get_scopes() { */ protected function get_datapoint_definitions() { return array( - 'GET:verification' => array( 'service' => 'siteverification' ), - 'POST:verification' => array( 'service' => 'siteverification' ), - 'GET:verification-token' => array( 'service' => 'siteverification' ), - 'GET:verified-sites' => array( 'service' => 'siteverification' ), + 'GET:verification' => array( 'service' => Google_Service_SiteVerification::class ), + 'POST:verification' => array( 'service' => Google_Service_SiteVerification::class ), + 'GET:verification-token' => array( 'service' => Google_Service_SiteVerification::class ), + 'GET:verified-sites' => array( 'service' => Google_Service_SiteVerification::class ), ); } @@ -367,7 +367,7 @@ protected function setup_info() { * @return Google_Service_SiteVerification The Site Verification API service. */ private function get_siteverification_service() { - return $this->get_service( 'siteverification' ); + return $this->get_service( Google_Service_SiteVerification::class ); } /** @@ -385,7 +385,7 @@ private function get_siteverification_service() { */ protected function setup_services( Google_Site_Kit_Client $client ) { return array( - 'siteverification' => new Google_Service_SiteVerification( $client ), + Google_Service_SiteVerification::class => new Google_Service_SiteVerification( $client ), ); } diff --git a/includes/Modules/Tag_Manager.php b/includes/Modules/Tag_Manager.php index b15c6bc8bcc..a81be42b1a9 100644 --- a/includes/Modules/Tag_Manager.php +++ b/includes/Modules/Tag_Manager.php @@ -274,15 +274,15 @@ public static function sanitize_container_name( $name ) { */ protected function get_datapoint_definitions() { return array( - 'GET:accounts' => array( 'service' => 'tagmanager' ), - 'GET:accounts-containers' => array( 'service' => 'tagmanager' ), - 'GET:containers' => array( 'service' => 'tagmanager' ), + 'GET:accounts' => array( 'service' => Google_Service_TagManager::class ), + 'GET:accounts-containers' => array( 'service' => Google_Service_TagManager::class ), + 'GET:containers' => array( 'service' => Google_Service_TagManager::class ), 'POST:create-container' => array( - 'service' => 'tagmanager', + 'service' => Google_Service_TagManager::class, 'scopes' => array( 'https://www.googleapis.com/auth/tagmanager.edit.containers' ), 'request_scopes_message' => __( 'Additional permissions are required to create a new Tag Manager container on your behalf.', 'google-site-kit' ), ), - 'GET:live-container-version' => array( 'service' => 'tagmanager' ), + 'GET:live-container-version' => array( 'service' => Google_Service_TagManager::class ), ); } @@ -495,7 +495,7 @@ function ( Google_Service_TagManager_Container $container ) use ( $usage_context * @throws Exception Thrown if the module did not correctly set up the service. */ public function get_tagmanager_service() { - return $this->get_service( 'tagmanager' ); + return $this->get_service( Google_Service_TagManager::class ); } /** @@ -529,7 +529,7 @@ protected function setup_info() { */ protected function setup_services( Google_Site_Kit_Client $client ) { return array( - 'tagmanager' => new Google_Service_TagManager( $client ), + Google_Service_TagManager::class => new Google_Service_TagManager( $client ), ); } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9fd07fd88fd..3ef5a69ebcc 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -18,5 +18,3 @@ parameters: scanDirectories: - %currentWorkingDirectory%/gtg - %currentWorkingDirectory%/third-party - ignoreErrors: - - '#property.*Google\\Site_Kit_Dependencies\\Google\\Service.*#'