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 ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get Default Table configurations. |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | private function get_default_tables() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Prepare field params based off provided configuration. |
||
| 210 | * |
||
| 211 | * @param array $table_configuration The table configuration array. |
||
| 212 | */ |
||
| 213 | private function prepare_fields( $table_configuration ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Verify provided table name is valid for checksum processing. |
||
| 224 | * |
||
| 225 | * @param string $table Table name to validate. |
||
| 226 | * |
||
| 227 | * @return mixed|string |
||
| 228 | * @throws Exception Throw an exception on validation failure. |
||
| 229 | */ |
||
| 230 | private function validate_table_name( $table ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Verify provided fields are proper names. |
||
| 246 | * |
||
| 247 | * @param array $fields Array of field names to validate. |
||
| 248 | * |
||
| 249 | * @throws Exception Throw an exception on failure to validate. |
||
| 250 | */ |
||
| 251 | private function validate_fields( $fields ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Verify the fields exist in the table. |
||
| 263 | * |
||
| 264 | * @param array $fields Array of fields to validate. |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | * @throws Exception Throw an exception on failure to validate. |
||
| 268 | */ |
||
| 269 | private function validate_fields_against_table( $fields ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Verify the configured fields. |
||
| 290 | * |
||
| 291 | * @throws Exception Throw an exception on failure to validate in the internal functions. |
||
| 292 | */ |
||
| 293 | private function validate_input() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Prepare filter values as SQL statements to be added to the other filters. |
||
| 302 | * |
||
| 303 | * @param array $filter_values The filter values array. |
||
| 304 | * @param string $table_prefix If the values are going to be used in a sub-query, add a prefix with the table alias. |
||
| 305 | * |
||
| 306 | * @return array|null |
||
| 307 | */ |
||
| 308 | private function prepare_filter_values_as_sql( $filter_values = array(), $table_prefix = '' ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Build the filter query baased off range fields and values and the additional sql. |
||
| 340 | * |
||
| 341 | * @param int|null $range_from Start of the range. |
||
| 342 | * @param int|null $range_to End of the range. |
||
| 343 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 344 | * @param string $table_prefix Table name to be prefixed to the columns. Used in sub-queries where columns can clash. |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function build_filter_statement( $range_from = null, $range_to = null, $filter_values = null, $table_prefix = '' ) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns the checksum query. All validation of fields and configurations are expected to occur prior to usage. |
||
| 408 | * |
||
| 409 | * @param int|null $range_from The start of the range. |
||
| 410 | * @param int|null $range_to The end of the range. |
||
| 411 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 412 | * @param bool $granular_result If the function should return a granular result. |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | * |
||
| 416 | * @throws Exception Throws and exception if validation fails in the internal function calls. |
||
| 417 | */ |
||
| 418 | private function build_checksum_query( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Obtain the min-max values (edges) of the range. |
||
| 488 | * |
||
| 489 | * @param int|null $range_from The start of the range. |
||
| 490 | * @param int|null $range_to The end of the range. |
||
| 491 | * @param int|null $limit How many values to return. |
||
| 492 | * |
||
| 493 | * @return array|object|void |
||
| 494 | * @throws Exception Throws an exception if validation fails on the internal function calls. |
||
| 495 | */ |
||
| 496 | public function get_range_edges( $range_from = null, $range_to = null, $limit = null ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Update the results to have key/checksum format. |
||
| 564 | * |
||
| 565 | * @param array $results Prepare the results for output of granular results. |
||
| 566 | */ |
||
| 567 | protected function prepare_results_for_output( &$results ) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Calculate the checksum based on provided range and filters. |
||
| 589 | * |
||
| 590 | * @param int|null $range_from The start of the range. |
||
| 591 | * @param int|null $range_to The end of the range. |
||
| 592 | * @param array|null $filter_values Additional filter values. Not used at the moment. |
||
| 593 | * @param bool $granular_result If the returned result should be granular or only the checksum. |
||
| 594 | * @param bool $simple_return_value If we want to use a simple return value for non-granular results (return only the checksum, without wrappers). |
||
| 595 | * |
||
| 596 | * @return array|mixed|object|WP_Error|null |
||
| 597 | */ |
||
| 598 | public function calculate_checksum( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false, $simple_return_value = true ) { |
||
| 636 | } |
||
| 637 |
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.