Complex classes like AbstractRule 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 AbstractRule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | abstract class AbstractRule implements \JsonSerializable |
||
5 | { |
||
6 | use Trait_RuleWithOptions; |
||
7 | use Trait_RuleWithCache; |
||
8 | |||
9 | /** @var array $ruleAliases */ |
||
10 | protected static $ruleAliases = [ |
||
11 | '!' => 'not', |
||
12 | '=' => 'equal', |
||
13 | '>' => 'above', |
||
14 | '<' => 'below', |
||
15 | '><' => 'between', |
||
16 | '=><' => 'between_or_equal_lower', |
||
17 | '><=' => 'between_or_equal_upper', |
||
18 | '=><=' => 'between_or_equal_both', |
||
19 | '<=' => 'below_or_equal', |
||
20 | '>=' => 'above_or_equal', |
||
21 | '!=' => 'not_equal', |
||
22 | 'in' => 'in', |
||
23 | '!in' => 'not_in', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @param string $english_operator |
||
28 | * @return string |
||
29 | */ |
||
30 | 139 | public static function findSymbolicOperator($english_operator) |
|
39 | |||
40 | /** |
||
41 | * @param string $symbolic_operator |
||
42 | * @return string |
||
43 | */ |
||
44 | 102 | public static function findEnglishOperator($symbolic_operator) |
|
53 | |||
54 | /** |
||
55 | * |
||
56 | * @param string $field |
||
57 | * @param string $type |
||
58 | * @param mixed $values |
||
59 | * @param array $options |
||
60 | * |
||
61 | * @return AbstractRule |
||
62 | */ |
||
63 | 143 | public static function generateSimpleRule($field, $type, $values, array $options=[]) |
|
74 | |||
75 | /** |
||
76 | * @param string $rule_operator |
||
77 | * |
||
78 | * @return string Class corresponding to the given operator |
||
79 | */ |
||
80 | 102 | public static function getRuleClass($rule_operator) |
|
98 | |||
99 | /** |
||
100 | * Clones the rule with a chained syntax. |
||
101 | * |
||
102 | * @return AbstractRule A copy of the current instance. |
||
103 | */ |
||
104 | 135 | public function copy() |
|
108 | |||
109 | /** |
||
110 | * Dumps the rule with a chained syntax. |
||
111 | * |
||
112 | * @param bool $exit Default false |
||
113 | * @param array $options + callstack_depth=2 The level of the caller to dump |
||
114 | * + mode='string' in 'export' | 'dump' | 'string' | 'xdebug' |
||
115 | * |
||
116 | * @return $this |
||
|
|||
117 | */ |
||
118 | 4 | final public function dump($exit=false, array $options=[]) |
|
189 | |||
190 | /** |
||
191 | * For implementing JsonSerializable interface. |
||
192 | * |
||
193 | * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
||
194 | */ |
||
195 | public function jsonSerialize() |
||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | */ |
||
203 | 3 | public function __toString() |
|
207 | |||
208 | /** |
||
209 | * @return string |
||
210 | */ |
||
211 | abstract public function toString(array $options=[]); |
||
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | abstract public function toArray(array $options=[]); |
||
217 | |||
218 | protected $instance_id; |
||
219 | |||
220 | /** |
||
221 | * Returns an id describing the instance internally for debug purpose. |
||
222 | * |
||
223 | * @see https://secure.php.net/manual/en/function.spl-object-id.php |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | 1 | public function getInstanceId() |
|
235 | |||
236 | /** |
||
237 | * Returns an id corresponding to the meaning of the rule. |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | 161 | final public function getSemanticId() |
|
252 | |||
253 | /** |
||
254 | * @deprecated addMinimalCase |
||
255 | */ |
||
256 | 4 | protected function forceLogicalCore() |
|
260 | |||
261 | /** |
||
262 | * Forces the two firsts levels of the tree to be an OrRule having |
||
263 | * only AndRules as operands: |
||
264 | * ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]] |
||
265 | * As a simplified ruleTree will alwways be reduced to this structure |
||
266 | * with no suboperands others than atomic ones or a simpler one like: |
||
267 | * ['or', ['field', '=', '1'], ['field2', '>', '3']] |
||
268 | * |
||
269 | * This helpes to ease the result of simplify() |
||
270 | * |
||
271 | * @return OrRule |
||
272 | */ |
||
273 | 48 | public function addMinimalCase() |
|
338 | |||
339 | /**/ |
||
340 | } |
||
341 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.