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 |
||
46 | class Writing_On_GitHub { |
||
47 | |||
48 | /** |
||
49 | * Object instance |
||
50 | * @var self |
||
51 | */ |
||
52 | public static $instance; |
||
53 | |||
54 | /** |
||
55 | * Language text domain |
||
56 | * @var string |
||
57 | */ |
||
58 | public static $text_domain = 'writing-on-github'; |
||
59 | |||
60 | /** |
||
61 | * Controller object |
||
62 | * @var Writing_On_GitHub_Controller |
||
63 | */ |
||
64 | public $controller; |
||
65 | |||
66 | /** |
||
67 | * Controller object |
||
68 | * @var Writing_On_GitHub_Admin |
||
69 | */ |
||
70 | public $admin; |
||
71 | |||
72 | /** |
||
73 | * CLI object. |
||
74 | * |
||
75 | * @var Writing_On_GitHub_CLI |
||
76 | */ |
||
77 | protected $cli; |
||
78 | |||
79 | /** |
||
80 | * Request object. |
||
81 | * |
||
82 | * @var Writing_On_GitHub_Request |
||
83 | */ |
||
84 | protected $request; |
||
85 | |||
86 | /** |
||
87 | * Response object. |
||
88 | * |
||
89 | * @var Writing_On_GitHub_Response |
||
90 | */ |
||
91 | protected $response; |
||
92 | |||
93 | /** |
||
94 | * Api object. |
||
95 | * |
||
96 | * @var Writing_On_GitHub_Api |
||
97 | */ |
||
98 | protected $api; |
||
99 | |||
100 | /** |
||
101 | * Import object. |
||
102 | * |
||
103 | * @var Writing_On_GitHub_Import |
||
104 | */ |
||
105 | protected $import; |
||
106 | |||
107 | /** |
||
108 | * Export object. |
||
109 | * |
||
110 | * @var Writing_On_GitHub_Export |
||
111 | */ |
||
112 | protected $export; |
||
113 | |||
114 | /** |
||
115 | * Semaphore object. |
||
116 | * |
||
117 | * @var Writing_On_GitHub_Semaphore |
||
118 | */ |
||
119 | protected $semaphore; |
||
120 | |||
121 | /** |
||
122 | * Database object. |
||
123 | * |
||
124 | * @var Writing_On_GitHub_Database |
||
125 | */ |
||
126 | protected $database; |
||
127 | |||
128 | /** |
||
129 | * Cache object. |
||
130 | * |
||
131 | * @var Writing_On_GitHub_Cache |
||
132 | */ |
||
133 | protected $cache; |
||
134 | |||
135 | /** |
||
136 | * Called at load time, hooks into WP core |
||
137 | */ |
||
138 | public function __construct() { |
||
151 | |||
152 | /** |
||
153 | * Attaches the plugin's hooks into WordPress. |
||
154 | */ |
||
155 | public function boot() { |
||
175 | |||
176 | public function edit_post_link($link, $postID, $context) { |
||
186 | |||
187 | public function ignore_post_meta($meta) { |
||
209 | |||
210 | public function the_content($content) { |
||
233 | |||
234 | /** |
||
235 | * Init i18n files |
||
236 | */ |
||
237 | public function l10n() { |
||
240 | |||
241 | /** |
||
242 | * Sets and kicks off the export cronjob |
||
243 | */ |
||
244 | public function start_export( $force = false ) { |
||
247 | |||
248 | /** |
||
249 | * Sets and kicks off the import cronjob |
||
250 | */ |
||
251 | public function start_import() { |
||
254 | |||
255 | /** |
||
256 | * Enables the admin notice on initial activation |
||
257 | */ |
||
258 | public function activate() { |
||
263 | |||
264 | /** |
||
265 | * Displays the activation admin notice |
||
266 | */ |
||
267 | public function activation_notice() { |
||
285 | |||
286 | /** |
||
287 | * Get the Controller object. |
||
288 | * |
||
289 | * @return Writing_On_GitHub_Controller |
||
290 | */ |
||
291 | public function controller() { |
||
294 | |||
295 | /** |
||
296 | * Lazy-load the CLI object. |
||
297 | * |
||
298 | * @return Writing_On_GitHub_CLI |
||
299 | */ |
||
300 | public function cli() { |
||
307 | |||
308 | /** |
||
309 | * Lazy-load the Request object. |
||
310 | * |
||
311 | * @return Writing_On_GitHub_Request |
||
312 | */ |
||
313 | public function request() { |
||
320 | |||
321 | /** |
||
322 | * Lazy-load the Response object. |
||
323 | * |
||
324 | * @return Writing_On_GitHub_Response |
||
325 | */ |
||
326 | public function response() { |
||
333 | |||
334 | /** |
||
335 | * Lazy-load the Api object. |
||
336 | * |
||
337 | * @return Writing_On_GitHub_Api |
||
338 | */ |
||
339 | public function api() { |
||
346 | |||
347 | /** |
||
348 | * Lazy-load the Import object. |
||
349 | * |
||
350 | * @return Writing_On_GitHub_Import |
||
351 | */ |
||
352 | public function import() { |
||
359 | |||
360 | /** |
||
361 | * Lazy-load the Export object. |
||
362 | * |
||
363 | * @return Writing_On_GitHub_Export |
||
364 | */ |
||
365 | public function export() { |
||
372 | |||
373 | /** |
||
374 | * Lazy-load the Semaphore object. |
||
375 | * |
||
376 | * @return Writing_On_GitHub_Semaphore |
||
377 | */ |
||
378 | public function semaphore() { |
||
385 | |||
386 | /** |
||
387 | * Lazy-load the Database object. |
||
388 | * |
||
389 | * @return Writing_On_GitHub_Database |
||
390 | */ |
||
391 | public function database() { |
||
398 | |||
399 | /** |
||
400 | * Lazy-load the Cache object. |
||
401 | * |
||
402 | * @return Writing_On_GitHub_Cache |
||
403 | */ |
||
404 | public function cache() { |
||
411 | |||
412 | /** |
||
413 | * Print to WP_CLI if in CLI environment or |
||
414 | * write to debug.log if WP_DEBUG is enabled |
||
415 | * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
||
416 | * |
||
417 | * @param mixed $msg |
||
418 | * @param string $write |
||
419 | */ |
||
420 | public static function write_log( $msg, $write = 'line' ) { |
||
435 | |||
436 | /** |
||
437 | * Kicks of an import or export cronjob. |
||
438 | * |
||
439 | * @param bool $force |
||
440 | * @param string $type |
||
441 | */ |
||
442 | protected function start_cron( $type, $force = false ) { |
||
448 | } |
||
449 |