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() { |
||
382 | |||
383 | |||
384 | /** |
||
385 | * Verify if a given timestamp is in the past or the future. |
||
386 | * |
||
387 | * @param int $time Unix timestamp. |
||
388 | * |
||
389 | * @return bool True if the time has passed, false otherwise. |
||
390 | */ |
||
391 | private function is_time_in_past( $time ) { |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Transform a time in seconds to minutes rounded to 2 decimals. |
||
398 | * |
||
399 | * @param int $time Unix timestamp. |
||
400 | * |
||
401 | * @return int|float |
||
402 | */ |
||
403 | private function get_minutes( $time ) { |
||
406 | |||
407 | |||
408 | /** |
||
409 | * Transform a time in seconds to hours rounded to 2 decimals. |
||
410 | * |
||
411 | * @param int $time Unix timestamp. |
||
412 | * |
||
413 | * @return int|float |
||
414 | */ |
||
415 | private function get_hours( $time ) { |
||
418 | |||
419 | |||
420 | // @codingStandardsIgnoreStart Code taken from elsewhere, ignoring style to allow for easier updating. |
||
421 | /** |
||
422 | * Do simple reliable floating point calculations without the risk of wrong results. |
||
423 | * |
||
424 | * @see http://floating-point-gui.de/ |
||
425 | * @see the big red warning on http://php.net/language.types.float.php |
||
426 | * |
||
427 | * In the rare case that the bcmath extension would not be loaded, it will return the |
||
428 | * normal calculation results. |
||
429 | * |
||
430 | * @see Source: https://gist.github.com/jrfnl/8449978 |
||
431 | * |
||
432 | * @param mixed $number1 Scalar (string/int/float/bool). |
||
433 | * @param string $action Calculation action to execute. Valid input: |
||
434 | * '+' or 'add' or 'addition', |
||
435 | * '-' or 'sub' or 'subtract', |
||
436 | * '*' or 'mul' or 'multiply', |
||
437 | * '/' or 'div' or 'divide', |
||
438 | * '%' or 'mod' or 'modulus', |
||
439 | * '=' or 'comp' or 'compare'. |
||
440 | * @param mixed $number2 Scalar (string/int/float/bool). |
||
441 | * @param bool $round Whether or not to round the result. Defaults to false. |
||
442 | * Will be disregarded for a compare operation. |
||
443 | * @param int $decimals Decimals for rounding operation. Defaults to 0. |
||
444 | * @param int $precision Calculation precision. Defaults to 10. |
||
445 | * |
||
446 | * @return mixed Calculation Result or false if either or the numbers isn't scalar or |
||
447 | * an invalid operation was passed. |
||
448 | * - for compare the result will always be an integer. |
||
449 | * - for all other operations, the result will either be an integer (preferred) |
||
450 | * or a float. |
||
451 | */ |
||
452 | private function bc_calc( $number1, $action, $number2, $round = false, $decimals = 0, $precision = 10 ) { |
||
453 | if ( ! is_scalar( $number1 ) || ! is_scalar( $number2 ) ) { |
||
454 | return false; |
||
455 | } |
||
456 | |||
457 | $bc = extension_loaded( 'bcmath' ); |
||
458 | |||
459 | if ( $bc ) { |
||
460 | $number1 = number_format( $number1, 10, '.', '' ); |
||
461 | $number2 = number_format( $number2, 10, '.', '' ); |
||
462 | } |
||
463 | |||
464 | $result = null; |
||
465 | $compare = false; |
||
466 | |||
467 | switch ( $action ) { |
||
468 | case '+': |
||
469 | case 'add': |
||
470 | case 'addition': |
||
471 | $result = ( $bc ) ? bcadd( $number1, $number2, $precision ) /* string */ : ( $number1 + $number2 ); |
||
472 | break; |
||
473 | |||
474 | case '-': |
||
475 | case 'sub': |
||
476 | case 'subtract': |
||
477 | $result = ( $bc ) ? bcsub( $number1, $number2, $precision ) /* string */ : ( $number1 - $number2 ); |
||
478 | break; |
||
479 | |||
480 | case '*': |
||
481 | case 'mul': |
||
482 | case 'multiply': |
||
483 | $result = ( $bc ) ? bcmul( $number1, $number2, $precision ) /* string */ : ( $number1 * $number2 ); |
||
484 | break; |
||
485 | |||
486 | case '/': |
||
487 | case 'div': |
||
488 | case 'divide': |
||
|
|||
489 | if ( $bc ) { |
||
490 | $result = bcdiv( $number1, $number2, $precision ); // String, or NULL if right_operand is 0. |
||
491 | } |
||
492 | else if ( $number2 != 0 ) { |
||
493 | $result = $number1 / $number2; |
||
494 | } |
||
495 | |||
496 | if ( ! isset( $result ) ) { |
||
497 | $result = 0; |
||
498 | } |
||
499 | break; |
||
500 | |||
501 | case '%': |
||
502 | case 'mod': |
||
503 | case 'modulus': |
||
504 | if ( $bc ) { |
||
505 | $result = bcmod( $number1, $number2, $precision ); // String, or NULL if modulus is 0. |
||
506 | } |
||
507 | else if ( $number2 != 0 ) { |
||
508 | $result = $number1 % $number2; |
||
509 | } |
||
510 | |||
511 | if ( ! isset( $result ) ) { |
||
512 | $result = 0; |
||
513 | } |
||
514 | break; |
||
515 | |||
516 | case '=': |
||
517 | case 'comp': |
||
518 | case 'compare': |
||
519 | $compare = true; |
||
520 | if ( $bc ) { |
||
521 | $result = bccomp( $number1, $number2, $precision ); // Returns int 0, 1 or -1. |
||
522 | } |
||
523 | else { |
||
524 | $result = ( $number1 == $number2 ) ? 0 : ( ( $number1 > $number2 ) ? 1 : -1 ); |
||
525 | } |
||
526 | break; |
||
527 | } |
||
528 | |||
529 | if ( isset( $result ) ) { |
||
530 | if ( $compare === false ) { |
||
531 | if ( $round === true ) { |
||
532 | $result = round( floatval( $result ), $decimals ); |
||
533 | if ( $decimals === 0 ) { |
||
534 | $result = (int) $result; |
||
535 | } |
||
536 | } |
||
537 | else { |
||
538 | $result = ( intval( $result ) == $result ) ? intval( $result ) : floatval( $result ); |
||
539 | } |
||
540 | } |
||
541 | return $result; |
||
542 | } |
||
543 | return false; |
||
544 | } |
||
545 | // @codingStandardsIgnoreEnd |
||
546 | |||
547 | |||
548 | /** |
||
549 | * Compares time with current time and adds ' ago' if current time is greater than event time. |
||
550 | * |
||
551 | * @param string $human_time Human readable time difference. |
||
552 | * @param int $time Unix time of event. |
||
553 | * |
||
554 | * @return string |
||
555 | */ |
||
556 | private function display_past_time( $human_time, $time ) { |
||
564 | |||
565 | |||
566 | /** |
||
567 | * Load the pretty output class & set the recursion limit. |
||
568 | */ |
||
569 | private function load_debug_bar_pretty_output() { |
||
579 | |||
580 | |||
581 | /** |
||
582 | * Print any type of variable with colour coding and a variable type indication. |
||
583 | * |
||
584 | * @param mixed $variable The variable to print. |
||
585 | */ |
||
586 | private function print_pretty_output( $variable ) { |
||
595 | |||
596 | |||
597 | /** |
||
598 | * Unset recursion depth limit for the pretty output class. |
||
599 | * |
||
600 | * @internal Method available since DBPO v1.4. |
||
601 | */ |
||
602 | private function reset_debug_bar_pretty_output() { |
||
607 | } // End of class ZT_Debug_Bar_Cron. |
||
608 | |||
610 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.