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 Vartype 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 Vartype, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Vartype { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Placeholder for test data. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | var $test_data = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Placeholder for test data labels. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | var $test_legend = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Placeholder for test data key array. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | var $test_data_keys = array(); |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * At which point in the tables to repeat headers. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | var $header_repeat = array( 's', 'a', 'o', 'p' ); |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Tests to be run, added in child class. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | var $tests = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Placeholder for grouping of the tests. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | var $test_groups = array(); |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Placeholder for test notes. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | var $table_notes = array(); |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Helper array to determine which class(es) should be used in table header cells. |
||
| 75 | * |
||
| 76 | * @var array Key is where to look in the test_group, value is the classname to add. |
||
| 77 | */ |
||
| 78 | var $table_header_class_map = array( |
||
| 79 | 'best' => 'best', |
||
| 80 | 'good' => 'good', |
||
| 81 | 'break_at' => 'end', |
||
| 82 | ); |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Constructor. |
||
| 87 | */ |
||
| 88 | function __construct() { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * PHP4 compatibility constructor. |
||
| 118 | */ |
||
| 119 | function vartype() { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Overwrite selected entries in the original test array with PHP-version specific function code. |
||
| 126 | * |
||
| 127 | * @param array $overload_tests Array of test entries to use in the overloading. |
||
| 128 | */ |
||
| 129 | function merge_tests( $overload_tests ) { |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the tab title for the initial tab for use in the page header. |
||
| 141 | * |
||
| 142 | * @param string $tab |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | View Code Duplication | function get_tab_title( $tab ) { |
|
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Get a list of all tabs which this class will create. |
||
| 158 | * |
||
| 159 | * Helper function for the sitemap. |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | function get_tab_list() { |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Generate a cheatsheet page. |
||
| 170 | * |
||
| 171 | * @param bool $all |
||
| 172 | */ |
||
| 173 | function do_page( $all = false ) { |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Determine which tests to run. |
||
| 188 | * |
||
| 189 | * @param string|null $test_group The current subsection. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | function get_test_group( $test_group = null ) { |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Run all the tests for one specific testgroup. |
||
| 204 | * |
||
| 205 | * @param string|null $test_group The current subsection. |
||
| 206 | */ |
||
| 207 | function run_test( $test_group = null ) { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Prepare the test data (the variables) for use in the tests. |
||
| 219 | * |
||
| 220 | * @param string|null $test_group The current subsection. |
||
| 221 | */ |
||
| 222 | function set_test_data( $test_group = null ) { |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Sort the test data via a set order - callback method. |
||
| 249 | * |
||
| 250 | * @param mixed $var_a |
||
| 251 | * @param mixed $var_b |
||
| 252 | * |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | function sort_test_data( $var_a, $var_b ) { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * Housekeeping so the variables can be re-initiated properly. |
||
| 297 | */ |
||
| 298 | function clean_up() { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * Generate the subsection tabs (at the top of the page) for the cheatsheet. |
||
| 317 | * |
||
| 318 | * @param bool $all |
||
| 319 | */ |
||
| 320 | function print_tabs( $all = false ) { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Print all tables for the cheatsheet. |
||
| 348 | */ |
||
| 349 | function print_tables() { |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Generate the table for one specific subsection of a cheatsheet. |
||
| 367 | * |
||
| 368 | * @param string $test_group The current subsection. |
||
| 369 | */ |
||
| 370 | function print_table( $test_group ) { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Generate the first row of the cheatsheet table. |
||
| 454 | * |
||
| 455 | * @param string $test_group The current subsection. |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | function create_table_header( $test_group ) { |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Get the - potentially linked - group label (= first cell in the table header). |
||
| 495 | * |
||
| 496 | * @param string $test_group The current subsection. |
||
| 497 | * |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | function get_table_header_group_label( $test_group ) { |
||
| 510 | |||
| 511 | |||
| 512 | /** |
||
| 513 | * Get the notes related to the group label, if any. |
||
| 514 | * |
||
| 515 | * @param string $test |
||
| 516 | * @param string $test_group |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | function get_table_header_note_indicators( $test, $test_group ) { |
||
| 537 | |||
| 538 | |||
| 539 | /** |
||
| 540 | * Get the CSS class string to attach to a table header cell. |
||
| 541 | * |
||
| 542 | * @param string $test_group |
||
| 543 | * @param string $test |
||
| 544 | * |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | function get_table_header_cell_class( $test_group, $test ) { |
||
| 559 | |||
| 560 | |||
| 561 | /** |
||
| 562 | * Generate the html for the table top. |
||
| 563 | * |
||
| 564 | * @param string $test_group The current subsection. |
||
| 565 | */ |
||
| 566 | function print_tabletop( $test_group ) { |
||
| 578 | |||
| 579 | |||
| 580 | /** |
||
| 581 | * Generate a cheatsheet result row. |
||
| 582 | * |
||
| 583 | * @param mixed $value The value this row applies to. |
||
| 584 | * @param string $test_group The current subsection. |
||
| 585 | */ |
||
| 586 | function print_row_cells( $value, $test_group ) { |
||
| 606 | |||
| 607 | |||
| 608 | /** |
||
| 609 | * Get the CSS class string to attach to a table cell. |
||
| 610 | * |
||
| 611 | * @param string $test_group |
||
| 612 | * @param string $test |
||
| 613 | * |
||
| 614 | * @return string |
||
| 615 | */ |
||
| 616 | function get_table_row_cell_class( $test_group, $test ) { |
||
| 632 | |||
| 633 | |||
| 634 | /** |
||
| 635 | * Print the error reference numbers. |
||
| 636 | * |
||
| 637 | * Used in table cells to link to errors which will be displayed as footnotes. |
||
| 638 | */ |
||
| 639 | function print_row_cell_error_refs() { |
||
| 648 | |||
| 649 | |||
| 650 | /** |
||
| 651 | * Get the value to use for the tests. |
||
| 652 | * |
||
| 653 | * If in PHP5 and the value is an object, it will clone the object so we'll have a 'clean' object |
||
| 654 | * for each test (not a reference). |
||
| 655 | * |
||
| 656 | * @param mixed $value |
||
| 657 | * |
||
| 658 | * @return mixed |
||
| 659 | */ |
||
| 660 | function generate_value( $value ) { |
||
| 666 | |||
| 667 | |||
| 668 | /** |
||
| 669 | * Generate footnotes for any errors encountered. |
||
| 670 | * |
||
| 671 | * @param string $test_group The current subsection. |
||
| 672 | */ |
||
| 673 | function print_error_footnotes( $test_group ) { |
||
| 687 | |||
| 688 | |||
| 689 | /** |
||
| 690 | * Generate footnotes for a test subsection if applicable. |
||
| 691 | * |
||
| 692 | * @param string $test_group The current subsection. |
||
| 693 | */ |
||
| 694 | function print_other_footnotes( $test_group ) { |
||
| 710 | } |
||
| 711 |