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 WordPress version. |
||
| 37 | * |
||
| 38 | * @since 2.9.3 |
||
| 39 | * |
||
| 40 | * GravityView will require this version of WordPress soon. |
||
| 41 | */ |
||
| 42 | private static $future_min_wp_version = GV_FUTURE_MIN_WP_VERSION; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string Minimum Gravity Forms version. |
||
| 46 | * |
||
| 47 | * GravityView requires at least this version of Gravity Forms to function properly. |
||
| 48 | */ |
||
| 49 | public static $min_gf_version = GV_MIN_GF_VERSION; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string Minimum PHP version. |
||
| 53 | * |
||
| 54 | * GravityView requires at least this version of PHP to function properly. |
||
| 55 | */ |
||
| 56 | private static $min_php_version = GV_MIN_PHP_VERSION; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string|bool Minimum future PHP version. |
||
| 60 | * |
||
| 61 | * GravityView will require this version of PHP soon. False if no future PHP version changes are planned. |
||
| 62 | */ |
||
| 63 | private static $future_min_php_version = GV_FUTURE_MIN_PHP_VERSION; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string|bool Minimum future Gravity Forms version. |
||
| 67 | * |
||
| 68 | * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned. |
||
| 69 | */ |
||
| 70 | private static $future_min_gf_version = GV_FUTURE_MIN_GF_VERSION; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var \GV\Plugin The \GV\Plugin static instance. |
||
| 74 | */ |
||
| 75 | private static $__instance = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @since 2.0 |
||
| 79 | * @api |
||
| 80 | * @var \GV\Addon_Settings The plugin "addon" settings. |
||
| 81 | * |
||
| 82 | */ |
||
| 83 | public $settings; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string The GFQuery functionality identifier. |
||
| 87 | */ |
||
| 88 | const FEATURE_GFQUERY = 'gfquery'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string The joins functionality identifier. |
||
| 92 | */ |
||
| 93 | const FEATURE_JOINS = 'joins'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string The unions functionality identifier. |
||
| 97 | */ |
||
| 98 | const FEATURE_UNIONS = 'unions'; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string The REST API functionality identifier. |
||
| 102 | */ |
||
| 103 | const FEATURE_REST = 'rest_api'; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the global instance of \GV\Plugin. |
||
| 107 | * |
||
| 108 | * @return \GV\Plugin The global instance of GravityView Plugin. |
||
| 109 | */ |
||
| 110 | public static function get() { |
||
| 118 | |||
| 119 | private function __construct() { |
||
| 136 | |||
| 137 | public function load_license_settings() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Check whether Gravity Forms is v2.5-beta or newer |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | * @todo add @since |
||
| 154 | * |
||
| 155 | */ |
||
| 156 | public function is_GF_25() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Check whether GravityView `is network activated. |
||
| 163 | * |
||
| 164 | * @return bool Whether it's network activated or not. |
||
| 165 | */ |
||
| 166 | public static function is_network_activated() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Include more legacy stuff. |
||
| 175 | * |
||
| 176 | * @param boolean $force Whether to force the includes. |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function include_legacy_frontend( $force = false ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Load more legacy core files. |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function include_legacy_core() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Load the translations. |
||
| 272 | * |
||
| 273 | * Order of look-ups: |
||
| 274 | * |
||
| 275 | * 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core) |
||
| 276 | * 2. /wp-content/mu-plugins/gravityview-{locale}.mo |
||
| 277 | * 3. /wp-content/mu-plugins/languages/gravityview-{locale}.mo |
||
| 278 | * 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | public function load_textdomain() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Register hooks that are fired when the plugin is activated and deactivated. |
||
| 311 | * |
||
| 312 | 1 | * @return void |
|
| 313 | 1 | */ |
|
| 314 | public function register_activation_hooks() { |
||
| 319 | |||
| 320 | 1 | /** |
|
| 321 | * Plugin activation function. |
||
| 322 | 1 | * |
|
| 323 | * @return void |
||
| 324 | * @internal |
||
| 325 | 1 | */ |
|
| 326 | public function activate() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Plugin deactivation function. |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | * @internal |
||
| 361 | */ |
||
| 362 | public function deactivate() { |
||
| 366 | |||
| 367 | 1 | /** |
|
| 368 | * Retrieve an absolute path within the GravityView plugin directory. |
||
| 369 | 1 | * |
|
| 370 | * @since 2.0 |
||
| 371 | * |
||
| 372 | * @param string $path Optional. Append this extra path component. |
||
| 373 | * @return string The absolute path to the plugin directory. |
||
| 374 | * @api |
||
| 375 | */ |
||
| 376 | public function dir( $path = '' ) { |
||
| 380 | |||
| 381 | 1 | /** |
|
| 382 | 1 | * Retrieve a relative path to the GravityView plugin directory from the WordPress plugin directory |
|
| 383 | * |
||
| 384 | * @since 2.2.3 |
||
| 385 | * |
||
| 386 | * @param string $path Optional. Append this extra path component. |
||
| 387 | * @return string The relative path to the plugin directory from the plugin directory. |
||
| 388 | * @api |
||
| 389 | */ |
||
| 390 | public function relpath( $path = '' ) { |
||
| 396 | 5 | ||
| 397 | 5 | /** |
|
| 398 | * Retrieve a URL within the GravityView plugin directory. |
||
| 399 | * |
||
| 400 | * @since 2.0 |
||
| 401 | * |
||
| 402 | * @param string $path Optional. Extra path appended to the URL. |
||
| 403 | * @return string The URL to this plugin, with trailing slash. |
||
| 404 | * @api |
||
| 405 | */ |
||
| 406 | public function url( $path = '/' ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Is everything compatible with this version of GravityView? |
||
| 413 | * |
||
| 414 | * @since 2.0 |
||
| 415 | * |
||
| 416 | * @return bool |
||
| 417 | * @api |
||
| 418 | */ |
||
| 419 | public function is_compatible() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Is this version of GravityView compatible with the current version of PHP? |
||
| 429 | * |
||
| 430 | * @since 2.0 |
||
| 431 | * |
||
| 432 | * @return bool true if compatible, false otherwise. |
||
| 433 | * @api |
||
| 434 | 5 | */ |
|
| 435 | public function is_compatible_php() { |
||
| 439 | |||
| 440 | 5 | /** |
|
| 441 | * Is this version of GravityView compatible with the future required version of PHP? |
||
| 442 | * |
||
| 443 | * @since 2.0 |
||
| 444 | * |
||
| 445 | * @return bool true if compatible, false otherwise. |
||
| 446 | * @api |
||
| 447 | */ |
||
| 448 | public function is_compatible_future_php() { |
||
| 452 | 5 | ||
| 453 | 5 | /** |
|
| 454 | * Is this version of GravityView compatible with the current version of WordPress? |
||
| 455 | * |
||
| 456 | * @since 2.0 |
||
| 457 | * |
||
| 458 | * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION |
||
| 459 | * |
||
| 460 | * @return bool true if compatible, false otherwise. |
||
| 461 | * @api |
||
| 462 | */ |
||
| 463 | public function is_compatible_wordpress( $version = null ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Is this version of GravityView compatible with the future version of WordPress? |
||
| 474 | * |
||
| 475 | * @since 2.9.3 |
||
| 476 | 4 | * |
|
| 477 | 4 | * @return bool true if compatible, false otherwise |
|
| 478 | 4 | * @api |
|
| 479 | */ |
||
| 480 | public function is_compatible_future_wordpress() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | 4 | * Is this version of GravityView compatible with the current version of Gravity Forms? |
|
| 489 | 4 | * |
|
| 490 | 4 | * @since 2.0 |
|
| 491 | * |
||
| 492 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
| 493 | * @api |
||
| 494 | */ |
||
| 495 | public function is_compatible_gravityforms() { |
||
| 501 | 4 | ||
| 502 | /** |
||
| 503 | * Is this version of GravityView compatible with the future version of Gravity Forms? |
||
| 504 | * |
||
| 505 | * @since 2.0 |
||
| 506 | 4 | * |
|
| 507 | 4 | * @return bool true if compatible, false otherwise (or not active/installed). |
|
| 508 | * @api |
||
| 509 | */ |
||
| 510 | public function is_compatible_future_gravityforms() { |
||
| 516 | |||
| 517 | 194 | /** |
|
| 518 | 194 | * Retrieve the current PHP version. |
|
| 519 | * |
||
| 520 | * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing. |
||
| 521 | * |
||
| 522 | * @return string The version of PHP. |
||
| 523 | 194 | */ |
|
| 524 | 74 | private function get_php_version() { |
|
| 529 | 194 | ||
| 530 | /** |
||
| 531 | * Retrieve the current WordPress version. |
||
| 532 | * |
||
| 533 | * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing. |
||
| 534 | * |
||
| 535 | * @return string The version of WordPress. |
||
| 536 | */ |
||
| 537 | private function get_wordpress_version() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Retrieve the current Gravity Forms version. |
||
| 545 | * |
||
| 546 | * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing. |
||
| 547 | * |
||
| 548 | * @return string|null The version of Gravity Forms or null if inactive. |
||
| 549 | */ |
||
| 550 | private function get_gravityforms_version() { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Feature support detection. |
||
| 564 | * |
||
| 565 | * @param string $feature Feature name. Check FEATURE_* class constants. |
||
| 566 | * |
||
| 567 | * @return boolean |
||
| 568 | */ |
||
| 569 | public function supports( $feature ) { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Delete GravityView Views, settings, roles, caps, etc. |
||
| 590 | * |
||
| 591 | * @return void |
||
| 592 | */ |
||
| 593 | public function uninstall() { |
||
| 678 | |||
| 679 | private function __clone() { |
||
| 681 | |||
| 682 | private function __wakeup() { |
||
| 684 | } |
||
| 685 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.