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 $line |
||
| 382 | * @param int $heading_tag |
||
| 383 | * @param bool $die |
||
| 384 | * @param string $margin |
||
| 385 | */ |
||
| 386 | public static function printv( |
||
| 411 | |||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $var_name |
||
| 416 | * @param string $heading_tag |
||
| 417 | * @param string $margin |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | protected static function heading($var_name = '', $heading_tag = 'h5', $margin = '') |
||
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $heading_tag |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | protected static function headingX($heading_tag = 'h5') |
||
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $content |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | protected static function grey_span($content = '') |
||
| 456 | |||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $file |
||
| 461 | * @param int $line |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | protected static function file_and_line($file, $line) |
||
| 475 | |||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $content |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | protected static function orange_span($content = '') |
||
| 489 | |||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * @param mixed $var |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | protected static function pre_span($var) |
||
| 506 | |||
| 507 | |||
| 508 | |||
| 509 | /** |
||
| 510 | * @param mixed $var |
||
| 511 | * @param string $var_name |
||
| 512 | * @param string $file |
||
| 513 | * @param int $line |
||
| 514 | * @param int $heading_tag |
||
| 515 | * @param bool $die |
||
| 516 | */ |
||
| 517 | public static function printr( |
||
| 557 | |||
| 558 | |||
| 559 | |||
| 560 | /******************** deprecated ********************/ |
||
| 561 | /** |
||
| 562 | * @deprecated 4.9.39.rc.034 |
||
| 563 | */ |
||
| 564 | public function reset_times() |
||
| 568 | |||
| 569 | |||
| 570 | |||
| 571 | /** |
||
| 572 | * @deprecated 4.9.39.rc.034 |
||
| 573 | * @param null $timer_name |
||
| 574 | */ |
||
| 575 | public function start_timer($timer_name = null) |
||
| 579 | |||
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * @deprecated 4.9.39.rc.034 |
||
| 584 | * @param string $timer_name |
||
| 585 | */ |
||
| 586 | public function stop_timer($timer_name = '') |
||
| 590 | |||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * @deprecated 4.9.39.rc.034 |
||
| 595 | * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
||
| 596 | * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
||
| 597 | * @return void |
||
| 598 | */ |
||
| 599 | public function measure_memory($label, $output_now = false) |
||
| 603 | |||
| 604 | |||
| 605 | |||
| 606 | /** |
||
| 607 | * @deprecated 4.9.39.rc.034 |
||
| 608 | * @param int $size |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | public function convert($size) |
||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | /** |
||
| 619 | * @deprecated 4.9.39.rc.034 |
||
| 620 | * @param bool $output_now |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | public function show_times($output_now = true) |
||
| 627 | |||
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * @deprecated 4.9.39.rc.034 |
||
| 632 | * @param string $timer_name |
||
| 633 | * @param float $total_time |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function format_time($timer_name, $total_time) |
||
| 640 | |||
| 641 | |||
| 642 | |||
| 643 | } |
||
| 644 | |||
| 680 |
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.