Complex classes like Enumerator 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 Enumerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Enumerator |
||
9 | { |
||
10 | use SplitPathTrait; |
||
11 | use ValueTrait; |
||
12 | |||
13 | /** |
||
14 | * Return the first element in an array passing a given truth test. |
||
15 | * |
||
16 | * @param array $array |
||
17 | * @param callable $callback |
||
18 | * @param mixed $default |
||
19 | * |
||
20 | * @return mixed |
||
21 | */ |
||
22 | public function first($array, callable $callback, $default = null) |
||
32 | |||
33 | /** |
||
34 | * Return the last element in an array passing a given truth test. |
||
35 | * |
||
36 | * @param array $array |
||
37 | * @param callable $callback |
||
38 | * @param mixed $default |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public function last($array, callable $callback, $default = null) |
||
46 | |||
47 | /** |
||
48 | * Get a random element from the array supplied |
||
49 | * |
||
50 | * @param array $array the source array |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public static function random(Array $array) |
||
62 | |||
63 | /** |
||
64 | * Get a subset of the items from the given array. |
||
65 | * |
||
66 | * @param string[] $array |
||
67 | * @param string[] $keys |
||
68 | * |
||
69 | * @return string[] |
||
70 | */ |
||
71 | public function only($array, $keys) |
||
75 | |||
76 | /** |
||
77 | * Get a value from the array, and remove it. |
||
78 | * |
||
79 | * @param array $array |
||
80 | * @param string $key |
||
81 | * @param string|null $default |
||
82 | * |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function pull(array &$array, $key, $default = null) |
||
93 | |||
94 | /** |
||
95 | * Pluck an array of values from an array. |
||
96 | * |
||
97 | * @param array|\ArrayAccess $array |
||
98 | * @param string $value |
||
99 | * @param string|null $key |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public function pluck(array $array, $value, $key = null) |
||
124 | |||
125 | /** |
||
126 | * Filter the array using the given Closure. |
||
127 | * |
||
128 | * @param array $array |
||
129 | * @param callable $callback |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | public function where(array $array, callable $callback) |
||
145 | |||
146 | /** |
||
147 | * Get an item from an array or object using "dot" notation. |
||
148 | * |
||
149 | * @param mixed $target |
||
150 | * @param string|array $key |
||
151 | * @param string $default |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function dataGet($target, $key, $default = null) |
||
199 | |||
200 | /** |
||
201 | * Push an item onto the beginning of an array. |
||
202 | * |
||
203 | * @param array $array |
||
204 | * @param mixed $value |
||
205 | * @param mixed $key |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | public function prepend(array $array, $value, $key = null) |
||
219 | |||
220 | /** |
||
221 | * Collapse an array of arrays into a single array. |
||
222 | * |
||
223 | * @param array $array |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function collapse(array $array) |
||
241 | |||
242 | /** |
||
243 | * Replace a given pattern with each value in the array in sequentially. |
||
244 | * |
||
245 | * @param string $pattern |
||
246 | * @param array $replacements |
||
247 | * @param string $subject |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function pregReplaceSub($pattern, &$replacements, $subject) |
||
257 | |||
258 | /** |
||
259 | * A shorter way to run a match on the array's keys rather than the values. |
||
260 | * |
||
261 | * @param string $pattern |
||
262 | * @param array $input |
||
263 | * @param int $flags |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function pregGrepKeys($pattern, array $input, $flags = 0) |
||
271 | |||
272 | /** |
||
273 | * Index the array by array of keys. |
||
274 | * |
||
275 | * @param array $data |
||
276 | * @param array $keys |
||
277 | * @param bool|true $unique |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getIndexedByKeys(array $data, array $keys, $unique = true) |
||
291 | |||
292 | /** |
||
293 | * Converts array of arrays to one-dimensional array, where key is $keyName and value is $valueName. |
||
294 | * |
||
295 | * @param array $array |
||
296 | * @param string $keyName |
||
297 | * @param string|array $valueName |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getIndexedValues(array $array, $keyName, $valueName) |
||
305 | |||
306 | /** |
||
307 | * @param array $result |
||
308 | * @param array $toSave |
||
309 | * @param array $keys |
||
310 | * @param bool|true $unique |
||
311 | */ |
||
312 | protected function indexByKeys(array &$result, array $toSave, array $keys, $unique = true) |
||
328 | |||
329 | /** |
||
330 | * Explode the "value" and "key" arguments passed to "pluck". |
||
331 | * |
||
332 | * @param string $value |
||
333 | * @param string|null $key |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | protected function explodePluckParameters($value, $key) |
||
345 | } |
||
346 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.