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 |
||
| 26 | class Plugin { |
||
| 27 | |||
| 28 | |||
| 29 | /** @var string plugin version */ |
||
| 30 | CONST VERSION = '1.5.5'; |
||
| 31 | |||
| 32 | /** @var string plugin name */ |
||
| 33 | CONST NAME = 'WP PHP Console'; |
||
| 34 | |||
| 35 | |||
| 36 | /** @var array settings options */ |
||
| 37 | private $options = []; |
||
| 38 | |||
| 39 | /** @var PhpConsole\Connector instance */ |
||
| 40 | public $connector; |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Loads plugin and connects to PHP Console. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | */ |
||
| 48 | public function __construct() { |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Sets constants and elevates PHP error reporting. |
||
| 59 | * |
||
| 60 | * @since 1.5.4 |
||
| 61 | */ |
||
| 62 | private function set_debug_mode() { |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Sets the plugin options. |
||
| 76 | * |
||
| 77 | * @since 1.5.4 |
||
| 78 | */ |
||
| 79 | private function set_options() { |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Sets plugin text domain. |
||
| 87 | * |
||
| 88 | * @internal action hook callback |
||
| 89 | * |
||
| 90 | * @since 1.0.0 |
||
| 91 | */ |
||
| 92 | public function set_locale() { |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Loads admin. |
||
| 104 | * |
||
| 105 | * @since 1.5.0 |
||
| 106 | */ |
||
| 107 | private function set_admin() { |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Sets plugin hooks. |
||
| 129 | * |
||
| 130 | * @since 1.5.4 |
||
| 131 | */ |
||
| 132 | private function set_hooks() { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Connects to PHP Console. |
||
| 150 | * |
||
| 151 | * PHP Console needs to hook in session, in WordPress we need to be in 'init': |
||
| 152 | * @link http://silvermapleweb.com/using-the-php-session-in-wordpress/ |
||
| 153 | * |
||
| 154 | * @internal action hook callback |
||
| 155 | * |
||
| 156 | * @since 1.4.0 |
||
| 157 | */ |
||
| 158 | public function connect() { |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Get WP PHP Console settings options. |
||
| 189 | * |
||
| 190 | * @since 1.4.0 |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | private function get_options() { |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Applies options. |
||
| 211 | * |
||
| 212 | * @since 1.4.0 |
||
| 213 | */ |
||
| 214 | private function apply_options() { |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Initializes PHP Console. |
||
| 249 | * |
||
| 250 | * @internal action hook callback |
||
| 251 | * |
||
| 252 | * @since 1.0.0 |
||
| 253 | */ |
||
| 254 | public function init() { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Prints an exception message as WordPress admin notice. |
||
| 357 | * |
||
| 358 | * @since 1.4.0 |
||
| 359 | * |
||
| 360 | * @param \Exception $e Exception object |
||
| 361 | */ |
||
| 362 | public function print_notice_exception( \Exception $e ) { |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Admin password notice. |
||
| 378 | * |
||
| 379 | * Prompts user to set a password for PHP Console upon plugin activation. |
||
| 380 | * |
||
| 381 | * @internal action hook callback |
||
| 382 | * |
||
| 383 | * @since 1.3.2 |
||
| 384 | */ |
||
| 385 | public function password_notice() { |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * Gets the plugin path. |
||
| 403 | * |
||
| 404 | * @since 1.5.5 |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public static function get_plugin_path() { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Gets the plugin vendor path. |
||
| 416 | * |
||
| 417 | * @since 1.5.5 |
||
| 418 | */ |
||
| 419 | public static function get_plugin_vendor_path() { |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the plugin page URL. |
||
| 427 | * |
||
| 428 | * @since 1.5.5 |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public static function get_plugin_page_url() { |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * Gets the GitHub repository page URL. |
||
| 440 | * |
||
| 441 | * @since 1.5.5 |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public static function get_project_page_url() { |
||
| 449 | |||
| 450 | |||
| 451 | /** |
||
| 452 | * Gets the plugin reviews page URL. |
||
| 453 | * |
||
| 454 | * @since 1.5.5 |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public static function get_reviews_page_url() { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Gets the plugin support page URL. |
||
| 466 | * |
||
| 467 | * @since 1.5.5 |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public static function get_support_page_url() { |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * Gets the admin settings page URL. |
||
| 479 | * |
||
| 480 | * @since 1.5.5 |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public static function get_settings_page_url() { |
||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Determines if the current page is the settings page. |
||
| 492 | * |
||
| 493 | * @since 1.5.5 |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | public static function is_settings_page() { |
||
| 501 | |||
| 502 | |||
| 503 | } |
||
| 504 |
If you suppress an error, we recommend checking for the error condition explicitly: