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.6.0'; |
||
| 31 | |||
| 32 | /** @var string plugin ID */ |
||
| 33 | CONST ID = 'wp-php-console'; |
||
| 34 | |||
| 35 | /** @var string plugin name */ |
||
| 36 | CONST NAME = 'WP PHP Console'; |
||
| 37 | |||
| 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() { |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Connects to PHP Console. |
||
| 83 | * |
||
| 84 | * PHP Console needs to hook in session, in WordPress we need to be in 'init': |
||
| 85 | * @link http://silvermapleweb.com/using-the-php-session-in-wordpress/ |
||
| 86 | * |
||
| 87 | * @internal action hook callback |
||
| 88 | * |
||
| 89 | * @since 1.4.0 |
||
| 90 | */ |
||
| 91 | public function connect() { |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * Applies options. |
||
| 122 | * |
||
| 123 | * @since 1.4.0 |
||
| 124 | */ |
||
| 125 | private function apply_options() { |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Initializes PHP Console. |
||
| 160 | * |
||
| 161 | * @internal action hook callback |
||
| 162 | * |
||
| 163 | * @since 1.0.0 |
||
| 164 | */ |
||
| 165 | public function init() { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Prints an exception message as WordPress admin notice. |
||
| 263 | * |
||
| 264 | * @since 1.4.0 |
||
| 265 | * |
||
| 266 | * @param \Exception $e Exception object |
||
| 267 | */ |
||
| 268 | private function print_notice_exception( \Exception $e ) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the plugin path. |
||
| 282 | * |
||
| 283 | * @since 1.6.0 |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public static function get_plugin_path() { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Gets the plugin vendor path. |
||
| 295 | * |
||
| 296 | * @since 1.6.0 |
||
| 297 | */ |
||
| 298 | public static function get_plugin_vendor_path() { |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Gets the plugin page URL. |
||
| 306 | * |
||
| 307 | * @since 1.6.0 |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public static function get_plugin_page_url() { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Gets the plugin reviews page URL. |
||
| 319 | * |
||
| 320 | * @since 1.6.0 |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public static function get_reviews_page_url() { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Gets the plugin support page URL. |
||
| 332 | * |
||
| 333 | * @since 1.6.0 |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public static function get_support_page_url() { |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Gets the GitHub repository page URL. |
||
| 345 | * |
||
| 346 | * @since 1.6.0 |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public static function get_wp_php_console_repository_url() { |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Gets the PHP Console project page URL. |
||
| 358 | * |
||
| 359 | * @since 1.6.0 |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public static function get_php_console_repository_url() { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets the PHP Console Google Chrome extension URL. |
||
| 371 | * |
||
| 372 | * @since 1.6.0 |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public static function get_php_console_chrome_extension_web_store_url() { |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Gets the PHP Console Google Chrome extension repository URL. |
||
| 384 | * |
||
| 385 | * @since 1.6.0 |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public static function get_php_console_chrome_extension_repository_url() { |
||
| 393 | |||
| 394 | |||
| 395 | } |
||
| 396 |
If you suppress an error, we recommend checking for the error condition explicitly: