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 VartypeCompare 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 VartypeCompare, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class VartypeCompare extends Vartype { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The tests to run. |
||
| 25 | * |
||
| 26 | * @var array $tests Multi-dimensional array. |
||
| 27 | * Possible lower level array keys: |
||
| 28 | * - title Used as tab title |
||
| 29 | * - tooltip Additional code sample for tooltip on tab |
||
| 30 | * - url Relevant PHP Manual page |
||
| 31 | * - arg Function arguments |
||
| 32 | * - function Function to run |
||
| 33 | * - Notes Array of notes on this test |
||
| 34 | */ |
||
| 35 | var $tests = array( |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Operator based comparisons. |
||
| 39 | */ |
||
| 40 | 'equal' => array( |
||
| 41 | 'title' => '==', |
||
| 42 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 43 | 'arg' => '$a, $b', |
||
| 44 | 'function' => 'pr_bool( $a == $b );', |
||
| 45 | ), |
||
| 46 | 'equal_strict' => array( |
||
| 47 | 'title' => '===', |
||
| 48 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 49 | 'arg' => '$a, $b', |
||
| 50 | 'function' => 'pr_bool( $a === $b );', |
||
| 51 | ), |
||
| 52 | 'not_equal' => array( |
||
| 53 | 'title' => '!=', |
||
| 54 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 55 | 'arg' => '$a, $b', |
||
| 56 | 'function' => 'pr_bool( $a != $b );', |
||
| 57 | ), |
||
| 58 | 'not_equal2' => array( |
||
| 59 | 'title' => '<>', |
||
| 60 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 61 | 'arg' => '$a, $b', |
||
| 62 | 'function' => 'pr_bool( $a <> $b );', |
||
| 63 | ), |
||
| 64 | 'not_equal_strict' => array( |
||
| 65 | 'title' => '!==', |
||
| 66 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 67 | 'arg' => '$a, $b', |
||
| 68 | 'function' => 'pr_bool( $a !== $b );', |
||
| 69 | ), |
||
| 70 | 'less_than' => array( |
||
| 71 | 'title' => '<', |
||
| 72 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 73 | 'arg' => '$a, $b', |
||
| 74 | 'function' => 'pr_bool( $a < $b );', |
||
| 75 | ), |
||
| 76 | 'greater_than' => array( |
||
| 77 | 'title' => '>', |
||
| 78 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 79 | 'arg' => '$a, $b', |
||
| 80 | 'function' => 'pr_bool( $a > $b );', |
||
| 81 | ), |
||
| 82 | 'less_than_or_equal' => array( |
||
| 83 | 'title' => '<=', |
||
| 84 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 85 | 'arg' => '$a, $b', |
||
| 86 | 'function' => 'pr_bool( $a <= $b );', |
||
| 87 | ), |
||
| 88 | 'greater_than_or_equal' => array( |
||
| 89 | 'title' => '>=', |
||
| 90 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 91 | 'arg' => '$a, $b', |
||
| 92 | 'function' => 'pr_bool( $a >= $b );', |
||
| 93 | ), |
||
| 94 | |||
| 95 | // Will be removed from $tests property from constructor if not on PHP 7+ to prevent parse errors. |
||
| 96 | 'spaceship' => array( |
||
| 97 | 'title' => '<=>', |
||
| 98 | 'url' => 'https://php.net/language.operators.comparison', |
||
| 99 | 'arg' => '$a, $b', |
||
| 100 | 'function' => 'pr_int( $a <=> $b );', |
||
| 101 | 'notes' => array( |
||
| 102 | '<p>The Spaceship operator is only available in PHP 7.0.0+.</p>', |
||
| 103 | ), |
||
| 104 | ), |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * String comparison functions. |
||
| 109 | * |
||
| 110 | * Note: all of these functions have a PHP5 equivalent in class.vartype-php5.php. |
||
| 111 | */ |
||
| 112 | 'strcmp' => array( |
||
| 113 | 'title' => 'strcmp()', |
||
| 114 | 'url' => 'https://php.net/strcmp', |
||
| 115 | 'arg' => '$a, $b', |
||
| 116 | 'function' => 'pc_compare_strings( $a, $b, "strcmp" );', |
||
| 117 | ), |
||
| 118 | 'strcasecmp' => array( |
||
| 119 | 'title' => 'strcasecmp()', |
||
| 120 | 'url' => 'https://php.net/strcasecmp', |
||
| 121 | 'arg' => '$a, $b', |
||
| 122 | 'function' => 'pc_compare_strings( $a, $b, "strcasecmp" );', |
||
| 123 | ), |
||
| 124 | 'strnatcmp' => array( |
||
| 125 | 'title' => 'strnatcmp()', |
||
| 126 | 'url' => 'https://php.net/strnatcmp', |
||
| 127 | 'arg' => '$a, $b', |
||
| 128 | 'function' => 'pc_compare_strings( $a, $b, "strnatcmp" );', |
||
| 129 | ), |
||
| 130 | 'strnatcasecmp' => array( |
||
| 131 | 'title' => 'strnatcasecmp()', |
||
| 132 | 'url' => 'https://php.net/strnatcasecmp', |
||
| 133 | 'arg' => '$a, $b', |
||
| 134 | 'function' => 'pc_compare_strings( $a, $b, "strnatcasecmp" );', |
||
| 135 | ), |
||
| 136 | 'strcoll' => array( |
||
| 137 | 'title' => 'strcoll()', |
||
| 138 | 'url' => 'https://php.net/strcoll', |
||
| 139 | 'arg' => '$a, $b', |
||
| 140 | 'function' => 'pc_compare_strings( $a, $b, "strcoll" );', |
||
| 141 | ), |
||
| 142 | 'similar_text' => array( |
||
| 143 | 'title' => 'similar_text()', |
||
| 144 | 'url' => 'https://php.net/similar_text', |
||
| 145 | 'arg' => '$a, $b', |
||
| 146 | 'function' => 'pc_compare_strings( $a, $b, "similar_text" );', |
||
| 147 | ), |
||
| 148 | 'levenshtein' => array( |
||
| 149 | 'title' => 'levenshtein()', |
||
| 150 | 'url' => 'https://php.net/levenshtein', |
||
| 151 | 'arg' => '$a, $b', |
||
| 152 | 'function' => 'pc_compare_strings( $a, $b, "levenshtein" );', |
||
| 153 | ), |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Number comparison functions |
||
| 158 | */ |
||
| 159 | 'bccomp' => array( |
||
| 160 | 'title' => 'bccomp()', |
||
| 161 | 'url' => 'https://php.net/bccomp', |
||
| 162 | 'arg' => '$a, $b', |
||
| 163 | 'function' => 'if ( extension_loaded( \'bcmath\' ) ) { $r = bccomp( $a, $b ); if ( is_int( $r ) ) { pr_int( $r ); } else { pr_var( $r, \'\', true, true ); } } else { print \'E: bcmath extension not installed\'; }', |
||
| 164 | 'notes' => array( |
||
| 165 | '<p>Remember that the default <code>bcscale()</code> is 0 !</p>', |
||
| 166 | '<p>For a reliable implementation of all the BCMath functions which avoids a number of the common pitfalls, see <a href="https://gist.github.com/jrfnl/8449978" target="_blank">this example function</a> (gist).</p>', |
||
| 167 | ), |
||
| 168 | ), |
||
| 169 | 'min' => array( |
||
| 170 | 'title' => 'min()', |
||
| 171 | 'url' => 'https://php.net/min', |
||
| 172 | 'arg' => '$a, $b', |
||
| 173 | 'function' => 'pr_var( min( $a, $b ), \'\', true, true );', |
||
| 174 | 'notes' => array( |
||
| 175 | '<p><strong>Please note:</strong> <code>min() / max()</code> will evaluate a non-numeric string as 0 if compared to integer, but still return the string if it\'s seen as the numerically lowest/highest value.</p>', |
||
| 176 | '<p><code>min()</code> If multiple arguments evaluate to 0, will return the lowest alphanumerical string value if any strings are given, else a numeric 0 is returned.</p>', |
||
| 177 | ), |
||
| 178 | ), |
||
| 179 | |||
| 180 | 'max' => array( |
||
| 181 | 'title' => 'max()', |
||
| 182 | 'url' => 'https://php.net/max', |
||
| 183 | 'arg' => '$a, $b', |
||
| 184 | 'function' => 'pr_var( max( $a, $b ), \'\', true, true );', |
||
| 185 | 'notes' => array( |
||
| 186 | '<p><strong>Please note:</strong> <code>min() / max()</code> will evaluate a non-numeric string as 0 if compared to integer, but still return the string if it\'s seen as the numerically lowest/highest value.</p>', |
||
| 187 | '<p><code>max()</code> returns the numerically highest of the parameter values. If multiple values can be considered of the same size, the one that is listed first will be returned.<br /> |
||
| 188 | If <code>max()</code> is given multiple arrays, the longest array is returned. If all the arrays have the same length, <code>max()</code> will use lexicographic ordering to find the return value.</p>', |
||
| 189 | ), |
||
| 190 | ), |
||
| 191 | |||
| 192 | /* |
||
| 193 | * Version compare. |
||
| 194 | * Expects string input. |
||
| 195 | */ |
||
| 196 | 'version_compare' => array( |
||
| 197 | 'title' => 'version_compare()', |
||
| 198 | 'url' => 'https://php.net/version_compare', |
||
| 199 | 'arg' => '$a, $b', |
||
| 200 | 'function' => 'pr_var( version_compare( $a, $b ), \'\', true, true );', |
||
| 201 | ), |
||
| 202 | ); |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Constructor. |
||
| 207 | */ |
||
| 208 | function __construct() { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * PHP4 Constructor. |
||
| 219 | */ |
||
| 220 | function VartypeCompare() { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the tab title for the initial tab for use in the page header. |
||
| 227 | * |
||
| 228 | * @param string $tab |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | View Code Duplication | function get_tab_title( $tab ) { |
|
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Get a list of all tabs which this class will create. |
||
| 243 | * |
||
| 244 | * Helper function for the sitemap. |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | function get_tab_list() { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Determine which tests to run. |
||
| 255 | * |
||
| 256 | * @param string|null $test_group |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | function get_test_group( $test_group = null ) { |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Generate the subsection tabs (at the top of the page) for the cheatsheet. |
||
| 271 | * |
||
| 272 | * @param bool $all |
||
| 273 | */ |
||
| 274 | function print_tabs( $all = false ) { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Print all tables for the cheatsheet. |
||
| 307 | */ |
||
| 308 | function print_tables() { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Generate the table for one specific subsection of a comparison cheatsheet. |
||
| 329 | * |
||
| 330 | * @param string $test The current subsection. |
||
| 331 | */ |
||
| 332 | function print_table( $test ) { |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * Generate the first row of the cheatsheet table. |
||
| 409 | * |
||
| 410 | * @param string $test The current subsection. |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | function create_table_header( $test ) { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Get the - potentially linked - group label (= first cell in the table header). |
||
| 466 | * |
||
| 467 | * @param string $test |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | function get_table_header_group_label( $test ) { |
||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * Get the notes related to the group label, if any. |
||
| 490 | * |
||
| 491 | * @param string $test |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | function get_table_header_group_notes( $test ) { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the CSS class string to attach to a table header cell. |
||
| 508 | * |
||
| 509 | * @param string $index |
||
| 510 | * @param string $key |
||
| 511 | * |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | function get_table_header_cell_class( $index, $key ) { |
||
| 522 | |||
| 523 | |||
| 524 | /** |
||
| 525 | * Adjust the cell label to make it usable as a title in a jQuery tooltip. |
||
| 526 | * |
||
| 527 | * @todo: improve upon - preferably in a way that the tooltip is fully HTML capable. |
||
| 528 | * |
||
| 529 | * @param string $label Original label. |
||
| 530 | * @param bool $object Whether this is an object or an array. Defaults to false (= array ). |
||
| 531 | * |
||
| 532 | * @return string Adjusted label |
||
| 533 | */ |
||
| 534 | function get_table_header_cell_title( $label, $object = false ) { |
||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * Generate a cheatsheet result row. |
||
| 552 | * |
||
| 553 | * @param mixed $value1 The value this row applies to. |
||
| 554 | * @param string $key1 The array key reference to that value in the testdata array. |
||
| 555 | * @param string $test The current subsection. |
||
| 556 | */ |
||
| 557 | function print_compare_row_cells( $value1, $key1, $test ) { |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the CSS class string to attach to a table cell. |
||
| 581 | * |
||
| 582 | * @param string $key1 |
||
| 583 | * @param string $key2 |
||
| 584 | * @param string $index |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | function get_table_cell_class( $key1, $key2, $index ) { |
||
| 598 | |||
| 599 | |||
| 600 | /** |
||
| 601 | * Generate footnotes for a test subsection if applicable. |
||
| 602 | * |
||
| 603 | * @param string $test The current subsection. |
||
| 604 | */ |
||
| 605 | function print_other_footnotes( $test ) { |
||
| 620 | } |
||
| 621 |