Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Writing_On_GitHub 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 Writing_On_GitHub, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Writing_On_GitHub { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Object instance |
||
| 49 | * @var self |
||
| 50 | */ |
||
| 51 | public static $instance; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Language text domain |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public static $text_domain = 'writing-on-github'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Controller object |
||
| 61 | * @var Writing_On_GitHub_Controller |
||
| 62 | */ |
||
| 63 | public $controller; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Controller object |
||
| 67 | * @var Writing_On_GitHub_Admin |
||
| 68 | */ |
||
| 69 | public $admin; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * CLI object. |
||
| 73 | * |
||
| 74 | * @var Writing_On_GitHub_CLI |
||
| 75 | */ |
||
| 76 | protected $cli; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Request object. |
||
| 80 | * |
||
| 81 | * @var Writing_On_GitHub_Request |
||
| 82 | */ |
||
| 83 | protected $request; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Response object. |
||
| 87 | * |
||
| 88 | * @var Writing_On_GitHub_Response |
||
| 89 | */ |
||
| 90 | protected $response; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Api object. |
||
| 94 | * |
||
| 95 | * @var Writing_On_GitHub_Api |
||
| 96 | */ |
||
| 97 | protected $api; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Import object. |
||
| 101 | * |
||
| 102 | * @var Writing_On_GitHub_Import |
||
| 103 | */ |
||
| 104 | protected $import; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Export object. |
||
| 108 | * |
||
| 109 | * @var Writing_On_GitHub_Export |
||
| 110 | */ |
||
| 111 | protected $export; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Semaphore object. |
||
| 115 | * |
||
| 116 | * @var Writing_On_GitHub_Semaphore |
||
| 117 | */ |
||
| 118 | protected $semaphore; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Database object. |
||
| 122 | * |
||
| 123 | * @var Writing_On_GitHub_Database |
||
| 124 | */ |
||
| 125 | protected $database; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Called at load time, hooks into WP core |
||
| 129 | */ |
||
| 130 | public function __construct() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Attaches the plugin's hooks into WordPress. |
||
| 146 | */ |
||
| 147 | public function boot() { |
||
| 167 | |||
| 168 | public function edit_post_link($link, $postID, $context) { |
||
| 178 | |||
| 179 | public function ignore_post_meta($meta) { |
||
| 201 | |||
| 202 | public function the_content($content) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Init i18n files |
||
| 228 | */ |
||
| 229 | public function l10n() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Sets and kicks off the export cronjob |
||
| 235 | */ |
||
| 236 | public function start_export( $force = false ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Sets and kicks off the import cronjob |
||
| 242 | */ |
||
| 243 | public function start_import() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Enables the admin notice on initial activation |
||
| 249 | */ |
||
| 250 | public function activate() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Displays the activation admin notice |
||
| 258 | */ |
||
| 259 | public function activation_notice() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the Controller object. |
||
| 280 | * |
||
| 281 | * @return Writing_On_GitHub_Controller |
||
| 282 | */ |
||
| 283 | public function controller() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Lazy-load the CLI object. |
||
| 289 | * |
||
| 290 | * @return Writing_On_GitHub_CLI |
||
| 291 | */ |
||
| 292 | public function cli() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Lazy-load the Request object. |
||
| 302 | * |
||
| 303 | * @return Writing_On_GitHub_Request |
||
| 304 | */ |
||
| 305 | public function request() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Lazy-load the Response object. |
||
| 315 | * |
||
| 316 | * @return Writing_On_GitHub_Response |
||
| 317 | */ |
||
| 318 | public function response() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Lazy-load the Api object. |
||
| 328 | * |
||
| 329 | * @return Writing_On_GitHub_Api |
||
| 330 | */ |
||
| 331 | public function api() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Lazy-load the Import object. |
||
| 341 | * |
||
| 342 | * @return Writing_On_GitHub_Import |
||
| 343 | */ |
||
| 344 | public function import() { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Lazy-load the Export object. |
||
| 354 | * |
||
| 355 | * @return Writing_On_GitHub_Export |
||
| 356 | */ |
||
| 357 | public function export() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Lazy-load the Semaphore object. |
||
| 367 | * |
||
| 368 | * @return Writing_On_GitHub_Semaphore |
||
| 369 | */ |
||
| 370 | public function semaphore() { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Lazy-load the Database object. |
||
| 380 | * |
||
| 381 | * @return Writing_On_GitHub_Database |
||
| 382 | */ |
||
| 383 | public function database() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Print to WP_CLI if in CLI environment or |
||
| 393 | * write to debug.log if WP_DEBUG is enabled |
||
| 394 | * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
||
| 395 | * |
||
| 396 | * @param mixed $msg |
||
| 397 | * @param string $write |
||
| 398 | */ |
||
| 399 | public static function write_log( $msg, $write = 'line' ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Kicks of an import or export cronjob. |
||
| 417 | * |
||
| 418 | * @param bool $force |
||
| 419 | * @param string $type |
||
| 420 | */ |
||
| 421 | protected function start_cron( $type, $force = false ) { |
||
| 427 | } |
||
| 428 |