| Total Complexity | 50 |
| Total Lines | 520 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Lite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Lite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | final class MonsterInsights_Lite { |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Holds the class object. |
||
| 60 | * |
||
| 61 | * @since 6.0.0 |
||
| 62 | * @access public |
||
| 63 | * @var object Instance of instantiated MonsterInsights class. |
||
| 64 | */ |
||
| 65 | public static $instance; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Plugin version, used for cache-busting of style and script file references. |
||
| 69 | * |
||
| 70 | * @since 6.0.0 |
||
| 71 | * @access public |
||
| 72 | * @var string $version Plugin version. |
||
| 73 | */ |
||
| 74 | public $version = '9.1.1'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Plugin file. |
||
| 78 | * |
||
| 79 | * @since 6.0.0 |
||
| 80 | * @access public |
||
| 81 | * @var string $file PHP File constant for main file. |
||
| 82 | */ |
||
| 83 | public $file; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The name of the plugin. |
||
| 87 | * |
||
| 88 | * @since 6.0.0 |
||
| 89 | * @access public |
||
| 90 | * @var string $plugin_name Plugin name. |
||
| 91 | */ |
||
| 92 | public $plugin_name = 'MonsterInsights Lite'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Unique plugin slug identifier. |
||
| 96 | * |
||
| 97 | * @since 6.0.0 |
||
| 98 | * @access public |
||
| 99 | * @var string $plugin_slug Plugin slug. |
||
| 100 | */ |
||
| 101 | public $plugin_slug = 'monsterinsights-lite'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Holds instance of MonsterInsights License class. |
||
| 105 | * |
||
| 106 | * @since 6.0.0 |
||
| 107 | * @access public |
||
| 108 | * @var MonsterInsights_License $license Instance of License class. |
||
| 109 | */ |
||
| 110 | protected $license; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Holds instance of MonsterInsights Admin Notice class. |
||
| 114 | * |
||
| 115 | * @since 6.0.0 |
||
| 116 | * @access public |
||
| 117 | * @var MonsterInsights_Admin_Notice $notices Instance of Admin Notice class. |
||
|
|
|||
| 118 | */ |
||
| 119 | public $notices; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Holds instance of MonsterInsights Reporting class. |
||
| 123 | * |
||
| 124 | * @since 6.0.0 |
||
| 125 | * @access public |
||
| 126 | * @var MonsterInsights_Reporting $reporting Instance of Reporting class. |
||
| 127 | */ |
||
| 128 | public $reporting; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Holds instance of MonsterInsights Notifications class. |
||
| 132 | * |
||
| 133 | * @since 7.11 |
||
| 134 | * @access public |
||
| 135 | * @var MonsterInsights_Notifications $notifications Instance of Notifications class. |
||
| 136 | */ |
||
| 137 | public $notifications; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Holds instance of MonsterInsights Notification Events |
||
| 141 | * |
||
| 142 | * @since 7.12.3 |
||
| 143 | * @access public |
||
| 144 | * @var MonsterInsights_Notification_Event $notification_event Instance of MonsterInsights_Notification_Event class. |
||
| 145 | */ |
||
| 146 | public $notification_event; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Holds instance of MonsterInsights Auth class. |
||
| 150 | * |
||
| 151 | * @since 7.0.0 |
||
| 152 | * @access public |
||
| 153 | * @var MonsterInsights_Auth $auth Instance of Auth class. |
||
| 154 | */ |
||
| 155 | protected $auth; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Holds instance of MonsterInsights API Auth class. |
||
| 159 | * |
||
| 160 | * @since 6.0.0 |
||
| 161 | * @access public |
||
| 162 | * @var MonsterInsights_Auth $api_auth Instance of APIAuth class. |
||
| 163 | */ |
||
| 164 | public $api_auth; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Holds instance of MonsterInsights API Rest Routes class. |
||
| 168 | * |
||
| 169 | * @since 7.4.0 |
||
| 170 | * @access public |
||
| 171 | * @var MonsterInsights_Rest_Routes $routes Instance of rest routes. |
||
| 172 | */ |
||
| 173 | public $routes; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * The tracking mode used in the frontend. |
||
| 177 | * |
||
| 178 | * @since 7.15.0 |
||
| 179 | * @accces public |
||
| 180 | * @var string |
||
| 181 | * @deprecated Since 8.3 with the removal of ga compatibility |
||
| 182 | */ |
||
| 183 | public $tracking_mode; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Setup checklist class property. |
||
| 187 | * |
||
| 188 | * @var MonsterInsights_Setup_Checklist |
||
| 189 | */ |
||
| 190 | public $setup_checklist; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Primary class constructor. |
||
| 194 | * |
||
| 195 | * @since 6.0.0 |
||
| 196 | * @access public |
||
| 197 | */ |
||
| 198 | public function __construct() { |
||
| 199 | // We don't use this |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns the singleton instance of the class. |
||
| 204 | * |
||
| 205 | * @access public |
||
| 206 | * @return object The MonsterInsights_Lite object. |
||
| 207 | * @since 6.0.0 |
||
| 208 | * |
||
| 209 | */ |
||
| 210 | public static function get_instance() { |
||
| 211 | |||
| 212 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof MonsterInsights_Lite ) ) { |
||
| 213 | self::$instance = new MonsterInsights_Lite(); |
||
| 214 | self::$instance->file = __FILE__; |
||
| 215 | |||
| 216 | // Detect Pro version and return early |
||
| 217 | if ( defined( 'MONSTERINSIGHTS_PRO_VERSION' ) ) { |
||
| 218 | add_action( 'admin_notices', array( self::$instance, 'monsterinsights_pro_notice' ) ); |
||
| 219 | |||
| 220 | return self::$instance; |
||
| 221 | } |
||
| 222 | |||
| 223 | // Define constants |
||
| 224 | self::$instance->define_globals(); |
||
| 225 | |||
| 226 | // Load in settings |
||
| 227 | self::$instance->load_settings(); |
||
| 228 | |||
| 229 | // Compatibility check |
||
| 230 | if ( ! self::$instance->check_compatibility() ) { |
||
| 231 | return self::$instance; |
||
| 232 | } |
||
| 233 | |||
| 234 | // Load in Licensing |
||
| 235 | self::$instance->load_licensing(); |
||
| 236 | |||
| 237 | // Load in Auth |
||
| 238 | self::$instance->load_auth(); |
||
| 239 | |||
| 240 | // Load files |
||
| 241 | self::$instance->require_files(); |
||
| 242 | |||
| 243 | // This does the version to version background upgrade routines and initial install |
||
| 244 | $mi_version = get_option( 'monsterinsights_current_version', '5.5.3' ); |
||
| 245 | if ( version_compare( $mi_version, '8.13.0', '<' ) ) { |
||
| 246 | monsterinsights_lite_call_install_and_upgrade(); |
||
| 247 | } |
||
| 248 | |||
| 249 | if ( is_admin() ) { |
||
| 250 | new AM_Deactivation_Survey( 'MonsterInsights', basename( __DIR__ ) ); |
||
| 251 | } |
||
| 252 | |||
| 253 | // Load the plugin textdomain. |
||
| 254 | add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ), 15 ); |
||
| 255 | |||
| 256 | // Load admin only components. |
||
| 257 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
| 258 | self::$instance->notices = new MonsterInsights_Notice_Admin(); |
||
| 259 | self::$instance->reporting = new MonsterInsights_Reporting(); |
||
| 260 | self::$instance->api_auth = new MonsterInsights_API_Auth(); |
||
| 261 | self::$instance->routes = new MonsterInsights_Rest_Routes(); |
||
| 262 | self::$instance->notifications = new MonsterInsights_Notifications(); |
||
| 263 | self::$instance->notification_event = new MonsterInsights_Notification_Event(); |
||
| 264 | self::$instance->setup_checklist = new MonsterInsights_Setup_Checklist(); |
||
| 265 | } |
||
| 266 | |||
| 267 | if ( monsterinsights_is_pro_version() ) { |
||
| 268 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/load.php'; |
||
| 269 | } else { |
||
| 270 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/load.php'; |
||
| 271 | } |
||
| 272 | |||
| 273 | // Run hook to load MonsterInsights addons. |
||
| 274 | do_action( 'monsterinsights_load_plugins' ); // the updater class for each addon needs to be instantiated via `monsterinsights_updater` |
||
| 275 | } |
||
| 276 | |||
| 277 | return self::$instance; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Throw error on object clone |
||
| 282 | * |
||
| 283 | * The whole idea of the singleton design pattern is that there is a single |
||
| 284 | * object therefore, we don't want the object to be cloned. |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | * @since 6.0.0 |
||
| 288 | * @access public |
||
| 289 | * |
||
| 290 | */ |
||
| 291 | public function __clone() { |
||
| 292 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Disable unserializing of the class |
||
| 297 | * |
||
| 298 | * Attempting to wakeup an MonsterInsights instance will throw a doing it wrong notice. |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | * @since 6.0.0 |
||
| 302 | * @access public |
||
| 303 | * |
||
| 304 | */ |
||
| 305 | public function __wakeup() { |
||
| 306 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Magic get function. |
||
| 311 | * |
||
| 312 | * We use this to lazy load certain functionality. Right now used to lazyload |
||
| 313 | * the API & Auth frontend, so it's only loaded if user is using a plugin |
||
| 314 | * that requires it. |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | * @since 7.0.0 |
||
| 318 | * @access public |
||
| 319 | * |
||
| 320 | */ |
||
| 321 | public function __get( $key ) { |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check compatibility with PHP and WP, and display notices if necessary |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | * @since 8.0.0 |
||
| 340 | */ |
||
| 341 | private function check_compatibility() { |
||
| 342 | if ( defined( 'MONSTERINSIGHTS_FORCE_ACTIVATION' ) && MONSTERINSIGHTS_FORCE_ACTIVATION ) { |
||
| 343 | return true; |
||
| 344 | } |
||
| 345 | |||
| 346 | require_once plugin_dir_path( __FILE__ ) . 'includes/compatibility-check.php'; |
||
| 347 | $compatibility = MonsterInsights_Compatibility_Check::get_instance(); |
||
| 348 | $compatibility->maybe_display_notice(); |
||
| 349 | |||
| 350 | return $compatibility->is_php_compatible() && $compatibility->is_wp_compatible(); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Define MonsterInsights constants. |
||
| 355 | * |
||
| 356 | * This function defines all of the MonsterInsights PHP constants. |
||
| 357 | * |
||
| 358 | * @return void |
||
| 359 | * @since 6.0.0 |
||
| 360 | * @access public |
||
| 361 | * |
||
| 362 | */ |
||
| 363 | public function define_globals() { |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Loads the plugin textdomain for translations. |
||
| 396 | * |
||
| 397 | * @access public |
||
| 398 | * @return void |
||
| 399 | * @since 6.0.0 |
||
| 400 | * |
||
| 401 | */ |
||
| 402 | public function load_textdomain() { |
||
| 403 | $plugin_text_domain = 'google-analytics-for-wordpress'; |
||
| 404 | |||
| 405 | $plugin_dir = basename( __DIR__ ); |
||
| 406 | |||
| 407 | load_plugin_textdomain( |
||
| 408 | $plugin_text_domain, |
||
| 409 | false, |
||
| 410 | $plugin_dir . '/languages' |
||
| 411 | ); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Output a nag notice if the user has both Lite and Pro activated |
||
| 416 | * |
||
| 417 | * @access public |
||
| 418 | * @return void |
||
| 419 | * @since 6.0.0 |
||
| 420 | * |
||
| 421 | */ |
||
| 422 | public function monsterinsights_pro_notice() { |
||
| 423 | $url = admin_url( 'plugins.php' ); |
||
| 424 | // Check for MS dashboard |
||
| 425 | if ( is_network_admin() ) { |
||
| 426 | $url = network_admin_url( 'plugins.php' ); |
||
| 427 | } |
||
| 428 | ?> |
||
| 429 | <div class="error"> |
||
| 430 | <p><?php echo sprintf(esc_html__('Please %1$suninstall%2$s the MonsterInsights Lite Plugin. Your Pro version of MonsterInsights may not work as expected until the Lite version is uninstalled.', 'google-analytics-for-wordpress'), '<a href="' . $url . '">', '</a>'); // phpcs:ignore ?></p> |
||
| 431 | </div> |
||
| 432 | <?php |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Loads MonsterInsights settings |
||
| 437 | * |
||
| 438 | * Adds the items to the base object, and adds the helper functions. |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | * @since 6.0.0 |
||
| 442 | * @access public |
||
| 443 | * |
||
| 444 | */ |
||
| 445 | public function load_settings() { |
||
| 446 | global $monsterinsights_settings; |
||
| 447 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/options.php'; |
||
| 448 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/helpers.php'; |
||
| 449 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/deprecated.php'; |
||
| 450 | $monsterinsights_settings = monsterinsights_get_options(); |
||
| 451 | } |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Loads MonsterInsights License |
||
| 456 | * |
||
| 457 | * Loads license class used by MonsterInsights |
||
| 458 | * |
||
| 459 | * @return void |
||
| 460 | * @since 7.0.0 |
||
| 461 | * @access public |
||
| 462 | * |
||
| 463 | */ |
||
| 464 | public function load_licensing() { |
||
| 465 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
| 466 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/license-compat.php'; |
||
| 467 | self::$instance->license = new MonsterInsights_License_Compat(); |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Loads MonsterInsights Auth |
||
| 473 | * |
||
| 474 | * Loads auth used by MonsterInsights |
||
| 475 | * |
||
| 476 | * @return void |
||
| 477 | * @since 7.0.0 |
||
| 478 | * @access public |
||
| 479 | * |
||
| 480 | */ |
||
| 481 | public function load_auth() { |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Loads all files into scope. |
||
| 490 | * |
||
| 491 | * @access public |
||
| 492 | * @return void |
||
| 493 | * @since 6.0.0 |
||
| 494 | * |
||
| 495 | */ |
||
| 496 | public function require_files() { |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get the tracking mode for the frontend scripts. |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | * @deprecated Since 8.3 with the removal of ga compatibility |
||
| 566 | */ |
||
| 567 | public function get_tracking_mode() { |
||
| 575 | } |
||
| 576 | } |
||
| 577 | |||
| 833 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths