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.
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 |
||
22 | class Writing_On_GitHub { |
||
|
|||
23 | |||
24 | /** |
||
25 | * Object instance |
||
26 | * @var self |
||
27 | */ |
||
28 | public static $instance; |
||
29 | |||
30 | /** |
||
31 | * Language text domain |
||
32 | * @var string |
||
33 | */ |
||
34 | public static $text_domain = 'writing-on-github'; |
||
35 | |||
36 | /** |
||
37 | * Controller object |
||
38 | * @var Writing_On_GitHub_Controller |
||
39 | */ |
||
40 | public $controller; |
||
41 | |||
42 | /** |
||
43 | * Controller object |
||
44 | * @var Writing_On_GitHub_Admin |
||
45 | */ |
||
46 | public $admin; |
||
47 | |||
48 | /** |
||
49 | * CLI object. |
||
50 | * |
||
51 | * @var Writing_On_GitHub_CLI |
||
52 | */ |
||
53 | protected $cli; |
||
54 | |||
55 | /** |
||
56 | * Request object. |
||
57 | * |
||
58 | * @var Writing_On_GitHub_Request |
||
59 | */ |
||
60 | protected $request; |
||
61 | |||
62 | /** |
||
63 | * Response object. |
||
64 | * |
||
65 | * @var Writing_On_GitHub_Response |
||
66 | */ |
||
67 | protected $response; |
||
68 | |||
69 | /** |
||
70 | * Api object. |
||
71 | * |
||
72 | * @var Writing_On_GitHub_Api |
||
73 | */ |
||
74 | protected $api; |
||
75 | |||
76 | /** |
||
77 | * Import object. |
||
78 | * |
||
79 | * @var Writing_On_GitHub_Import |
||
80 | */ |
||
81 | protected $import; |
||
82 | |||
83 | /** |
||
84 | * Export object. |
||
85 | * |
||
86 | * @var Writing_On_GitHub_Export |
||
87 | */ |
||
88 | protected $export; |
||
89 | |||
90 | /** |
||
91 | * Semaphore object. |
||
92 | * |
||
93 | * @var Writing_On_GitHub_Semaphore |
||
94 | */ |
||
95 | protected $semaphore; |
||
96 | |||
97 | /** |
||
98 | * Database object. |
||
99 | * |
||
100 | * @var Writing_On_GitHub_Database |
||
101 | */ |
||
102 | protected $database; |
||
103 | |||
104 | /** |
||
105 | * Called at load time, hooks into WP core |
||
106 | */ |
||
107 | public function __construct() { |
||
120 | |||
121 | /** |
||
122 | * Attaches the plugin's hooks into WordPress. |
||
123 | */ |
||
124 | public function boot() { |
||
144 | |||
145 | public function edit_post_link($link, $postID, $context) { |
||
155 | |||
156 | public function ignore_post_meta($meta) { |
||
178 | |||
179 | public function the_content($content) { |
||
202 | |||
203 | /** |
||
204 | * Init i18n files |
||
205 | */ |
||
206 | public function l10n() { |
||
209 | |||
210 | /** |
||
211 | * Sets and kicks off the export cronjob |
||
212 | */ |
||
213 | public function start_export( $force = false ) { |
||
216 | |||
217 | /** |
||
218 | * Sets and kicks off the import cronjob |
||
219 | */ |
||
220 | public function start_import() { |
||
223 | |||
224 | /** |
||
225 | * Enables the admin notice on initial activation |
||
226 | */ |
||
227 | public function activate() { |
||
232 | |||
233 | /** |
||
234 | * Displays the activation admin notice |
||
235 | */ |
||
236 | public function activation_notice() { |
||
254 | |||
255 | /** |
||
256 | * Get the Controller object. |
||
257 | * |
||
258 | * @return Writing_On_GitHub_Controller |
||
259 | */ |
||
260 | public function controller() { |
||
263 | |||
264 | /** |
||
265 | * Lazy-load the CLI object. |
||
266 | * |
||
267 | * @return Writing_On_GitHub_CLI |
||
268 | */ |
||
269 | public function cli() { |
||
276 | |||
277 | /** |
||
278 | * Lazy-load the Request object. |
||
279 | * |
||
280 | * @return Writing_On_GitHub_Request |
||
281 | */ |
||
282 | public function request() { |
||
289 | |||
290 | /** |
||
291 | * Lazy-load the Response object. |
||
292 | * |
||
293 | * @return Writing_On_GitHub_Response |
||
294 | */ |
||
295 | public function response() { |
||
302 | |||
303 | /** |
||
304 | * Lazy-load the Api object. |
||
305 | * |
||
306 | * @return Writing_On_GitHub_Api |
||
307 | */ |
||
308 | public function api() { |
||
315 | |||
316 | /** |
||
317 | * Lazy-load the Import object. |
||
318 | * |
||
319 | * @return Writing_On_GitHub_Import |
||
320 | */ |
||
321 | public function import() { |
||
328 | |||
329 | /** |
||
330 | * Lazy-load the Export object. |
||
331 | * |
||
332 | * @return Writing_On_GitHub_Export |
||
333 | */ |
||
334 | public function export() { |
||
341 | |||
342 | /** |
||
343 | * Lazy-load the Semaphore object. |
||
344 | * |
||
345 | * @return Writing_On_GitHub_Semaphore |
||
346 | */ |
||
347 | public function semaphore() { |
||
354 | |||
355 | /** |
||
356 | * Lazy-load the Database object. |
||
357 | * |
||
358 | * @return Writing_On_GitHub_Database |
||
359 | */ |
||
360 | public function database() { |
||
367 | |||
368 | /** |
||
369 | * Print to WP_CLI if in CLI environment or |
||
370 | * write to debug.log if WP_DEBUG is enabled |
||
371 | * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
||
372 | * |
||
373 | * @param mixed $msg |
||
374 | * @param string $write |
||
375 | */ |
||
376 | public static function write_log( $msg, $write = 'line' ) { |
||
391 | |||
392 | /** |
||
393 | * Kicks of an import or export cronjob. |
||
394 | * |
||
395 | * @param bool $force |
||
396 | * @param string $type |
||
397 | */ |
||
398 | protected function start_cron( $type, $force = false ) { |
||
404 | } |
||
405 |