Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Collection implements TransmutableTypeInterface, CollectionInterface, Selectable |
||
28 | { |
||
29 | use Transmutable; |
||
30 | use Boxable; |
||
31 | |||
32 | /** |
||
33 | * @var null|string |
||
34 | */ |
||
35 | private $type; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $elements; |
||
41 | |||
42 | /** |
||
43 | * @param array $elements |
||
44 | * @param null|string $type |
||
45 | */ |
||
46 | 70 | public function __construct(array $elements = array(), string $type = null) |
|
53 | |||
54 | /** |
||
55 | * @param $element |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | 1 | public function unshift($element) |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 3 | public function first() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 3 | public function last() |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 6 | public function key() |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 9 | public function next() |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 6 | public function current() |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 1 | public function remove($key) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 1 | public function removeElement($element) |
|
136 | |||
137 | /** |
||
138 | * Required by interface ArrayAccess. |
||
139 | * |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 1 | public function offsetExists($offset) |
|
146 | |||
147 | /** |
||
148 | * Required by interface ArrayAccess. |
||
149 | * |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 1 | public function offsetGet($offset) |
|
156 | |||
157 | /** |
||
158 | * Required by interface ArrayAccess. |
||
159 | * |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function offsetSet($offset, $value) |
||
170 | |||
171 | /** |
||
172 | * Required by interface ArrayAccess. |
||
173 | * |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | 1 | public function offsetUnset($offset) |
|
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | 1 | public function containsKey($key) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 1 | public function contains($element) |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 1 | public function exists(Closure $p) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 1 | public function indexOf($element) |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 1 | public function get($key) |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 3 | public function getKeys() |
|
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | 3 | public function getValues() |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | 5 | public function count() |
|
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | 1 | public function isEmpty() |
|
258 | |||
259 | /** |
||
260 | * Required by interface IteratorAggregate. |
||
261 | * |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 10 | public function getIterator() |
|
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | * |
||
272 | * @return Collection |
||
273 | */ |
||
274 | public function map(Closure $func) |
||
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | * |
||
282 | * @return Collection |
||
283 | */ |
||
284 | public function filter(Closure $p) |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | 2 | public function forAll(Closure $p) |
|
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | * |
||
306 | * @return Collection |
||
307 | */ |
||
308 | public function partition(Closure $p) |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function clear() |
||
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | public function slice($offset, $length = null) |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | * |
||
342 | * @return Collection |
||
343 | */ |
||
344 | 1 | public function matching(Criteria $criteria) |
|
375 | |||
376 | /** |
||
377 | * @throws \LogicException when not collection is untyped or collection type does not contain __toString method |
||
378 | * |
||
379 | * @return Collection |
||
380 | */ |
||
381 | public function unique() |
||
382 | { |
||
383 | 2 | $closure = function ($key, $value) { |
|
384 | 2 | return is_string($value); |
|
385 | 2 | }; |
|
386 | |||
387 | 2 | if ((null !== $this->type && method_exists($this->type, '__toString')) || $this->forAll($closure)) { |
|
388 | 1 | $result = new static(array_unique($this->elements, SORT_STRING), $this->type); |
|
389 | |||
390 | 1 | return $result; |
|
391 | } |
||
392 | |||
393 | 1 | throw new \LogicException('Collection instance is not typed, or type has no string support.'); |
|
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param mixed $value |
||
398 | */ |
||
399 | 2 | public function add($value) |
|
405 | |||
406 | 1 | public function set($key, $value) |
|
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | * |
||
414 | * @return string|array|int |
||
415 | */ |
||
416 | 21 | public function __invoke(int $toType = Type::ARRAY) |
|
435 | |||
436 | /** |
||
437 | * @param CollectionInterface $collection |
||
438 | * @param bool $keepDupes |
||
439 | * |
||
440 | * @return Collection |
||
441 | */ |
||
442 | 2 | public function merge(CollectionInterface $collection, $keepDupes = false): Collection |
|
450 | |||
451 | /** |
||
452 | * {@inheritdoc} |
||
453 | * |
||
454 | * @return Collection |
||
455 | */ |
||
456 | 4 | public static function valueOf($mixed): Collection |
|
460 | |||
461 | /** |
||
462 | * @param string $delimeter |
||
463 | * |
||
464 | * @return StringType |
||
465 | */ |
||
466 | 1 | public function implode(string $delimeter): StringType |
|
470 | |||
471 | /** |
||
472 | * Returns a mixed variable as an array. |
||
473 | * |
||
474 | * @param $mixed |
||
475 | * |
||
476 | * @return array |
||
477 | */ |
||
478 | 4 | private static function asArray($mixed): array |
|
504 | |||
505 | /** |
||
506 | * @param $value |
||
507 | * |
||
508 | * @return mixed |
||
509 | */ |
||
510 | 66 | private function getRealValue($value) |
|
518 | } |
||
519 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.