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 |
||
| 19 | class Table_Checksum { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Table to be checksummed. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public $table = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Table Checksum Configuration. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $table_configuration = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Field to be used for range queries. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $range_field = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * ID Field(s) to be used. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | public $key_fields = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Field(s) to be used in generating the checksum value. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public $checksum_fields = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Default filter values for the table |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | public $filter_values = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * SQL Query to be used to filter results (allow/disallow). |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | public $additional_filter_sql = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Default Checksum Table Configurations. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public $default_tables = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Salt to be used when generating checksum. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $salt = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Tables which are allowed to be checksummed. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | public $allowed_tables = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * If the table has a "parent" table that it's related to. |
||
| 93 | * |
||
| 94 | * @var mixed|null |
||
| 95 | */ |
||
| 96 | private $parent_table = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Table_Checksum constructor. |
||
| 100 | * |
||
| 101 | * @param string $table The table to calculate checksums for. |
||
| 102 | * @param string $salt Optional salt to add to the checksum. |
||
|
|
|||
| 103 | * |
||
| 104 | * @throws Exception Throws exception from inner functions. |
||
| 105 | */ |
||
| 106 | public function __construct( $table, $salt = null ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get Default Table configurations. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | private function get_default_tables() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Prepare field params based off provided configuration. |
||
| 211 | * |
||
| 212 | * @param array $table_configuration The table configuration array. |
||
| 213 | */ |
||
| 214 | private function prepare_fields( $table_configuration ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Verify provided table name is valid for checksum processing. |
||
| 225 | * |
||
| 226 | * @param string $table Table name to validate. |
||
| 227 | * |
||
| 228 | * @return mixed|string |
||
| 229 | * @throws Exception Throw an exception on validation failure. |
||
| 230 | */ |
||
| 231 | private function validate_table_name( $table ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Verify provided fields are proper names. |
||
| 247 | * |
||
| 248 | * @param array $fields Array of field names to validate. |
||
| 249 | * |
||
| 250 | * @throws Exception Throw an exception on failure to validate. |
||
| 251 | */ |
||
| 252 | private function validate_fields( $fields ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Verify the fields exist in the table. |
||
| 264 | * |
||
| 265 | * @param array $fields Array of fields to validate. |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | * @throws Exception Throw an exception on failure to validate. |
||
| 269 | */ |
||
| 270 | private function validate_fields_against_table( $fields ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Verify the configured fields. |
||
| 291 | * |
||
| 292 | * @throws Exception Throw an exception on failure to validate in the internal functions. |
||
| 293 | */ |
||
| 294 | private function validate_input() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Prepare filter values as SQL statements to be added to the other filters. |
||
| 303 | * |
||
| 304 | * @param array $filter_values The filter values array. |
||
| 305 | * @param string $table_prefix If the values are going to be used in a sub-query, add a prefix with the table alias. |
||
| 306 | * |
||
| 307 | * @return array|null |
||
| 308 | */ |
||
| 309 | private function prepare_filter_values_as_sql( $filter_values = array(), $table_prefix = '' ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Build the filter query baased off range fields and values and the additional sql. |
||
| 341 | * |
||
| 342 | * @param int|null $range_from Start of the range. |
||
| 343 | * @param int|null $range_to End of the range. |
||
| 344 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 345 | * @param string $table_prefix Table name to be prefixed to the columns. Used in sub-queries where columns can clash. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function build_filter_statement( $range_from = null, $range_to = null, $filter_values = null, $table_prefix = '' ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns the checksum query. All validation of fields and configurations are expected to occur prior to usage. |
||
| 409 | * |
||
| 410 | * @param int|null $range_from The start of the range. |
||
| 411 | * @param int|null $range_to The end of the range. |
||
| 412 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 413 | * @param bool $granular_result If the function should return a granular result. |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | * |
||
| 417 | * @throws Exception Throws and exception if validation fails in the internal function calls. |
||
| 418 | */ |
||
| 419 | private function build_checksum_query( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Obtain the min-max values (edges) of the range. |
||
| 481 | * |
||
| 482 | * @param int|null $range_from The start of the range. |
||
| 483 | * @param int|null $range_to The end of the range. |
||
| 484 | * @param int|null $limit How many values to return. |
||
| 485 | * |
||
| 486 | * @return array|object|void |
||
| 487 | * @throws Exception Throws an exception if validation fails on the internal function calls. |
||
| 488 | */ |
||
| 489 | public function get_range_edges( $range_from = null, $range_to = null, $limit = null ) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Update the results to have key/checksum format. |
||
| 557 | * |
||
| 558 | * @param array $results Prepare the results for output of granular results. |
||
| 559 | */ |
||
| 560 | protected function prepare_results_for_output( &$results ) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Calculate the checksum based on provided range and filters. |
||
| 582 | * |
||
| 583 | * @param int|null $range_from The start of the range. |
||
| 584 | * @param int|null $range_to The end of the range. |
||
| 585 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 586 | * @param bool $granular_result If the returned result should be granular or only the checksum. |
||
| 587 | * @param bool $simple_return_value If we want to use a simple return value for non-granular results (return only the checksum, without wrappers). |
||
| 588 | * |
||
| 589 | * @return array|mixed|object|WP_Error|null |
||
| 590 | */ |
||
| 591 | public function calculate_checksum( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false, $simple_return_value = true ) { |
||
| 631 | } |
||
| 632 |
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.