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 LogicalFilter 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 LogicalFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class LogicalFilter implements \JsonSerializable |
||
| 30 | { |
||
| 31 | /** @var AndRule $rules */ |
||
| 32 | protected $rules; |
||
| 33 | |||
| 34 | /** @var Filterer $default_filterer */ |
||
| 35 | protected $default_filterer; |
||
| 36 | |||
| 37 | /** @var array $options */ |
||
| 38 | protected $options = []; |
||
| 39 | |||
| 40 | /** @var array $default_options */ |
||
| 41 | protected static $default_options = [ |
||
| 42 | 'in.normalization_threshold' => 0, |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Creates a filter. You can provide a description of rules as in |
||
| 47 | * addRules() as paramater. |
||
| 48 | * |
||
| 49 | * @param array $rules |
||
| 50 | * @param Filterer $default_filterer |
||
|
|
|||
| 51 | * |
||
| 52 | * @see self::addRules |
||
| 53 | */ |
||
| 54 | 280 | public function __construct($rules=[], Filterer $default_filterer=null, array $options=[]) |
|
| 78 | |||
| 79 | /** |
||
| 80 | */ |
||
| 81 | 30 | protected function getDefaultFilterer() |
|
| 89 | |||
| 90 | /** |
||
| 91 | */ |
||
| 92 | 1 | public static function setDefaultOptions(array $options) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | 83 | public static function getDefaultOptions() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 276 | public function getOptions() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * This method parses different ways to define the rules of a LogicalFilter |
||
| 124 | * and add them as a new And part of the filter. |
||
| 125 | * + You can add N already instanciated Rules. |
||
| 126 | * + You can provide 3 arguments: $field, $operator, $value |
||
| 127 | * + You can provide a tree of rules: |
||
| 128 | * ['or', |
||
| 129 | * ['and', |
||
| 130 | * ['field_5', 'above', 'a'], |
||
| 131 | * ['field_5', 'below', 'a'], |
||
| 132 | * ], |
||
| 133 | * ['field_6', 'equal', 'b'], |
||
| 134 | * ] |
||
| 135 | * |
||
| 136 | * @param mixed The descriptions of the rules to add |
||
| 137 | * @return $this |
||
| 138 | * |
||
| 139 | * @todo remove the _ for PHP 7 |
||
| 140 | */ |
||
| 141 | 273 | public function and_() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * This method parses different ways to define the rules of a LogicalFilter |
||
| 154 | * and add them as a new Or part of the filter. |
||
| 155 | * + You can add N already instanciated Rules. |
||
| 156 | * + You can provide 3 arguments: $field, $operator, $value |
||
| 157 | * + You can provide a tree of rules: |
||
| 158 | * ['or', |
||
| 159 | * ['and', |
||
| 160 | * ['field_5', 'above', 'a'], |
||
| 161 | * ['field_5', 'below', 'a'], |
||
| 162 | * ], |
||
| 163 | * ['field_6', 'equal', 'b'], |
||
| 164 | * ] |
||
| 165 | * |
||
| 166 | * @param mixed The descriptions of the rules to add |
||
| 167 | * @return $this |
||
| 168 | * |
||
| 169 | * @todo |
||
| 170 | * @todo remove the _ for PHP 7 |
||
| 171 | */ |
||
| 172 | 24 | public function or_() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @deprecated |
||
| 185 | */ |
||
| 186 | 1 | public function matches($rules_to_match) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Checks that a filter matches another one. |
||
| 193 | * |
||
| 194 | * @param array|AbstractRule $rules_to_match |
||
| 195 | * |
||
| 196 | * @return bool Whether or not this combination of filters has |
||
| 197 | * potential solutions |
||
| 198 | */ |
||
| 199 | 1 | public function hasSolutionIf($rules_to_match) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Retrieve all the rules. |
||
| 210 | * |
||
| 211 | * @param bool $copy By default copy the rule tree to avoid side effects. |
||
| 212 | * |
||
| 213 | * @return AbstractRule The tree of rules |
||
| 214 | */ |
||
| 215 | 133 | public function getRules($copy = true) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Remove any constraint being a duplicate of another one. |
||
| 222 | * |
||
| 223 | * @param array $options stop_after | stop_before | |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | 142 | public function simplify($options=[]) |
|
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Forces the two firsts levels of the tree to be an OrRule having |
||
| 242 | * only AndRules as operands: |
||
| 243 | * ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]] |
||
| 244 | * As a simplified ruleTree will alwways be reduced to this structure |
||
| 245 | * with no suboperands others than atomic ones or a simpler one like: |
||
| 246 | * ['or', ['field', '=', '1'], ['field2', '>', '3']] |
||
| 247 | * |
||
| 248 | * This helpes to ease the result of simplify() |
||
| 249 | * |
||
| 250 | * @return OrRule |
||
| 251 | */ |
||
| 252 | 74 | public function addMinimalCase() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Checks if there is at least on set of conditions which is not |
||
| 263 | * contradictory. |
||
| 264 | * |
||
| 265 | * Checking if a filter has solutions require to simplify it first. |
||
| 266 | * To let the control on the balance between readability and |
||
| 267 | * performances, the required simplification can be saved or not |
||
| 268 | * depending on the $save_simplification parameter. |
||
| 269 | * |
||
| 270 | * @param $save_simplification |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | 82 | public function hasSolution($save_simplification=true) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns an array describing the rule tree of the Filter. |
||
| 290 | * |
||
| 291 | * @param array $options |
||
| 292 | * |
||
| 293 | * @return array A description of the rules. |
||
| 294 | */ |
||
| 295 | 188 | public function toArray(array $options=[]) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Returns an array describing the rule tree of the Filter. |
||
| 302 | * |
||
| 303 | * @param $debug Provides a source oriented dump. |
||
| 304 | * |
||
| 305 | * @return array A description of the rules. |
||
| 306 | */ |
||
| 307 | 4 | public function toString(array $options=[]) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Returns a unique id corresponding to the set of rules of the filter |
||
| 314 | * |
||
| 315 | * @return string The unique semantic id |
||
| 316 | */ |
||
| 317 | 1 | public function getSemanticId() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * For implementing JsonSerializable interface. |
||
| 324 | * |
||
| 325 | * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 326 | */ |
||
| 327 | 1 | public function jsonSerialize() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | 4 | public function __toString() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @see https://secure.php.net/manual/en/language.oop5.magic.php#object.invoke |
||
| 342 | * @param mixed $row |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 3 | public function __invoke($row, $key=null) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Removes all the defined rules. |
||
| 352 | * |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | 2 | public function flushRules() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param array|callable Associative array of renamings or callable |
||
| 363 | * that would rename the fields. |
||
| 364 | * |
||
| 365 | * @return LogicalFilter $this |
||
| 366 | */ |
||
| 367 | 1 | public function renameFields($renamings) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param array|callable Associative array of renamings or callable |
||
| 378 | * that would rename the fields. |
||
| 379 | * |
||
| 380 | * @return string $this |
||
| 381 | */ |
||
| 382 | 10 | public function removeRules($filter) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Apply a "RuleFilter" on the rules of the current instance. |
||
| 421 | * |
||
| 422 | * @param array|LogicalFilter|AbstractRule $rules |
||
| 423 | * @param array|callable $options |
||
| 424 | * |
||
| 425 | * @return array The rules matching the filter |
||
| 426 | */ |
||
| 427 | 21 | public function filterRules($rules=[], array $options=[]) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param array|callable Associative array of renamings or callable |
||
| 450 | * that would rename the fields. |
||
| 451 | * |
||
| 452 | * @return array The rules matching the filter |
||
| 453 | * @return array $options debug | leaves_only | clean_empty_branches |
||
| 454 | */ |
||
| 455 | 4 | public function keepLeafRulesMatching($filter=[], array $options=[]) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param array|callable Associative array of renamings or callable |
||
| 496 | * that would rename the fields. |
||
| 497 | * |
||
| 498 | * @return array The rules matching the filter |
||
| 499 | * |
||
| 500 | * |
||
| 501 | * @todo Merge with rules |
||
| 502 | */ |
||
| 503 | 3 | public function listLeafRulesMatching($filter=[]) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * $filter->onEachRule( |
||
| 539 | * ['field', 'in', [...]], |
||
| 540 | * function ($rule, $key, array &$rules) { |
||
| 541 | * // ... |
||
| 542 | * }) |
||
| 543 | * |
||
| 544 | * $filter->onEachRule( |
||
| 545 | * ['field', 'in', [...]], |
||
| 546 | * [ |
||
| 547 | * Filterer::on_row_matches => function ($rule, $key, array &$rules) { |
||
| 548 | * // ... |
||
| 549 | * }, |
||
| 550 | * Filterer::on_row_mismatches => function ($rule, $key, array &$rules) { |
||
| 551 | * // ... |
||
| 552 | * }, |
||
| 553 | * ] |
||
| 554 | * ) |
||
| 555 | * |
||
| 556 | * @todo Make it available on AbstractRule also |
||
| 557 | * |
||
| 558 | * @param array|LogicalFilter |
||
| 559 | * @param array|callable Associative array of renamings or callable |
||
| 560 | * that would rename the fields. |
||
| 561 | * |
||
| 562 | * @return array The rules matching the filter |
||
| 563 | */ |
||
| 564 | 93 | public function onEachRule($filter=[], $options) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * $filter->onEachCase(function (AndRule $case, $key, array &$caseRules) { |
||
| 591 | * // do whatever you want on the current case... |
||
| 592 | * }) |
||
| 593 | * |
||
| 594 | * @param array|callable $action Callback to apply on each case. |
||
| 595 | * @return LogicalFilter $this |
||
| 596 | * |
||
| 597 | * @todo Make it available on AbstractRule also |
||
| 598 | */ |
||
| 599 | 6 | public function onEachCase(callable $action) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Retrieves the minimum possibility and the maximum possibility for |
||
| 623 | * each field of the rules matching the filter. |
||
| 624 | * |
||
| 625 | * @param array|LogicalFilter|AbstractRule $ruleFilter |
||
| 626 | * |
||
| 627 | * @return array The bounds of the range and a nullable property for each field |
||
| 628 | */ |
||
| 629 | 5 | public function getRanges($ruleFilter=null) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Retrieves the minimum possibility and the maximum possibility for |
||
| 690 | * the given field. |
||
| 691 | * |
||
| 692 | * @param mixed $field |
||
| 693 | * @return array The bounds of the range and a nullable property for the given field |
||
| 694 | */ |
||
| 695 | 5 | public function getFieldRange($field) |
|
| 702 | |||
| 703 | /** |
||
| 704 | * Clone the current object and its rules. |
||
| 705 | * |
||
| 706 | * @return LogicalFilter A copy of the current instance with a copied ruletree |
||
| 707 | */ |
||
| 708 | 7 | public function copy() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Make a deep copy of the rules |
||
| 715 | */ |
||
| 716 | 7 | public function __clone() |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Copy the current instance into the variable given as parameter |
||
| 725 | * and returns the copy. |
||
| 726 | * |
||
| 727 | * @return LogicalFilter |
||
| 728 | */ |
||
| 729 | 3 | public function saveAs( &$variable) |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Copy the current instance into the variable given as parameter |
||
| 736 | * and returns the copied instance. |
||
| 737 | * |
||
| 738 | * @return LogicalFilter |
||
| 739 | */ |
||
| 740 | 1 | public function saveCopyAs( &$copied_variable) |
|
| 745 | |||
| 746 | /** |
||
| 747 | * @param bool $exit=false |
||
| 748 | * @param array $options + callstack_depth=2 The level of the caller to dump |
||
| 749 | * + mode='string' in 'export' | 'dump' | 'string' |
||
| 750 | * |
||
| 751 | * @return $this |
||
| 752 | */ |
||
| 753 | 4 | public function dump($exit=false, array $options=[]) |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Applies the current instance to a set of data. |
||
| 808 | * |
||
| 809 | * @param mixed $data_to_filter |
||
| 810 | * @param Filterer|callable|null $filterer |
||
| 811 | * |
||
| 812 | * @return mixed The filtered data |
||
| 813 | */ |
||
| 814 | 26 | public function applyOn($data_to_filter, $action_on_matches=null, $filterer=null) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Applies the current instance to a value (and its index optionnally). |
||
| 840 | * |
||
| 841 | * @param mixed $value_to_check |
||
| 842 | * @param scalar $index |
||
| 843 | * @param Filterer|callable|null $filterer |
||
| 844 | * |
||
| 845 | * @return AbstractRule|false|true + False if the filter doesn't validates |
||
| 846 | * + Null if the target has no sens (operation filtered by field for example) |
||
| 847 | * + A rule tree containing the first matching case if there is one. |
||
| 848 | */ |
||
| 849 | 4 | public function validates($value_to_check, $key_to_check=null, $filterer=null) |
|
| 866 | |||
| 867 | /**/ |
||
| 868 | } |
||
| 869 |
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.