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 | * Current version |
||
62 | * @var string |
||
63 | */ |
||
64 | public static $version = '1.7.5'; |
||
65 | |||
66 | /** |
||
67 | * Controller object |
||
68 | * @var Writing_On_GitHub_Controller |
||
69 | */ |
||
70 | public $controller; |
||
71 | |||
72 | /** |
||
73 | * Controller object |
||
74 | * @var Writing_On_GitHub_Admin |
||
75 | */ |
||
76 | public $admin; |
||
77 | |||
78 | /** |
||
79 | * CLI object. |
||
80 | * |
||
81 | * @var Writing_On_GitHub_CLI |
||
82 | */ |
||
83 | protected $cli; |
||
84 | |||
85 | /** |
||
86 | * Request object. |
||
87 | * |
||
88 | * @var Writing_On_GitHub_Request |
||
89 | */ |
||
90 | protected $request; |
||
91 | |||
92 | /** |
||
93 | * Response object. |
||
94 | * |
||
95 | * @var Writing_On_GitHub_Response |
||
96 | */ |
||
97 | protected $response; |
||
98 | |||
99 | /** |
||
100 | * Api object. |
||
101 | * |
||
102 | * @var Writing_On_GitHub_Api |
||
103 | */ |
||
104 | protected $api; |
||
105 | |||
106 | /** |
||
107 | * Import object. |
||
108 | * |
||
109 | * @var Writing_On_GitHub_Import |
||
110 | */ |
||
111 | protected $import; |
||
112 | |||
113 | /** |
||
114 | * Export object. |
||
115 | * |
||
116 | * @var Writing_On_GitHub_Export |
||
117 | */ |
||
118 | protected $export; |
||
119 | |||
120 | /** |
||
121 | * Semaphore object. |
||
122 | * |
||
123 | * @var Writing_On_GitHub_Semaphore |
||
124 | */ |
||
125 | protected $semaphore; |
||
126 | |||
127 | /** |
||
128 | * Database object. |
||
129 | * |
||
130 | * @var Writing_On_GitHub_Database |
||
131 | */ |
||
132 | protected $database; |
||
133 | |||
134 | /** |
||
135 | * Cache object. |
||
136 | * |
||
137 | * @var Writing_On_GitHub_Cache |
||
138 | */ |
||
139 | protected $cache; |
||
140 | |||
141 | /** |
||
142 | * Called at load time, hooks into WP core |
||
143 | */ |
||
144 | public function __construct() { |
||
157 | |||
158 | /** |
||
159 | * Attaches the plugin's hooks into WordPress. |
||
160 | */ |
||
161 | public function boot() { |
||
180 | |||
181 | public function edit_post_link($link, $postID, $context) { |
||
191 | |||
192 | public function ignore_post_meta($meta) { |
||
214 | |||
215 | /** |
||
216 | * Init i18n files |
||
217 | */ |
||
218 | public function l10n() { |
||
221 | |||
222 | /** |
||
223 | * Sets and kicks off the export cronjob |
||
224 | */ |
||
225 | public function start_export() { |
||
228 | |||
229 | /** |
||
230 | * Sets and kicks off the import cronjob |
||
231 | */ |
||
232 | public function start_import() { |
||
235 | |||
236 | /** |
||
237 | * Enables the admin notice on initial activation |
||
238 | */ |
||
239 | public function activate() { |
||
244 | |||
245 | /** |
||
246 | * Displays the activation admin notice |
||
247 | */ |
||
248 | public function activation_notice() { |
||
266 | |||
267 | /** |
||
268 | * Get the Controller object. |
||
269 | * |
||
270 | * @return Writing_On_GitHub_Controller |
||
271 | */ |
||
272 | public function controller() { |
||
275 | |||
276 | /** |
||
277 | * Lazy-load the CLI object. |
||
278 | * |
||
279 | * @return Writing_On_GitHub_CLI |
||
280 | */ |
||
281 | public function cli() { |
||
288 | |||
289 | /** |
||
290 | * Lazy-load the Request object. |
||
291 | * |
||
292 | * @return Writing_On_GitHub_Request |
||
293 | */ |
||
294 | public function request() { |
||
301 | |||
302 | /** |
||
303 | * Lazy-load the Response object. |
||
304 | * |
||
305 | * @return Writing_On_GitHub_Response |
||
306 | */ |
||
307 | public function response() { |
||
314 | |||
315 | /** |
||
316 | * Lazy-load the Api object. |
||
317 | * |
||
318 | * @return Writing_On_GitHub_Api |
||
319 | */ |
||
320 | public function api() { |
||
327 | |||
328 | /** |
||
329 | * Lazy-load the Import object. |
||
330 | * |
||
331 | * @return Writing_On_GitHub_Import |
||
332 | */ |
||
333 | public function import() { |
||
340 | |||
341 | /** |
||
342 | * Lazy-load the Export object. |
||
343 | * |
||
344 | * @return Writing_On_GitHub_Export |
||
345 | */ |
||
346 | public function export() { |
||
353 | |||
354 | /** |
||
355 | * Lazy-load the Semaphore object. |
||
356 | * |
||
357 | * @return Writing_On_GitHub_Semaphore |
||
358 | */ |
||
359 | public function semaphore() { |
||
366 | |||
367 | /** |
||
368 | * Lazy-load the Database object. |
||
369 | * |
||
370 | * @return Writing_On_GitHub_Database |
||
371 | */ |
||
372 | public function database() { |
||
379 | |||
380 | /** |
||
381 | * Lazy-load the Cache object. |
||
382 | * |
||
383 | * @return Writing_On_GitHub_Cache |
||
384 | */ |
||
385 | public function cache() { |
||
392 | |||
393 | /** |
||
394 | * Print to WP_CLI if in CLI environment or |
||
395 | * write to debug.log if WP_DEBUG is enabled |
||
396 | * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
||
397 | * |
||
398 | * @param mixed $msg |
||
399 | * @param string $write |
||
400 | */ |
||
401 | public static function write_log( $msg, $write = 'line' ) { |
||
416 | |||
417 | /** |
||
418 | * Kicks of an import or export cronjob. |
||
419 | * |
||
420 | * @param $type |
||
421 | */ |
||
422 | protected function start_cron( $type ) { |
||
428 | } |
||
429 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.