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 |
||
| 18 | final class Plugin { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @since 2.0 |
||
| 22 | * @api |
||
| 23 | * @var string The plugin version. |
||
| 24 | * |
||
| 25 | */ |
||
| 26 | public static $version = GV_PLUGIN_VERSION; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string Minimum WordPress version. |
||
| 30 | * |
||
| 31 | * GravityView requires at least this version of WordPress to function properly. |
||
| 32 | */ |
||
| 33 | private static $min_wp_version = GV_MIN_WP_VERSION; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string Minimum Gravity Forms version. |
||
| 37 | * |
||
| 38 | * GravityView requires at least this version of Gravity Forms to function properly. |
||
| 39 | */ |
||
| 40 | public static $min_gf_version = GV_MIN_GF_VERSION; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Minimum PHP version. |
||
| 44 | * |
||
| 45 | * GravityView requires at least this version of PHP to function properly. |
||
| 46 | */ |
||
| 47 | private static $min_php_version = GV_MIN_PHP_VERSION; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string|bool Minimum future PHP version. |
||
| 51 | * |
||
| 52 | * GravityView will require this version of PHP soon. False if no future PHP version changes are planned. |
||
| 53 | */ |
||
| 54 | private static $future_min_php_version = GV_FUTURE_MIN_PHP_VERSION; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string|bool Minimum future Gravity Forms version. |
||
| 58 | * |
||
| 59 | * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned. |
||
| 60 | */ |
||
| 61 | private static $future_min_gf_version = GV_FUTURE_MIN_GF_VERSION; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \GV\Plugin The \GV\Plugin static instance. |
||
| 65 | */ |
||
| 66 | private static $__instance = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @since 2.0 |
||
| 70 | * @api |
||
| 71 | * @var \GV\Addon_Settings The plugin "addon" settings. |
||
| 72 | * |
||
| 73 | */ |
||
| 74 | public $settings; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string The GFQuery functionality identifier. |
||
| 78 | */ |
||
| 79 | const FEATURE_GFQUERY = 'gfquery'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string The joins functionality identifier. |
||
| 83 | */ |
||
| 84 | const FEATURE_JOINS = 'joins'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string The unions functionality identifier. |
||
| 88 | */ |
||
| 89 | const FEATURE_UNIONS = 'unions'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string The REST API functionality identifier. |
||
| 93 | */ |
||
| 94 | const FEATURE_REST = 'rest_api'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the global instance of \GV\Plugin. |
||
| 98 | * |
||
| 99 | * @return \GV\Plugin The global instance of GravityView Plugin. |
||
| 100 | */ |
||
| 101 | public static function get() { |
||
| 109 | |||
| 110 | private function __construct() { |
||
| 127 | |||
| 128 | public function load_license_settings() { |
||
| 139 | |||
| 140 | 1 | /** |
|
| 141 | * Check whether Gravity Forms is v2.5-beta or newer |
||
| 142 | 1 | * |
|
| 143 | * @return bool |
||
| 144 | 1 | * @todo add @since |
|
| 145 | * |
||
| 146 | */ |
||
| 147 | public function is_GF_25() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check whether GravityView `is network activated. |
||
| 154 | * |
||
| 155 | * @return bool Whether it's network activated or not. |
||
| 156 | */ |
||
| 157 | public static function is_network_activated() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Include more legacy stuff. |
||
| 166 | * |
||
| 167 | * @param boolean $force Whether to force the includes. |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function include_legacy_frontend( $force = false ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Load more legacy core files. |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function include_legacy_core() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Load the translations. |
||
| 263 | * |
||
| 264 | * Order of look-ups: |
||
| 265 | * |
||
| 266 | * 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core) |
||
| 267 | * 2. /wp-content/mu-plugins/gravityview-{locale}.mo |
||
| 268 | * 3. /wp-content/mu-plugins/languages/gravityview-{locale}.mo |
||
| 269 | * 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public function load_textdomain() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Register hooks that are fired when the plugin is activated and deactivated. |
||
| 312 | 1 | * |
|
| 313 | 1 | * @return void |
|
| 314 | */ |
||
| 315 | public function register_activation_hooks() { |
||
| 320 | 1 | ||
| 321 | /** |
||
| 322 | 1 | * Plugin activation function. |
|
| 323 | * |
||
| 324 | * @return void |
||
| 325 | 1 | * @internal |
|
| 326 | */ |
||
| 327 | public function activate() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Plugin deactivation function. |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | * @internal |
||
| 362 | */ |
||
| 363 | public function deactivate() { |
||
| 367 | 1 | ||
| 368 | /** |
||
| 369 | 1 | * Retrieve an absolute path within the GravityView plugin directory. |
|
| 370 | * |
||
| 371 | * @since 2.0 |
||
| 372 | * |
||
| 373 | * @param string $path Optional. Append this extra path component. |
||
| 374 | * @return string The absolute path to the plugin directory. |
||
| 375 | * @api |
||
| 376 | */ |
||
| 377 | public function dir( $path = '' ) { |
||
| 381 | 1 | ||
| 382 | 1 | /** |
|
| 383 | * Retrieve a relative path to the GravityView plugin directory from the WordPress plugin directory |
||
| 384 | * |
||
| 385 | * @since 2.2.3 |
||
| 386 | * |
||
| 387 | * @param string $path Optional. Append this extra path component. |
||
| 388 | * @return string The relative path to the plugin directory from the plugin directory. |
||
| 389 | * @api |
||
| 390 | */ |
||
| 391 | public function relpath( $path = '' ) { |
||
| 397 | 5 | ||
| 398 | /** |
||
| 399 | * Retrieve a URL within the GravityView plugin directory. |
||
| 400 | * |
||
| 401 | * @since 2.0 |
||
| 402 | * |
||
| 403 | * @param string $path Optional. Extra path appended to the URL. |
||
| 404 | * @return string The URL to this plugin, with trailing slash. |
||
| 405 | * @api |
||
| 406 | */ |
||
| 407 | public function url( $path = '/' ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Is everything compatible with this version of GravityView? |
||
| 414 | * |
||
| 415 | * @since 2.0 |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | * @api |
||
| 419 | */ |
||
| 420 | public function is_compatible() { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Is this version of GravityView compatible with the current version of PHP? |
||
| 430 | * |
||
| 431 | * @since 2.0 |
||
| 432 | * |
||
| 433 | * @return bool true if compatible, false otherwise. |
||
| 434 | 5 | * @api |
|
| 435 | */ |
||
| 436 | 5 | public function is_compatible_php() { |
|
| 440 | 5 | ||
| 441 | /** |
||
| 442 | * Is this version of GravityView compatible with the future required version of PHP? |
||
| 443 | * |
||
| 444 | * @since 2.0 |
||
| 445 | * |
||
| 446 | * @return bool true if compatible, false otherwise. |
||
| 447 | * @api |
||
| 448 | */ |
||
| 449 | public function is_compatible_future_php() { |
||
| 453 | 5 | ||
| 454 | /** |
||
| 455 | * Is this version of GravityView compatible with the current version of WordPress? |
||
| 456 | * |
||
| 457 | * @since 2.0 |
||
| 458 | * |
||
| 459 | * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION |
||
| 460 | * |
||
| 461 | * @return bool true if compatible, false otherwise. |
||
| 462 | * @api |
||
| 463 | */ |
||
| 464 | public function is_compatible_wordpress( $version = null ) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Is this version of GravityView compatible with the current version of Gravity Forms? |
||
| 475 | * |
||
| 476 | 4 | * @since 2.0 |
|
| 477 | 4 | * |
|
| 478 | 4 | * @return bool true if compatible, false otherwise (or not active/installed). |
|
| 479 | * @api |
||
| 480 | */ |
||
| 481 | public function is_compatible_gravityforms() { |
||
| 487 | |||
| 488 | 4 | /** |
|
| 489 | 4 | * Is this version of GravityView compatible with the future version of Gravity Forms? |
|
| 490 | 4 | * |
|
| 491 | * @since 2.0 |
||
| 492 | * |
||
| 493 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
| 494 | * @api |
||
| 495 | */ |
||
| 496 | public function is_compatible_future_gravityforms() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Retrieve the current PHP version. |
||
| 505 | * |
||
| 506 | 4 | * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing. |
|
| 507 | 4 | * |
|
| 508 | * @return string The version of PHP. |
||
| 509 | */ |
||
| 510 | private function get_php_version() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | 194 | * Retrieve the current WordPress version. |
|
| 518 | 194 | * |
|
| 519 | * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing. |
||
| 520 | * |
||
| 521 | * @return string The version of WordPress. |
||
| 522 | */ |
||
| 523 | 194 | private function get_wordpress_version() { |
|
| 528 | 194 | ||
| 529 | 194 | /** |
|
| 530 | * Retrieve the current Gravity Forms version. |
||
| 531 | * |
||
| 532 | * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing. |
||
| 533 | * |
||
| 534 | * @return string|null The version of Gravity Forms or null if inactive. |
||
| 535 | */ |
||
| 536 | private function get_gravityforms_version() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Feature support detection. |
||
| 550 | * |
||
| 551 | * @param string $feature Feature name. Check FEATURE_* class constants. |
||
| 552 | * |
||
| 553 | * @return boolean |
||
| 554 | */ |
||
| 555 | public function supports( $feature ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Delete GravityView Views, settings, roles, caps, etc. |
||
| 576 | * |
||
| 577 | * @return void |
||
| 578 | */ |
||
| 579 | public function uninstall() { |
||
| 664 | |||
| 665 | private function __clone() { |
||
| 667 | |||
| 668 | private function __wakeup() { |
||
| 670 | } |
||
| 671 |
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: