Complex classes like Plugin 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | final class Plugin { |
||
| 18 | /** |
||
| 19 | * @var string The plugin version. |
||
| 20 | * |
||
| 21 | * @api |
||
| 22 | * @since 2.0 |
||
| 23 | */ |
||
| 24 | public static $version = GV_PLUGIN_VERSION; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string Minimum WordPress version. |
||
| 28 | * |
||
| 29 | * GravityView requires at least this version of WordPress to function properly. |
||
| 30 | */ |
||
| 31 | private static $min_wp_version = GV_MIN_WP_VERSION; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string Minimum Gravity Forms version. |
||
| 35 | * |
||
| 36 | * GravityView requires at least this version of Gravity Forms to function properly. |
||
| 37 | */ |
||
| 38 | public static $min_gf_version = GV_MIN_GF_VERSION; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string Minimum PHP version. |
||
| 42 | * |
||
| 43 | * GravityView requires at least this version of PHP to function properly. |
||
| 44 | */ |
||
| 45 | private static $min_php_version = GV_MIN_PHP_VERSION; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string|bool Minimum future PHP version. |
||
| 49 | * |
||
| 50 | * GravityView will require this version of PHP soon. False if no future PHP version changes are planned. |
||
| 51 | */ |
||
| 52 | private static $future_min_php_version = GV_FUTURE_MIN_PHP_VERSION; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string|bool Minimum future Gravity Forms version. |
||
| 56 | * |
||
| 57 | * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned. |
||
| 58 | */ |
||
| 59 | private static $future_min_gf_version = GV_FUTURE_MIN_GF_VERSION; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \GV\Plugin The \GV\Plugin static instance. |
||
| 63 | */ |
||
| 64 | private static $__instance = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \GV\Addon_Settings The plugin "addon" settings. |
||
| 68 | * |
||
| 69 | * @api |
||
| 70 | * @since 2.0 |
||
| 71 | */ |
||
| 72 | public $settings; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string The GFQuery functionality identifier. |
||
| 76 | */ |
||
| 77 | const FEATURE_GFQUERY = 'gfquery'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string The joins functionality identifier. |
||
| 81 | */ |
||
| 82 | const FEATURE_JOINS = 'joins'; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string The unions functionality identifier. |
||
| 86 | */ |
||
| 87 | const FEATURE_UNIONS = 'unions'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string The REST API functionality identifier. |
||
| 91 | */ |
||
| 92 | const FEATURE_REST = 'rest_api'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the global instance of \GV\Plugin. |
||
| 96 | * |
||
| 97 | * @return \GV\Plugin The global instance of GravityView Plugin. |
||
| 98 | */ |
||
| 99 | public static function get() { |
||
| 105 | |||
| 106 | |||
| 107 | private function __construct() { |
||
| 123 | |||
| 124 | public function load_license_settings() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Check whether GravityView is network activated. |
||
| 137 | * |
||
| 138 | * @return bool Whether it's network activated or not. |
||
| 139 | */ |
||
| 140 | 1 | public static function is_network_activated() { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Include more legacy stuff. |
||
| 149 | * |
||
| 150 | * @param boolean $force Whether to force the includes. |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function include_legacy_frontend( $force = false ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Load more legacy core files. |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | public function include_legacy_core() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Load the translations. |
||
| 241 | * |
||
| 242 | * Order of look-ups: |
||
| 243 | * |
||
| 244 | * 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core) |
||
| 245 | * 2. /wp-content/mu-plugins/gravityview-{locale}.mo |
||
| 246 | * 3. /wp-content/mu-plugins/languages/gravityview-{locale}.mo |
||
| 247 | * 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function load_textdomain() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Register hooks that are fired when the plugin is activated and deactivated. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function register_activation_hooks() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Plugin activation function. |
||
| 300 | * |
||
| 301 | * @internal |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | 1 | public function activate() { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Plugin deactivation function. |
||
| 335 | * |
||
| 336 | * @internal |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public function deactivate() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Retrieve an absolute path within the GravityView plugin directory. |
||
| 345 | * |
||
| 346 | * @api |
||
| 347 | * @since 2.0 |
||
| 348 | * |
||
| 349 | * @param string $path Optional. Append this extra path component. |
||
| 350 | * @return string The absolute path to the plugin directory. |
||
| 351 | */ |
||
| 352 | 113 | public function dir( $path = '' ) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Retrieve a relative path to the GravityView plugin directory from the WordPress plugin directory |
||
| 358 | * |
||
| 359 | * @api |
||
| 360 | * @since 2.2.3 |
||
| 361 | * |
||
| 362 | * @param string $path Optional. Append this extra path component. |
||
| 363 | * @return string The relative path to the plugin directory from the plugin directory. |
||
| 364 | */ |
||
| 365 | 1 | public function relpath( $path = '' ) { |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Retrieve a URL within the GravityView plugin directory. |
||
| 374 | * |
||
| 375 | * @api |
||
| 376 | * @since 2.0 |
||
| 377 | * |
||
| 378 | * @param string $path Optional. Extra path appended to the URL. |
||
| 379 | * @return string The URL to this plugin, with trailing slash. |
||
| 380 | */ |
||
| 381 | 1 | public function url( $path = '/' ) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Is everything compatible with this version of GravityView? |
||
| 387 | * |
||
| 388 | * @api |
||
| 389 | * @since 2.0 |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | 4 | public function is_compatible() { |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Is this version of GravityView compatible with the current version of PHP? |
||
| 402 | * |
||
| 403 | * @api |
||
| 404 | * @since 2.0 |
||
| 405 | * |
||
| 406 | * @return bool true if compatible, false otherwise. |
||
| 407 | */ |
||
| 408 | 4 | public function is_compatible_php() { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Is this version of GravityView compatible with the future required version of PHP? |
||
| 414 | * |
||
| 415 | * @api |
||
| 416 | * @since 2.0 |
||
| 417 | * |
||
| 418 | * @return bool true if compatible, false otherwise. |
||
| 419 | */ |
||
| 420 | public function is_compatible_future_php() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Is this version of GravityView compatible with the current version of WordPress? |
||
| 426 | * |
||
| 427 | * @api |
||
| 428 | * @since 2.0 |
||
| 429 | * |
||
| 430 | * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION |
||
| 431 | * |
||
| 432 | * @return bool true if compatible, false otherwise. |
||
| 433 | */ |
||
| 434 | 4 | public function is_compatible_wordpress( $version = null ) { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Is this version of GravityView compatible with the current version of Gravity Forms? |
||
| 445 | * |
||
| 446 | * @api |
||
| 447 | * @since 2.0 |
||
| 448 | * |
||
| 449 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
| 450 | */ |
||
| 451 | 4 | public function is_compatible_gravityforms() { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Is this version of GravityView compatible with the future version of Gravity Forms? |
||
| 458 | * |
||
| 459 | * @api |
||
| 460 | * @since 2.0 |
||
| 461 | * |
||
| 462 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
| 463 | */ |
||
| 464 | public function is_compatible_future_gravityforms() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Retrieve the current PHP version. |
||
| 471 | * |
||
| 472 | * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing. |
||
| 473 | * |
||
| 474 | * @return string The version of PHP. |
||
| 475 | */ |
||
| 476 | 3 | private function get_php_version() { |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Retrieve the current WordPress version. |
||
| 483 | * |
||
| 484 | * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing. |
||
| 485 | * |
||
| 486 | * @return string The version of WordPress. |
||
| 487 | */ |
||
| 488 | 3 | private function get_wordpress_version() { |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Retrieve the current Gravity Forms version. |
||
| 495 | * |
||
| 496 | * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing. |
||
| 497 | * |
||
| 498 | * @return string|null The version of Gravity Forms or null if inactive. |
||
| 499 | */ |
||
| 500 | 3 | private function get_gravityforms_version() { |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Feature support detection. |
||
| 512 | * |
||
| 513 | * @param string $feature Feature name. Check FEATURE_* class constants. |
||
| 514 | * |
||
| 515 | * @return boolean |
||
| 516 | */ |
||
| 517 | 188 | public function supports( $feature ) { |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Delete GravityView Views, settings, roles, caps, etc. |
||
| 537 | * |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | public function uninstall() { |
||
| 623 | |||
| 624 | private function __clone() { } |
||
| 625 | |||
| 626 | private function __wakeup() { } |
||
| 627 | } |
||
| 628 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: