Complex classes like GenericSequence 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 GenericSequence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait GenericSequence |
||
16 | { |
||
17 | /** |
||
18 | * @var array internal array used to store the values of the sequence. |
||
19 | */ |
||
20 | private $array = []; |
||
21 | |||
22 | /** |
||
23 | * @inheritDoc |
||
24 | */ |
||
25 | public function __construct($values = null) |
||
31 | |||
32 | /** |
||
33 | * @inheritdoc |
||
34 | */ |
||
35 | public function toArray(): array |
||
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | public function apply(callable $callback) |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | public function merge($values): Sequence |
||
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | public function count(): int |
||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | public function contains(...$values): bool |
||
81 | |||
82 | /** |
||
83 | * @inheritDoc |
||
84 | */ |
||
85 | public function filter(callable $callback = null): Sequence |
||
89 | |||
90 | /** |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function find($value) |
||
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | public function first() |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function get(int $index) |
||
121 | |||
122 | /** |
||
123 | * @inheritDoc |
||
124 | */ |
||
125 | public function insert(int $index, ...$values) |
||
133 | |||
134 | /** |
||
135 | * @inheritDoc |
||
136 | */ |
||
137 | public function join(string $glue = null): string |
||
141 | |||
142 | /** |
||
143 | * @inheritDoc |
||
144 | */ |
||
145 | public function last() |
||
153 | |||
154 | /** |
||
155 | * @inheritDoc |
||
156 | */ |
||
157 | public function map(callable $callback): Sequence |
||
161 | |||
162 | /** |
||
163 | * @inheritDoc |
||
164 | */ |
||
165 | public function pop() |
||
176 | |||
177 | /** |
||
178 | * Pushes all values of either an array or traversable object. |
||
179 | */ |
||
180 | private function pushAll($values) |
||
188 | |||
189 | /** |
||
190 | * @inheritDoc |
||
191 | */ |
||
192 | public function push(...$values) |
||
196 | |||
197 | /** |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | public function reduce(callable $callback, $initial = null) |
||
204 | |||
205 | /** |
||
206 | * @inheritDoc |
||
207 | */ |
||
208 | public function remove(int $index) |
||
219 | |||
220 | /** |
||
221 | * @inheritDoc |
||
222 | */ |
||
223 | public function reverse() |
||
227 | |||
228 | /** |
||
229 | * @inheritDoc |
||
230 | */ |
||
231 | public function reversed(): Sequence |
||
235 | |||
236 | /** |
||
237 | * Converts negative or large rotations into the minimum positive number |
||
238 | * of rotations required to rotate the sequence by a given $r. |
||
239 | */ |
||
240 | private function normalizeRotations(int $r) |
||
249 | |||
250 | /** |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | public function rotate(int $rotations) |
||
259 | |||
260 | /** |
||
261 | * @inheritDoc |
||
262 | */ |
||
263 | public function set(int $index, $value) |
||
271 | |||
272 | /** |
||
273 | * @inheritDoc |
||
274 | */ |
||
275 | public function shift() |
||
286 | |||
287 | /** |
||
288 | * @inheritDoc |
||
289 | */ |
||
290 | public function slice(int $offset, int $length = null): Sequence |
||
298 | |||
299 | /** |
||
300 | * @inheritDoc |
||
301 | */ |
||
302 | public function sort(callable $comparator = null) |
||
310 | |||
311 | /** |
||
312 | * @inheritDoc |
||
313 | */ |
||
314 | public function sorted(callable $comparator = null): Sequence |
||
320 | |||
321 | /** |
||
322 | * @inheritDoc |
||
323 | */ |
||
324 | public function sum() |
||
328 | |||
329 | /** |
||
330 | * @inheritDoc |
||
331 | */ |
||
332 | public function unshift(...$values) |
||
339 | |||
340 | /** |
||
341 | * |
||
342 | */ |
||
343 | private function validIndex(int $index) |
||
347 | |||
348 | /** |
||
349 | * |
||
350 | */ |
||
351 | public function getIterator() |
||
357 | |||
358 | /** |
||
359 | * @inheritdoc |
||
360 | */ |
||
361 | public function clear() |
||
366 | |||
367 | /** |
||
368 | * @inheritdoc |
||
369 | */ |
||
370 | public function offsetSet($offset, $value) |
||
378 | |||
379 | /** |
||
380 | * @inheritdoc |
||
381 | */ |
||
382 | public function &offsetGet($offset) |
||
390 | |||
391 | /** |
||
392 | * @inheritdoc |
||
393 | */ |
||
394 | public function offsetUnset($offset) |
||
400 | |||
401 | /** |
||
402 | * @inheritdoc |
||
403 | */ |
||
404 | public function offsetExists($offset) |
||
410 | } |
||
411 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.