Complex classes like ArrayQuery 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 ArrayQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class ArrayQuery |
||
26 | { |
||
27 | /** |
||
28 | * @var array the data to search, filter. |
||
29 | */ |
||
30 | private $data; |
||
31 | /** |
||
32 | * @var array the array tokenized so user can search multidimensional array by key paths -ie `parentkey.child` |
||
33 | */ |
||
34 | private $tokens; |
||
35 | /** |
||
36 | * @var array the conditions to apply |
||
37 | */ |
||
38 | private $conditions = []; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * @param array $array |
||
43 | */ |
||
44 | public function __construct(array $array) |
||
51 | |||
52 | /** |
||
53 | * Adds a condition to apply the array |
||
54 | * |
||
55 | * @param string $key the key to search in the array |
||
56 | * @param mixed $value the value to search. It supports SQL like operator plus some custom ones: |
||
57 | * - `~` or `like` : like `%value%` in SQL |
||
58 | * - `n~` or `nlike` : like `NOT LIKE` in SQL |
||
59 | * @param string $operator the operator. It can be `and` or `or`. If any of `or` matches it will be added to the |
||
60 | * successful results. |
||
61 | * |
||
62 | * @return static |
||
63 | */ |
||
64 | public function addCondition($key, $value, $operator = 'and') |
||
112 | |||
113 | /** |
||
114 | * Returns the first matched result. |
||
115 | * @return array the first matched result, empty array if none found. |
||
116 | */ |
||
117 | public function one() |
||
127 | |||
128 | /** |
||
129 | * Returns array of matched results. |
||
130 | * @return array the matched results. |
||
131 | */ |
||
132 | public function find() |
||
146 | |||
147 | /** |
||
148 | * Tokenizes the array to ease the search in multidimensional arrays. |
||
149 | * |
||
150 | * @param array $array the array to tokenize |
||
151 | * @param string $prefix the key prefix |
||
152 | * @param bool $addParent whether to add parent value anyway. False |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function tokenize($array, $prefix = '', $addParent = true) |
||
177 | |||
178 | /** |
||
179 | * Checks data against conditions |
||
180 | * |
||
181 | * @param mixed $data the data to match against. |
||
182 | * |
||
183 | * @return bool true if matches condition |
||
184 | */ |
||
185 | private function matches($data) |
||
208 | } |
||
209 |