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 Table_Checksum 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 Table_Checksum, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Table_Checksum { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Table to be checksummed. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $table = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Table Checksum Configuration. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | public $table_configuration = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Field to be used for range queries. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $range_field = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * ID Field(s) to be used. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $key_fields = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Field(s) to be used in generating the checksum value. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public $checksum_fields = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Default filter values for the table |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public $filter_values = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * SQL Query to be used to filter results (allow/disallow). |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | public $additional_filter_sql = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Default Checksum Table Configurations. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | public $default_tables = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Salt to be used when generating checksum. |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | public $salt = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Tables which are allowed to be checksummed. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | public $allowed_tables = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * If the table has a "parent" table that it's related to. |
||
| 92 | * |
||
| 93 | * @var mixed|null |
||
| 94 | */ |
||
| 95 | private $parent_table = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Table_Checksum constructor. |
||
| 99 | * |
||
| 100 | * @param string $table The table to calculate checksums for. |
||
| 101 | * @param string $salt Optional salt to add to the checksum. |
||
|
|
|||
| 102 | * |
||
| 103 | * @throws Exception Throws exception from inner functions. |
||
| 104 | */ |
||
| 105 | public function __construct( $table, $salt = null ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get Default Table configurations. |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | private function get_default_tables() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Prepare field params based off provided configuration. |
||
| 196 | * |
||
| 197 | * @param array $table_configuration The table configuration array. |
||
| 198 | */ |
||
| 199 | private function prepare_fields( $table_configuration ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Verify provided table name is valid for checksum processing. |
||
| 210 | * |
||
| 211 | * @param string $table Table name to validate. |
||
| 212 | * |
||
| 213 | * @return mixed|string |
||
| 214 | * @throws Exception Throw an exception on validation failure. |
||
| 215 | */ |
||
| 216 | private function validate_table_name( $table ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Verify provided fields are proper names. |
||
| 232 | * |
||
| 233 | * @param array $fields Array of field names to validate. |
||
| 234 | * |
||
| 235 | * @throws Exception Throw an exception on failure to validate. |
||
| 236 | */ |
||
| 237 | private function validate_fields( $fields ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Verify the fields exist in the table. |
||
| 249 | * |
||
| 250 | * @param array $fields Array of fields to validate. |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | * @throws Exception Throw an exception on failure to validate. |
||
| 254 | */ |
||
| 255 | private function validate_fields_against_table( $fields ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Verify the configured fields. |
||
| 276 | * |
||
| 277 | * @throws Exception Throw an exception on failure to validate in the internal functions. |
||
| 278 | */ |
||
| 279 | private function validate_input() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Prepare filter values as SQL statements to be added to the other filters. |
||
| 288 | * |
||
| 289 | * @param array $filter_values The filter values array. |
||
| 290 | * @param string $table_prefix If the values are going to be used in a sub-query, add a prefix with the table alias. |
||
| 291 | * |
||
| 292 | * @return array|null |
||
| 293 | */ |
||
| 294 | private function prepare_filter_values_as_sql( $filter_values = array(), $table_prefix = '' ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Build the filter query baased off range fields and values and the additional sql. |
||
| 325 | * |
||
| 326 | * @param int|null $range_from Start of the range. |
||
| 327 | * @param int|null $range_to End of the range. |
||
| 328 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 329 | * @param string $table_prefix Table name to be prefixed to the columns. Used in sub-queries where columns can clash. |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function build_filter_statement( $range_from = null, $range_to = null, $filter_values = null, $table_prefix = '' ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the checksum query. All validation of fields and configurations are expected to occur prior to usage. |
||
| 391 | * |
||
| 392 | * @param int|null $range_from The start of the range. |
||
| 393 | * @param int|null $range_to The end of the range. |
||
| 394 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 395 | * @param bool $granular_result If the function should return a granular result. |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | * |
||
| 399 | * @throws Exception Throws and exception if validation fails in the internal function calls. |
||
| 400 | */ |
||
| 401 | private function build_checksum_query( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Obtain the min-max values (edges) of the range. |
||
| 464 | * |
||
| 465 | * @param int|null $range_from The start of the range. |
||
| 466 | * @param int|null $range_to The end of the range. |
||
| 467 | * @param int|null $limit How many values to return. |
||
| 468 | * |
||
| 469 | * @return array|object|void |
||
| 470 | * @throws Exception Throws an exception if validation fails on the internal function calls. |
||
| 471 | */ |
||
| 472 | public function get_range_edges( $range_from = null, $range_to = null, $limit = null ) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Update the results to have key/checksum format. |
||
| 539 | * |
||
| 540 | * @param array $results Prepare the results for output of granular results. |
||
| 541 | */ |
||
| 542 | public function prepare_results_for_output( &$results ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Calculate the checksum based on provided range and filters. |
||
| 564 | * |
||
| 565 | * @param int|null $range_from The start of the range. |
||
| 566 | * @param int|null $range_to The end of the range. |
||
| 567 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 568 | * @param bool $granular_result If the returned result should be granular or only the checksum. |
||
| 569 | * @param bool $simple_return_value If we want to use a simple return value for non-granular results (return only the checksum, without wrappers). |
||
| 570 | * |
||
| 571 | * @return array|mixed|object|WP_Error|null |
||
| 572 | */ |
||
| 573 | public function calculate_checksum( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false, $simple_return_value = true ) { |
||
| 606 | } |
||
| 607 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.