Complex classes like WordPress_GitHub_Sync 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 WordPress_GitHub_Sync, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class WordPress_GitHub_Sync { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Object instance. |
||
| 52 | * |
||
| 53 | * @var self |
||
| 54 | */ |
||
| 55 | public static $instance; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Language text domain. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public static $text_domain = 'wp-github-sync'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Current version. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public static $version = '2.0.1'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Controller object. |
||
| 73 | * |
||
| 74 | * @var WordPress_GitHub_Sync_Controller |
||
| 75 | */ |
||
| 76 | public $controller; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Admin object. |
||
| 80 | * |
||
| 81 | * @var WordPress_GitHub_Sync_Admin |
||
| 82 | */ |
||
| 83 | public $admin; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * CLI object. |
||
| 87 | * |
||
| 88 | * @var WordPress_GitHub_Sync_CLI |
||
| 89 | */ |
||
| 90 | protected $cli; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Request object. |
||
| 94 | * |
||
| 95 | * @var WordPress_GitHub_Sync_Request |
||
| 96 | */ |
||
| 97 | protected $request; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Response object. |
||
| 101 | * |
||
| 102 | * @var WordPress_GitHub_Sync_Response |
||
| 103 | */ |
||
| 104 | protected $response; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Api object. |
||
| 108 | * |
||
| 109 | * @var WordPress_GitHub_Sync_Api |
||
| 110 | */ |
||
| 111 | protected $api; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Import object. |
||
| 115 | * |
||
| 116 | * @var WordPress_GitHub_Sync_Import |
||
| 117 | */ |
||
| 118 | protected $import; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Export object. |
||
| 122 | * |
||
| 123 | * @var WordPress_GitHub_Sync_Export |
||
| 124 | */ |
||
| 125 | protected $export; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Semaphore object. |
||
| 129 | * |
||
| 130 | * @var WordPress_GitHub_Sync_Semaphore |
||
| 131 | */ |
||
| 132 | protected $semaphore; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Database object. |
||
| 136 | * |
||
| 137 | * @var WordPress_GitHub_Sync_Database |
||
| 138 | */ |
||
| 139 | protected $database; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Cache object. |
||
| 143 | * |
||
| 144 | * @var WordPress_GitHub_Sync_Cache |
||
| 145 | */ |
||
| 146 | protected $cache; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Called at load time, hooks into WP core |
||
| 150 | */ |
||
| 151 | public function __construct() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Attaches the plugin's hooks into WordPress. |
||
| 167 | */ |
||
| 168 | public function boot() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Init i18n files |
||
| 188 | */ |
||
| 189 | public function l10n() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Sets and kicks off the export cronjob |
||
| 195 | */ |
||
| 196 | public function start_export() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Sets and kicks off the import cronjob |
||
| 203 | */ |
||
| 204 | public function start_import() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Enables the admin notice on initial activation |
||
| 210 | */ |
||
| 211 | public function activate() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Displays the activation admin notice |
||
| 219 | */ |
||
| 220 | public function activation_notice() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the Controller object. |
||
| 241 | * |
||
| 242 | * @return WordPress_GitHub_Sync_Controller |
||
| 243 | */ |
||
| 244 | public function controller() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Lazy-load the CLI object. |
||
| 250 | * |
||
| 251 | * @return WordPress_GitHub_Sync_CLI |
||
| 252 | */ |
||
| 253 | public function cli() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Lazy-load the Request object. |
||
| 263 | * |
||
| 264 | * @return WordPress_GitHub_Sync_Request |
||
| 265 | */ |
||
| 266 | public function request() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Lazy-load the Response object. |
||
| 276 | * |
||
| 277 | * @return WordPress_GitHub_Sync_Response |
||
| 278 | */ |
||
| 279 | public function response() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Lazy-load the Api object. |
||
| 289 | * |
||
| 290 | * @return WordPress_GitHub_Sync_Api |
||
| 291 | */ |
||
| 292 | public function api() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Lazy-load the Import object. |
||
| 302 | * |
||
| 303 | * @return WordPress_GitHub_Sync_Import |
||
| 304 | */ |
||
| 305 | public function import() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Lazy-load the Export object. |
||
| 315 | * |
||
| 316 | * @return WordPress_GitHub_Sync_Export |
||
| 317 | */ |
||
| 318 | public function export() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Lazy-load the Semaphore object. |
||
| 328 | * |
||
| 329 | * @return WordPress_GitHub_Sync_Semaphore |
||
| 330 | */ |
||
| 331 | public function semaphore() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Lazy-load the Database object. |
||
| 341 | * |
||
| 342 | * @return WordPress_GitHub_Sync_Database |
||
| 343 | */ |
||
| 344 | public function database() { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Lazy-load the Cache object. |
||
| 354 | * |
||
| 355 | * @return WordPress_GitHub_Sync_Cache |
||
| 356 | */ |
||
| 357 | public function cache() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | 2 | * Print to WP_CLI if in CLI environment or |
|
| 367 | 2 | * write to debug.log if WP_DEBUG is enabled |
|
| 368 | * |
||
| 369 | * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
||
| 370 | 2 | * |
|
| 371 | * @param mixed $msg Message text. |
||
| 372 | 2 | * @param string $write How to write the message, if CLI. |
|
| 373 | 2 | */ |
|
| 374 | public static function write_log( $msg, $write = 'line' ) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Kicks of an import or export cronjob. |
||
| 392 | * |
||
| 393 | * @param string $type Cron to kick off. |
||
| 394 | */ |
||
| 395 | protected function start_cron( $type ) { |
||
| 400 | } |
||
| 401 |