Complex classes like ZT_Debug_Bar_Cron 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 ZT_Debug_Bar_Cron, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ZT_Debug_Bar_Cron extends Debug_Bar_Panel { |
||
28 | |||
29 | const DBCRON_STYLES_VERSION = '1.0'; |
||
30 | |||
31 | const DBCRON_NAME = 'debug-bar-cron'; |
||
32 | |||
33 | /** |
||
34 | * Holds all of the cron events. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $_crons; |
||
39 | |||
40 | /** |
||
41 | * Holds only the cron events initiated by WP core. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $_core_crons; |
||
46 | |||
47 | /** |
||
48 | * Holds the cron events created by plugins or themes. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $_user_crons; |
||
53 | |||
54 | /** |
||
55 | * Total number of cron events. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | private $_total_crons = 0; |
||
60 | |||
61 | /** |
||
62 | * Total number of cron events created by plugins and themes. |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | private $_total_user_crons = 0; |
||
67 | |||
68 | /** |
||
69 | * Total number of WP core cron events. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | private $_total_core_crons = 0; |
||
74 | |||
75 | /** |
||
76 | * Whether cron is being executed or not. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $_doing_cron = 'No'; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Give the panel a title and set the enqueues. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function init() { |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Enqueue styles. |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | public function enqueue_scripts_styles() { |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Show the menu item in Debug Bar. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function prerender() { |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Show the contents of the page. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function render() { |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Gets all of the cron jobs. |
||
184 | * |
||
185 | * This function sorts the cron jobs into core crons, and custom crons. It also tallies |
||
186 | * a total count for the crons as this number is otherwise tough to get. |
||
187 | * |
||
188 | * @return array|null Array of crons. |
||
189 | */ |
||
190 | private function get_crons() { |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Displays the events in an easy to read table. |
||
240 | * |
||
241 | * @param array $events Array of events. |
||
242 | * @param string $no_events_msg Message to display if there are no events. |
||
243 | */ |
||
244 | private function display_events( $events, $no_events_msg ) { |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Displays the cron arguments in a readable format. |
||
329 | * |
||
330 | * @param mixed $args Cron argument(s). |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | private function display_cron_arguments( $args ) { |
||
344 | |||
345 | |||
346 | /** |
||
347 | * Displays all of the schedules defined. |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | private function display_schedules() { |
||
384 | |||
385 | /** |
||
386 | * Sorting method for cron scheldules. Order by schedules interval. |
||
387 | * |
||
388 | * @param array $a First element of comparison pair. |
||
389 | * @param array $b Second element of comparison pair. |
||
390 | * |
||
391 | * @return int Return 1 if $a argument 'interval' greater then $b argument 'interval', 0 if both intervals equivalent and -1 otherwise. |
||
392 | */ |
||
393 | function schedules_sorting( $a, $b ) { |
||
400 | |||
401 | /** |
||
402 | * Verify if a given timestamp is in the past or the future. |
||
403 | * |
||
404 | * @param int $time Unix timestamp. |
||
405 | * |
||
406 | * @return bool True if the time has passed, false otherwise. |
||
407 | */ |
||
408 | private function is_time_in_past( $time ) { |
||
411 | |||
412 | |||
413 | /** |
||
414 | * Transform a time in seconds to minutes rounded to 2 decimals. |
||
415 | * |
||
416 | * @param int $time Unix timestamp. |
||
417 | * |
||
418 | * @return int|float |
||
419 | */ |
||
420 | private function get_minutes( $time ) { |
||
423 | |||
424 | |||
425 | /** |
||
426 | * Transform a time in seconds to hours rounded to 2 decimals. |
||
427 | * |
||
428 | * @param int $time Unix timestamp. |
||
429 | * |
||
430 | * @return int|float |
||
431 | */ |
||
432 | private function get_hours( $time ) { |
||
435 | |||
436 | |||
437 | /** |
||
438 | * Compares time with current time and adds ' ago' if current time is greater than event time. |
||
439 | * |
||
440 | * @param string $human_time Human readable time difference. |
||
441 | * @param int $time Unix time of event. |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | private function display_past_time( $human_time, $time ) { |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Load the pretty output class & set the recursion limit. |
||
457 | */ |
||
458 | private function load_debug_bar_pretty_output() { |
||
468 | |||
469 | |||
470 | /** |
||
471 | * Print any type of variable with colour coding and a variable type indication. |
||
472 | * |
||
473 | * @param mixed $variable The variable to print. |
||
474 | */ |
||
475 | private function print_pretty_output( $variable ) { |
||
484 | |||
485 | |||
486 | /** |
||
487 | * Unset recursion depth limit for the pretty output class. |
||
488 | * |
||
489 | * @internal Method available since DBPO v1.4. |
||
490 | */ |
||
491 | private function reset_debug_bar_pretty_output() { |
||
496 | } // End of class ZT_Debug_Bar_Cron. |
||
497 | |||
499 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.