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 | |||
8 | /** @var array $ruleAliases */ |
||
9 | protected static $ruleAliases = [ |
||
10 | '!' => 'not', |
||
11 | '=' => 'equal', |
||
12 | '>' => 'above', |
||
13 | '<' => 'below', |
||
14 | '><' => 'between', |
||
15 | '=><' => 'between_or_equal_lower', |
||
16 | '><=' => 'between_or_equal_upper', |
||
17 | '=><=' => 'between_or_equal_both', |
||
18 | '<=' => 'below_or_equal', |
||
19 | '>=' => 'above_or_equal', |
||
20 | '!=' => 'not_equal', |
||
21 | 'in' => 'in', |
||
22 | '!in' => 'not_in', |
||
23 | ]; |
||
24 | |||
25 | /** @var array $cache */ |
||
26 | protected $cache = [ |
||
27 | 'array' => null, |
||
28 | 'string' => null, |
||
29 | 'semantic_id' => null, |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @param string $english_operator |
||
34 | * @return string |
||
35 | */ |
||
36 | 134 | public static function findSymbolicOperator($english_operator) |
|
45 | |||
46 | /** |
||
47 | * @param string $symbolic_operator |
||
48 | * @return string |
||
49 | */ |
||
50 | 97 | public static function findEnglishOperator($symbolic_operator) |
|
59 | |||
60 | protected static $static_cache = [ |
||
61 | 'rules_generation' => [], |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * @param string $field |
||
67 | * @param string $type |
||
68 | * @param mixed $values |
||
69 | * @param array $options |
||
70 | * |
||
71 | * @return AbstractRule |
||
72 | */ |
||
73 | 138 | public static function generateSimpleRule($field, $type, $values, array $options=[]) |
|
84 | |||
85 | /** |
||
86 | * @param string $rule_operator |
||
87 | * |
||
88 | * @return string Class corresponding to the given operator |
||
89 | */ |
||
90 | 97 | public static function getRuleClass($rule_operator) |
|
108 | |||
109 | /** |
||
110 | * Clones the rule with a chained syntax. |
||
111 | * |
||
112 | * @return AbstractRule A copy of the current instance. |
||
113 | */ |
||
114 | 130 | public function copy() |
|
118 | |||
119 | /** |
||
120 | * Dumps the rule with a chained syntax. |
||
121 | * |
||
122 | * @param bool $exit Default false |
||
123 | * @param array $options + callstack_depth=2 The level of the caller to dump |
||
124 | * + mode='string' in 'export' | 'dump' | 'string' | 'xdebug' |
||
125 | * |
||
126 | * @return $this |
||
|
|||
127 | */ |
||
128 | 4 | final public function dump($exit=false, array $options=[]) |
|
199 | |||
200 | /** |
||
201 | */ |
||
202 | 167 | public function flushCache() |
|
212 | |||
213 | /** |
||
214 | */ |
||
215 | 1 | public static function flushStaticCache() |
|
221 | |||
222 | /** |
||
223 | * For implementing JsonSerializable interface. |
||
224 | * |
||
225 | * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
||
226 | */ |
||
227 | public function jsonSerialize() |
||
231 | |||
232 | /** |
||
233 | * @return string |
||
234 | */ |
||
235 | 3 | public function __toString() |
|
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | abstract public function toString(array $options=[]); |
||
244 | |||
245 | /** |
||
246 | * @return array |
||
247 | */ |
||
248 | abstract public function toArray(array $options=[]); |
||
249 | |||
250 | protected $instance_id; |
||
251 | |||
252 | /** |
||
253 | * Returns an id describing the instance internally for debug purpose. |
||
254 | * |
||
255 | * @see https://secure.php.net/manual/en/function.spl-object-id.php |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 1 | public function getInstanceId() |
|
267 | |||
268 | /** |
||
269 | * Returns an id corresponding to the meaning of the rule. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | 156 | final public function getSemanticId() |
|
284 | |||
285 | /** |
||
286 | * @deprecated addMinimalCase |
||
287 | */ |
||
288 | 4 | protected function forceLogicalCore() |
|
292 | |||
293 | /** |
||
294 | * Forces the two firsts levels of the tree to be an OrRule having |
||
295 | * only AndRules as operands: |
||
296 | * ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]] |
||
297 | * As a simplified ruleTree will alwways be reduced to this structure |
||
298 | * with no suboperands others than atomic ones or a simpler one like: |
||
299 | * ['or', ['field', '=', '1'], ['field2', '>', '3']] |
||
300 | * |
||
301 | * This helpes to ease the result of simplify() |
||
302 | * |
||
303 | * @return OrRule |
||
304 | */ |
||
305 | 43 | public function addMinimalCase() |
|
370 | |||
371 | /**/ |
||
372 | } |
||
373 |
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.