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 | 260 | public function __construct($rules=[], Filterer $default_filterer=null, array $options=[]) |
|
78 | |||
79 | /** |
||
80 | */ |
||
81 | 9 | protected function getDefaultFilterer() |
|
89 | |||
90 | /** |
||
91 | */ |
||
92 | 1 | public static function setDefaultOptions(array $options) |
|
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | 74 | public static function getDefaultOptions() |
|
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | 249 | public function getOptions() |
|
121 | |||
122 | /** |
||
123 | * This method parses different ways to define the rules of a LogicalFilter. |
||
124 | * + You can add N already instanciated Rules. |
||
125 | * + You can provide 3 arguments: $field, $operator, $value |
||
126 | * + You can provide a tree of rules: |
||
127 | * [ |
||
128 | * 'or', |
||
129 | * [ |
||
130 | * 'and', |
||
131 | * ['field_5', 'above', 'a'], |
||
132 | * ['field_5', 'below', 'a'], |
||
133 | * ], |
||
134 | * ['field_6', 'equal', 'b'], |
||
135 | * ] |
||
136 | * |
||
137 | * @param string $operation and | or |
||
138 | * @param array $rules_description Rules description |
||
139 | * @return LogicalFilter $this |
||
140 | */ |
||
141 | 256 | protected function addRules($operation, array $rules_description) |
|
226 | |||
227 | /** |
||
228 | * Add one rule object to the filter |
||
229 | * |
||
230 | * @param AbstractRule $rule |
||
231 | * @param string $operation |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | 251 | protected function addRule( AbstractRule $rule, $operation=AndRule::operator ) |
|
271 | |||
272 | /** |
||
273 | * Recursion auxiliary of addCompositeRule. |
||
274 | * |
||
275 | * @param array $rules_composition The description of the |
||
276 | * rules to add. |
||
277 | * @param AbstractOperationRule $recursion_position The position in the |
||
278 | * tree where rules must |
||
279 | * be added. |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | 248 | protected function addCompositeRule_recursion( |
|
411 | |||
412 | /** |
||
413 | * This method parses different ways to define the rules of a LogicalFilter |
||
414 | * and add them as a new And part of the filter. |
||
415 | * + You can add N already instanciated Rules. |
||
416 | * + You can provide 3 arguments: $field, $operator, $value |
||
417 | * + You can provide a tree of rules: |
||
418 | * [ |
||
419 | * 'or', |
||
420 | * [ |
||
421 | * 'and', |
||
422 | * ['field_5', 'above', 'a'], |
||
423 | * ['field_5', 'below', 'a'], |
||
424 | * ], |
||
425 | * ['field_6', 'equal', 'b'], |
||
426 | * ] |
||
427 | * |
||
428 | * @param mixed The descriptions of the rules to add |
||
429 | * @return $this |
||
430 | * |
||
431 | * @todo remove the _ for PHP 7 |
||
432 | */ |
||
433 | 253 | public function and_() |
|
438 | |||
439 | /** |
||
440 | * This method parses different ways to define the rules of a LogicalFilter |
||
441 | * and add them as a new Or part of the filter. |
||
442 | * + You can add N already instanciated Rules. |
||
443 | * + You can provide 3 arguments: $field, $operator, $value |
||
444 | * + You can provide a tree of rules: |
||
445 | * [ |
||
446 | * 'or', |
||
447 | * [ |
||
448 | * 'and', |
||
449 | * ['field_5', 'above', 'a'], |
||
450 | * ['field_5', 'below', 'a'], |
||
451 | * ], |
||
452 | * ['field_6', 'equal', 'b'], |
||
453 | * ] |
||
454 | * |
||
455 | * @param mixed The descriptions of the rules to add |
||
456 | * @return $this |
||
457 | * |
||
458 | * @todo |
||
459 | * @todo remove the _ for PHP 7 |
||
460 | */ |
||
461 | 24 | public function or_() |
|
466 | |||
467 | /** |
||
468 | * @deprecated |
||
469 | */ |
||
470 | 1 | public function matches($rules_to_match) |
|
474 | |||
475 | /** |
||
476 | * Checks that a filter matches another one. |
||
477 | * |
||
478 | * @param array|AbstractRule $rules_to_match |
||
479 | * |
||
480 | * @return bool Whether or not this combination of filters has |
||
481 | * potential solutions |
||
482 | */ |
||
483 | 1 | public function hasSolutionIf($rules_to_match) |
|
491 | |||
492 | /** |
||
493 | * Retrieve all the rules. |
||
494 | * |
||
495 | * @param bool $copy By default copy the rule tree to avoid side effects. |
||
496 | * |
||
497 | * @return AbstractRule The tree of rules |
||
498 | */ |
||
499 | 112 | public function getRules($copy = true) |
|
503 | |||
504 | /** |
||
505 | * Remove any constraint being a duplicate of another one. |
||
506 | * |
||
507 | * @param array $options stop_after | stop_before | |
||
508 | * @return $this |
||
509 | */ |
||
510 | 121 | public function simplify($options=[]) |
|
522 | |||
523 | |||
524 | /** |
||
525 | * Forces the two firsts levels of the tree to be an OrRule having |
||
526 | * only AndRules as operands: |
||
527 | * ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]] |
||
528 | * As a simplified ruleTree will alwways be reduced to this structure |
||
529 | * with no suboperands others than atomic ones or a simpler one like: |
||
530 | * ['or', ['field', '=', '1'], ['field2', '>', '3']] |
||
531 | * |
||
532 | * This helpes to ease the result of simplify() |
||
533 | * |
||
534 | * @return OrRule |
||
535 | */ |
||
536 | 53 | public function addMinimalCase() |
|
544 | |||
545 | /** |
||
546 | * Checks if there is at least on set of conditions which is not |
||
547 | * contradictory. |
||
548 | * |
||
549 | * Checking if a filter has solutions require to simplify it first. |
||
550 | * To let the control on the balance between readability and |
||
551 | * performances, the required simplification can be saved or not |
||
552 | * depending on the $save_simplification parameter. |
||
553 | * |
||
554 | * @param $save_simplification |
||
555 | * |
||
556 | * @return bool |
||
557 | */ |
||
558 | 61 | public function hasSolution($save_simplification=true) |
|
571 | |||
572 | /** |
||
573 | * Returns an array describing the rule tree of the Filter. |
||
574 | * |
||
575 | * @param array $options |
||
576 | * |
||
577 | * @return array A description of the rules. |
||
578 | */ |
||
579 | 188 | public function toArray(array $options=[]) |
|
583 | |||
584 | /** |
||
585 | * Returns an array describing the rule tree of the Filter. |
||
586 | * |
||
587 | * @param $debug Provides a source oriented dump. |
||
588 | * |
||
589 | * @return array A description of the rules. |
||
590 | */ |
||
591 | 4 | public function toString(array $options=[]) |
|
595 | |||
596 | /** |
||
597 | * Returns a unique id corresponding to the set of rules of the filter |
||
598 | * |
||
599 | * @return string The unique semantic id |
||
600 | */ |
||
601 | 1 | public function getSemanticId() |
|
605 | |||
606 | /** |
||
607 | * For implementing JsonSerializable interface. |
||
608 | * |
||
609 | * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
||
610 | */ |
||
611 | 1 | public function jsonSerialize() |
|
615 | |||
616 | /** |
||
617 | * @return string |
||
618 | */ |
||
619 | 4 | public function __toString() |
|
623 | |||
624 | /** |
||
625 | * @see https://secure.php.net/manual/en/language.oop5.magic.php#object.invoke |
||
626 | * @param mixed $row |
||
627 | * @return bool |
||
628 | */ |
||
629 | 3 | public function __invoke($row, $key=null) |
|
633 | |||
634 | /** |
||
635 | * Removes all the defined rules. |
||
636 | * |
||
637 | * @return $this |
||
638 | */ |
||
639 | 2 | public function flushRules() |
|
644 | |||
645 | /** |
||
646 | * @param array|callable Associative array of renamings or callable |
||
647 | * that would rename the fields. |
||
648 | * |
||
649 | * @return LogicalFilter $this |
||
650 | */ |
||
651 | 1 | public function renameFields($renamings) |
|
659 | |||
660 | /** |
||
661 | * @param array|callable Associative array of renamings or callable |
||
662 | * that would rename the fields. |
||
663 | * |
||
664 | * @return string $this |
||
665 | */ |
||
666 | 10 | public function removeRules($filter) |
|
702 | |||
703 | /** |
||
704 | * Apply a "RuleFilter" on the rules of the current instance. |
||
705 | * |
||
706 | * @param array|LogicalFilter $rule_filter |
||
707 | * @param array|callable $options |
||
708 | * |
||
709 | * @return array The rules matching the filter |
||
710 | */ |
||
711 | 20 | public function filterRules($rule_filter=[], array $options=[]) |
|
727 | |||
728 | /** |
||
729 | * @param array|callable Associative array of renamings or callable |
||
730 | * that would rename the fields. |
||
731 | * |
||
732 | * @return array The rules matching the filter |
||
733 | * @return array $options debug | leaves_only | clean_empty_branches |
||
734 | */ |
||
735 | 4 | public function keepLeafRulesMatching($filter=[], array $options=[]) |
|
773 | |||
774 | /** |
||
775 | * @param array|callable Associative array of renamings or callable |
||
776 | * that would rename the fields. |
||
777 | * |
||
778 | * @return array The rules matching the filter |
||
779 | * |
||
780 | * |
||
781 | * @todo Merge with rules |
||
782 | */ |
||
783 | 3 | public function listLeafRulesMatching($filter=[]) |
|
816 | |||
817 | /** |
||
818 | * $filter->onEachRule( |
||
819 | * ['field', 'in', [...]], |
||
820 | * function ($rule, $key, array &$rules) { |
||
821 | * // ... |
||
822 | * }) |
||
823 | * |
||
824 | * $filter->onEachRule( |
||
825 | * ['field', 'in', [...]], |
||
826 | * [ |
||
827 | * Filterer::on_row_matches => function ($rule, $key, array &$rules) { |
||
828 | * // ... |
||
829 | * }, |
||
830 | * Filterer::on_row_mismatches => function ($rule, $key, array &$rules) { |
||
831 | * // ... |
||
832 | * }, |
||
833 | * ] |
||
834 | * ) |
||
835 | * |
||
836 | * @todo Make it available on AbstractRule also |
||
837 | * |
||
838 | * @param array|LogicalFilter |
||
839 | * @param array|callable Associative array of renamings or callable |
||
840 | * that would rename the fields. |
||
841 | * |
||
842 | * @return array The rules matching the filter |
||
843 | */ |
||
844 | 7 | public function onEachRule($filter=[], $options) |
|
868 | |||
869 | /** |
||
870 | * $filter->onEachCase(function (AndRule $case, $key, array &$caseRules) { |
||
871 | * // do whatever you want on the current case... |
||
872 | * }) |
||
873 | * |
||
874 | * @param array|callable $action Callback to apply on each case. |
||
875 | * @return LogicalFilter $this |
||
876 | * |
||
877 | * @todo Make it available on AbstractRule also |
||
878 | */ |
||
879 | 6 | public function onEachCase(callable $action) |
|
900 | |||
901 | /** |
||
902 | * Retrieves the minimum possibility and the maximum possibility for |
||
903 | * each field of the rules matching the filter. |
||
904 | * |
||
905 | * @param array|LogicalFilter|AbstractRule $ruleFilter |
||
906 | * |
||
907 | * @return array The bounds of the range and a nullable property for each field |
||
908 | */ |
||
909 | 5 | public function getRanges($ruleFilter=null) |
|
967 | |||
968 | /** |
||
969 | * Retrieves the minimum possibility and the maximum possibility for |
||
970 | * the given field. |
||
971 | * |
||
972 | * @param mixed $field |
||
973 | * @return array The bounds of the range and a nullable property for the given field |
||
974 | */ |
||
975 | 5 | public function getFieldRange($field) |
|
982 | |||
983 | /** |
||
984 | * Clone the current object and its rules. |
||
985 | * |
||
986 | * @return LogicalFilter A copy of the current instance with a copied ruletree |
||
987 | */ |
||
988 | 7 | public function copy() |
|
992 | |||
993 | /** |
||
994 | * Make a deep copy of the rules |
||
995 | */ |
||
996 | 7 | public function __clone() |
|
1002 | |||
1003 | /** |
||
1004 | * Copy the current instance into the variable given as parameter |
||
1005 | * and returns the copy. |
||
1006 | * |
||
1007 | * @return LogicalFilter |
||
1008 | */ |
||
1009 | 3 | public function saveAs( &$variable) |
|
1013 | |||
1014 | /** |
||
1015 | * Copy the current instance into the variable given as parameter |
||
1016 | * and returns the copied instance. |
||
1017 | * |
||
1018 | * @return LogicalFilter |
||
1019 | */ |
||
1020 | 1 | public function saveCopyAs( &$copied_variable) |
|
1025 | |||
1026 | /** |
||
1027 | * @param bool $exit=false |
||
1028 | * @param array $options + callstack_depth=2 The level of the caller to dump |
||
1029 | * + mode='string' in 'export' | 'dump' | 'string' |
||
1030 | * |
||
1031 | * @return $this |
||
1032 | */ |
||
1033 | 4 | public function dump($exit=false, array $options=[]) |
|
1085 | |||
1086 | /** |
||
1087 | * Applies the current instance to a set of data. |
||
1088 | * |
||
1089 | * @param mixed $data_to_filter |
||
1090 | * @param Filterer|callable|null $filterer |
||
1091 | * |
||
1092 | * @return mixed The filtered data |
||
1093 | */ |
||
1094 | 5 | public function applyOn($data_to_filter, $action_on_matches=null, $filterer=null) |
|
1117 | |||
1118 | /** |
||
1119 | * Applies the current instance to a value (and its index optionnally). |
||
1120 | * |
||
1121 | * @param mixed $value_to_check |
||
1122 | * @param scalar $index |
||
1123 | * @param Filterer|callable|null $filterer |
||
1124 | * |
||
1125 | * @return AbstractRule|false|true + False if the filter doesn't validates |
||
1126 | * + Null if the target has no sens (operation filtered by field for example) |
||
1127 | * + A rule tree containing the first matching case if there is one. |
||
1128 | */ |
||
1129 | 4 | public function validates($value_to_check, $key_to_check=null, $filterer=null) |
|
1146 | |||
1147 | /**/ |
||
1148 | } |
||
1149 |
This check looks for
@param
annotations 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.