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 | 148 | 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 | 55 | public static function getDefaultOptions() |
|
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | 138 | 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 | 144 | 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 | 140 | 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 | 135 | protected function addCompositeRule_recursion( |
|
415 | |||
416 | /** |
||
417 | * This method parses different ways to define the rules of a LogicalFilter |
||
418 | * and add them as a new And part of the filter. |
||
419 | * + You can add N already instanciated Rules. |
||
420 | * + You can provide 3 arguments: $field, $operator, $value |
||
421 | * + You can provide a tree of rules: |
||
422 | * [ |
||
423 | * 'or', |
||
424 | * [ |
||
425 | * 'and', |
||
426 | * ['field_5', 'above', 'a'], |
||
427 | * ['field_5', 'below', 'a'], |
||
428 | * ], |
||
429 | * ['field_6', 'equal', 'b'], |
||
430 | * ] |
||
431 | * |
||
432 | * @param mixed The descriptions of the rules to add |
||
433 | * @return $this |
||
434 | * |
||
435 | * @todo remove the _ for PHP 7 |
||
436 | */ |
||
437 | 141 | public function and_() |
|
442 | |||
443 | /** |
||
444 | * This method parses different ways to define the rules of a LogicalFilter |
||
445 | * and add them as a new Or part of the filter. |
||
446 | * + You can add N already instanciated Rules. |
||
447 | * + You can provide 3 arguments: $field, $operator, $value |
||
448 | * + You can provide a tree of rules: |
||
449 | * [ |
||
450 | * 'or', |
||
451 | * [ |
||
452 | * 'and', |
||
453 | * ['field_5', 'above', 'a'], |
||
454 | * ['field_5', 'below', 'a'], |
||
455 | * ], |
||
456 | * ['field_6', 'equal', 'b'], |
||
457 | * ] |
||
458 | * |
||
459 | * @param mixed The descriptions of the rules to add |
||
460 | * @return $this |
||
461 | * |
||
462 | * @todo |
||
463 | * @todo remove the _ for PHP 7 |
||
464 | */ |
||
465 | 7 | public function or_() |
|
470 | |||
471 | /** |
||
472 | * @deprecated |
||
473 | */ |
||
474 | 1 | public function matches($rules_to_match) |
|
478 | |||
479 | /** |
||
480 | * Checks that a filter matches another one. |
||
481 | * |
||
482 | * @param array|AbstractRule $rules_to_match |
||
483 | * |
||
484 | * @return bool Whether or not this combination of filters has |
||
485 | * potential solutions |
||
486 | */ |
||
487 | 1 | public function hasSolutionIf($rules_to_match) |
|
495 | |||
496 | /** |
||
497 | * Retrieve all the rules. |
||
498 | * |
||
499 | * @param bool $copy By default copy the rule tree to avoid side effects. |
||
500 | * |
||
501 | * @return AbstractRule The tree of rules |
||
502 | */ |
||
503 | 59 | public function getRules($copy = true) |
|
507 | |||
508 | /** |
||
509 | * Remove any constraint being a duplicate of another one. |
||
510 | * |
||
511 | * @param array $options stop_after | stop_before | |
||
512 | * @return $this |
||
513 | */ |
||
514 | 92 | public function simplify($options=[]) |
|
526 | |||
527 | |||
528 | /** |
||
529 | * Forces the two firsts levels of the tree to be an OrRule having |
||
530 | * only AndRules as operands: |
||
531 | * ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]] |
||
532 | * As a simplified ruleTree will alwways be reduced to this structure |
||
533 | * with no suboperands others than atomic ones or a simpler one like: |
||
534 | * ['or', ['field', '=', '1'], ['field2', '>', '3']] |
||
535 | * |
||
536 | * This helpes to ease the result of simplify() |
||
537 | * |
||
538 | * @return OrRule |
||
539 | */ |
||
540 | 29 | public function addMinimalCase() |
|
548 | |||
549 | /** |
||
550 | * Checks if there is at least on set of conditions which is not |
||
551 | * contradictory. |
||
552 | * |
||
553 | * Checking if a filter has solutions require to simplify it first. |
||
554 | * To let the control on the balance between readability and |
||
555 | * performances, the required simplification can be saved or not |
||
556 | * depending on the $save_simplification parameter. |
||
557 | * |
||
558 | * @param $save_simplification |
||
559 | * |
||
560 | * @return bool |
||
561 | */ |
||
562 | 37 | public function hasSolution($save_simplification=true) |
|
575 | |||
576 | /** |
||
577 | * Returns an array describing the rule tree of the Filter. |
||
578 | * |
||
579 | * @param array $options |
||
580 | * |
||
581 | * @return array A description of the rules. |
||
582 | */ |
||
583 | 95 | public function toArray(array $options=[]) |
|
587 | |||
588 | /** |
||
589 | * Returns an array describing the rule tree of the Filter. |
||
590 | * |
||
591 | * @param $debug Provides a source oriented dump. |
||
592 | * |
||
593 | * @return array A description of the rules. |
||
594 | */ |
||
595 | 4 | public function toString(array $options=[]) |
|
599 | |||
600 | /** |
||
601 | * Returns a unique id corresponding to the set of rules of the filter |
||
602 | * |
||
603 | * @return string The unique semantic id |
||
604 | */ |
||
605 | 1 | public function getSemanticId() |
|
609 | |||
610 | /** |
||
611 | * For implementing JsonSerializable interface. |
||
612 | * |
||
613 | * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
||
614 | */ |
||
615 | 1 | public function jsonSerialize() |
|
619 | |||
620 | /** |
||
621 | * @return string |
||
622 | */ |
||
623 | 4 | public function __toString() |
|
627 | |||
628 | /** |
||
629 | * @see https://secure.php.net/manual/en/language.oop5.magic.php#object.invoke |
||
630 | * @param mixed $row |
||
631 | * @return bool |
||
632 | */ |
||
633 | 3 | public function __invoke($row, $key=null) |
|
637 | |||
638 | /** |
||
639 | * Removes all the defined rules. |
||
640 | * |
||
641 | * @return $this |
||
642 | */ |
||
643 | 2 | public function flushRules() |
|
648 | |||
649 | /** |
||
650 | * @param array|callable Associative array of renamings or callable |
||
651 | * that would rename the fields. |
||
652 | * |
||
653 | * @return LogicalFilter $this |
||
654 | */ |
||
655 | 1 | public function renameFields($renamings) |
|
666 | |||
667 | /** |
||
668 | * @param array|callable Associative array of renamings or callable |
||
669 | * that would rename the fields. |
||
670 | * |
||
671 | * @return string $this |
||
672 | */ |
||
673 | 10 | public function removeRules($filter) |
|
709 | |||
710 | /** |
||
711 | * @param array|callable Associative array of renamings or callable |
||
712 | * that would rename the fields. |
||
713 | * |
||
714 | * @return array The rules matching the filter |
||
715 | * @return array $options debug | leaves_only | clean_empty_branches |
||
716 | * |
||
717 | * |
||
718 | * @todo Merge with rules |
||
719 | */ |
||
720 | 4 | public function keepLeafRulesMatching($filter=[], array $options=[]) |
|
758 | |||
759 | /** |
||
760 | * @param array|callable Associative array of renamings or callable |
||
761 | * that would rename the fields. |
||
762 | * |
||
763 | * @return array The rules matching the filter |
||
764 | * |
||
765 | * |
||
766 | * @todo Merge with rules |
||
767 | */ |
||
768 | 3 | public function listLeafRulesMatching($filter=[]) |
|
801 | |||
802 | /** |
||
803 | * $filter->onEachRule( |
||
804 | * ['field', 'in', [...]], |
||
805 | * function ($rule, $key, array &$rules) { |
||
806 | * // ... |
||
807 | * }) |
||
808 | * |
||
809 | * $filter->onEachRule( |
||
810 | * ['field', 'in', [...]], |
||
811 | * [ |
||
812 | * Filterer::on_row_matches => function ($rule, $key, array &$rules) { |
||
813 | * // ... |
||
814 | * }, |
||
815 | * Filterer::on_row_mismatches => function ($rule, $key, array &$rules) { |
||
816 | * // ... |
||
817 | * }, |
||
818 | * ] |
||
819 | * ) |
||
820 | * |
||
821 | * @todo Make it available on AbstractRule also |
||
822 | * |
||
823 | * @param array|LogicalFilter |
||
824 | * @param array|callable Associative array of renamings or callable |
||
825 | * that would rename the fields. |
||
826 | * |
||
827 | * @return array The rules matching the filter |
||
828 | */ |
||
829 | 2 | public function onEachRule($filter=[], $options) |
|
853 | |||
854 | /** |
||
855 | * $filter->onEachCase(function (AndRule $case, $key, array &$caseRules) { |
||
856 | * // do whatever you want on the current case... |
||
857 | * }) |
||
858 | * |
||
859 | * @param array|callable $action Callback to apply on each case. |
||
860 | * @return LogicalFilter $this |
||
861 | * |
||
862 | * @todo Make it available on AbstractRule also |
||
863 | */ |
||
864 | 1 | public function onEachCase(callable $action) |
|
885 | |||
886 | /** |
||
887 | * Retrieves the minimum possibility and the maximum possibility for |
||
888 | * the given field. |
||
889 | * |
||
890 | * @param array|LogicalFilter|AbstractRule $filter |
||
891 | 7 | * @return array The bounds of the range and a nullable |
|
892 | */ |
||
893 | 7 | public function getRanges($ruleFilter=null) |
|
951 | 4 | ||
952 | /** |
||
953 | * Retrieves the minimum possibility and the maximum possibility for |
||
954 | * the given field. |
||
955 | * |
||
956 | * @param mixed $field |
||
957 | * @return array The minimum and maximum possible for the rules of the filter |
||
958 | */ |
||
959 | public function getFieldRange($field) |
||
963 | |||
964 | /** |
||
965 | * Clone the current object and its rules. |
||
966 | * |
||
967 | * @return LogicalFilter A copy of the current instance with a copied ruletree |
||
968 | */ |
||
969 | public function copy() |
||
973 | |||
974 | /** |
||
975 | * Make a deep copy of the rules |
||
976 | */ |
||
977 | public function __clone() |
||
983 | |||
984 | /** |
||
985 | * Copy the current instance into the variable given as parameter |
||
986 | 4 | * and returns the copy. |
|
987 | * |
||
988 | * @return LogicalFilter |
||
989 | */ |
||
990 | public function saveAs( &$variable) |
||
994 | |||
995 | /** |
||
996 | * Copy the current instance into the variable given as parameter |
||
997 | 5 | * and returns the copied instance. |
|
998 | * |
||
999 | 5 | * @return LogicalFilter |
|
1000 | 5 | */ |
|
1001 | 5 | public function saveCopyAs( &$copied_variable) |
|
1006 | |||
1007 | /** |
||
1008 | * @param bool $exit=false |
||
1009 | * @param array $options + callstack_depth=2 The level of the caller to dump |
||
1010 | * + mode='string' in 'export' | 'dump' | 'string' |
||
1011 | * |
||
1012 | 5 | * @return $this |
|
1013 | 2 | */ |
|
1014 | 2 | public function dump($exit=false, array $options=[]) |
|
1066 | |||
1067 | /** |
||
1068 | * Applies the current instance to a set of data. |
||
1069 | * |
||
1070 | * @param mixed $data_to_filter |
||
1071 | * @param Filterer|callable|null $filterer |
||
1072 | * |
||
1073 | * @return mixed The filtered data |
||
1074 | */ |
||
1075 | public function applyOn($data_to_filter, $action_on_matches=null, $filterer=null) |
||
1098 | |||
1099 | /** |
||
1100 | * Applies the current instance to a value (and its index optionnally). |
||
1101 | * |
||
1102 | * @param mixed $value_to_check |
||
1103 | * @param scalar $index |
||
1104 | * @param Filterer|callable|null $filterer |
||
1105 | * |
||
1106 | * @return AbstractRule|false|true + False if the filter doesn't validates |
||
1107 | * + Null if the target has no sens (operation filtered by field for example) |
||
1108 | * + A rule tree containing the first matching case if there is one. |
||
1109 | */ |
||
1110 | public function validates($value_to_check, $key_to_check=null, $filterer=null) |
||
1127 | |||
1128 | /**/ |
||
1129 | } |
||
1130 |
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.