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 EEH_Debug_Tools 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 EEH_Debug_Tools, and based on these observations, apply Extract Interface, too.
| 1 | <?php use EventEspresso\core\services\Benchmark; |
||
| 17 | class EEH_Debug_Tools |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * instance of the EEH_Autoloader object |
||
| 22 | * |
||
| 23 | * @var $_instance |
||
| 24 | * @access private |
||
| 25 | */ |
||
| 26 | private static $_instance; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $_memory_usage_points = array(); |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * @singleton method used to instantiate class object |
||
| 37 | * @access public |
||
| 38 | * @return EEH_Debug_Tools |
||
| 39 | */ |
||
| 40 | public static function instance() |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * private class constructor |
||
| 53 | */ |
||
| 54 | private function __construct() |
||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * show_db_name |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public static function show_db_name() |
||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * dump EE_Session object at bottom of page after everything else has happened |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function espresso_session_footer_dump() |
||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * List All Hooked Functions |
||
| 122 | * to list all functions for a specific hook, add ee_list_hooks={hook-name} to URL |
||
| 123 | * http://wp.smashingmagazine.com/2009/08/18/10-useful-wordpress-hook-hacks/ |
||
| 124 | * |
||
| 125 | * @param string $tag |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | public function espresso_list_hooked_functions($tag = '') |
||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * registered_filter_callbacks |
||
| 159 | * |
||
| 160 | * @param string $hook_name |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public static function registered_filter_callbacks($hook_name = '') |
||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * captures plugin activation errors for debugging |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | * @throws EE_Error |
||
| 186 | */ |
||
| 187 | public static function ee_plugin_activation_errors() |
||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * This basically mimics the WordPress _doing_it_wrong() function except adds our own messaging etc. |
||
| 232 | * Very useful for providing helpful messages to developers when the method of doing something has been deprecated, |
||
| 233 | * or we want to make sure they use something the right way. |
||
| 234 | * |
||
| 235 | * @access public |
||
| 236 | * @param string $function The function that was called |
||
| 237 | * @param string $message A message explaining what has been done incorrectly |
||
| 238 | * @param string $version The version of Event Espresso where the error was added |
||
| 239 | * @param string $applies_when a version string for when you want the doing_it_wrong notice to begin appearing |
||
| 240 | * for a deprecated function. This allows deprecation to occur during one version, |
||
| 241 | * but not have any notices appear until a later version. This allows developers |
||
| 242 | * extra time to update their code before notices appear. |
||
| 243 | * @param int $error_type |
||
| 244 | * @uses trigger_error() |
||
| 245 | */ |
||
| 246 | public function doing_it_wrong( |
||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Logger helpers |
||
| 303 | */ |
||
| 304 | /** |
||
| 305 | * debug |
||
| 306 | * |
||
| 307 | * @param string $class |
||
| 308 | * @param string $func |
||
| 309 | * @param string $line |
||
| 310 | * @param array $info |
||
| 311 | * @param bool $display_request |
||
| 312 | * @param string $debug_index |
||
| 313 | * @param string $debug_key |
||
| 314 | * @throws EE_Error |
||
| 315 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
||
| 316 | */ |
||
| 317 | public static function log( |
||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * strip_objects |
||
| 348 | * |
||
| 349 | * @param array $info |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public static function strip_objects($info = array()) |
||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @param mixed $var |
||
| 379 | * @param string $var_name |
||
| 380 | * @param string $file |
||
| 381 | * @param int|string $line |
||
| 382 | * @param int $heading_tag |
||
| 383 | * @param bool $die |
||
| 384 | * @param string $margin |
||
| 385 | */ |
||
| 386 | public static function printv( |
||
| 413 | |||
| 414 | |||
| 415 | protected static function plainOutput() |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $var_name |
||
| 423 | * @param string $heading_tag |
||
| 424 | * @param string $margin |
||
| 425 | * @param int $line |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | protected static function heading($var_name = '', $heading_tag = 'h5', $margin = '', $line = 0) |
||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $heading_tag |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | protected static function headingX($heading_tag = 'h5') |
||
| 450 | |||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * @param string $content |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | protected static function grey_span($content = '') |
||
| 464 | |||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $file |
||
| 469 | * @param int $line |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | protected static function file_and_line($file, $line) |
||
| 483 | |||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $content |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | protected static function orange_span($content = '') |
||
| 497 | |||
| 498 | |||
| 499 | |||
| 500 | /** |
||
| 501 | * @param mixed $var |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | protected static function pre_span($var) |
||
| 514 | |||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * @param mixed $var |
||
| 519 | * @param string $var_name |
||
| 520 | * @param string $file |
||
| 521 | * @param int|string $line |
||
| 522 | * @param int $heading_tag |
||
| 523 | * @param bool $die |
||
| 524 | */ |
||
| 525 | public static function printr( |
||
| 565 | |||
| 566 | |||
| 567 | |||
| 568 | /******************** deprecated ********************/ |
||
| 569 | |||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * @deprecated 4.9.39.rc.034 |
||
| 574 | */ |
||
| 575 | public function reset_times() |
||
| 579 | |||
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * @deprecated 4.9.39.rc.034 |
||
| 584 | * @param null $timer_name |
||
| 585 | */ |
||
| 586 | public function start_timer($timer_name = null) |
||
| 590 | |||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * @deprecated 4.9.39.rc.034 |
||
| 595 | * @param string $timer_name |
||
| 596 | */ |
||
| 597 | public function stop_timer($timer_name = '') |
||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * @deprecated 4.9.39.rc.034 |
||
| 606 | * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
||
| 607 | * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
||
| 608 | * @return void |
||
| 609 | */ |
||
| 610 | public function measure_memory($label, $output_now = false) |
||
| 614 | |||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * @deprecated 4.9.39.rc.034 |
||
| 619 | * @param int $size |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | public function convert($size) |
||
| 626 | |||
| 627 | |||
| 628 | |||
| 629 | /** |
||
| 630 | * @deprecated 4.9.39.rc.034 |
||
| 631 | * @param bool $output_now |
||
| 632 | * @return string |
||
| 633 | */ |
||
| 634 | public function show_times($output_now = true) |
||
| 638 | |||
| 639 | |||
| 640 | |||
| 641 | /** |
||
| 642 | * @deprecated 4.9.39.rc.034 |
||
| 643 | * @param string $timer_name |
||
| 644 | * @param float $total_time |
||
| 645 | * @return string |
||
| 646 | */ |
||
| 647 | public function format_time($timer_name, $total_time) |
||
| 651 | |||
| 652 | |||
| 653 | |||
| 654 | } |
||
| 655 | |||
| 691 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.