Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PersistentRelatedEntitiesCollection 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 PersistentRelatedEntitiesCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PersistentRelatedEntitiesCollection implements Collection, Selectable |
||
23 | { |
||
24 | private $registry; |
||
25 | private $job; |
||
26 | private $entities; |
||
27 | |||
28 | 32 | public function __construct(ManagerRegistry $registry, Job $job) |
|
33 | |||
34 | /** |
||
35 | * Gets the PHP array representation of this collection. |
||
36 | * |
||
37 | * @return array<object> The PHP array representation of this collection. |
||
38 | */ |
||
39 | public function toArray() |
||
45 | |||
46 | /** |
||
47 | * Sets the internal iterator to the first element in the collection and |
||
48 | * returns this element. |
||
49 | * |
||
50 | * @return object|false |
||
51 | */ |
||
52 | 2 | public function first() |
|
58 | |||
59 | /** |
||
60 | * Sets the internal iterator to the last element in the collection and |
||
61 | * returns this element. |
||
62 | * |
||
63 | * @return object|false |
||
64 | */ |
||
65 | public function last() |
||
71 | |||
72 | /** |
||
73 | * Gets the current key/index at the current internal iterator position. |
||
74 | * |
||
75 | * @return string|integer |
||
76 | */ |
||
77 | public function key() |
||
83 | |||
84 | /** |
||
85 | * Moves the internal iterator position to the next element. |
||
86 | * |
||
87 | * @return object|false |
||
88 | */ |
||
89 | public function next() |
||
95 | |||
96 | /** |
||
97 | * Gets the element of the collection at the current internal iterator position. |
||
98 | * |
||
99 | * @return object|false |
||
100 | */ |
||
101 | public function current() |
||
107 | |||
108 | /** |
||
109 | * Removes an element with a specific key/index from the collection. |
||
110 | * |
||
111 | * @param string|integer $key |
||
112 | * @return object|null The removed element or NULL, if no element exists for the given key. |
||
113 | */ |
||
114 | public function remove($key) |
||
118 | |||
119 | /** |
||
120 | * Removes the specified element from the collection, if it is found. |
||
121 | * |
||
122 | * @param object $element The element to remove. |
||
123 | * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. |
||
124 | */ |
||
125 | public function removeElement($element) |
||
129 | |||
130 | /** |
||
131 | * ArrayAccess implementation of offsetExists() |
||
132 | * |
||
133 | * @see containsKey() |
||
134 | * |
||
135 | * @param mixed $offset |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function offsetExists($offset) |
||
144 | |||
145 | /** |
||
146 | * ArrayAccess implementation of offsetGet() |
||
147 | * |
||
148 | * @see get() |
||
149 | * |
||
150 | * @param mixed $offset |
||
151 | * @return mixed |
||
152 | */ |
||
153 | public function offsetGet($offset) |
||
159 | |||
160 | /** |
||
161 | * ArrayAccess implementation of offsetSet() |
||
162 | * |
||
163 | * @see add() |
||
164 | * @see set() |
||
165 | * |
||
166 | * @param mixed $offset |
||
167 | * @param mixed $value |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function offsetSet($offset, $value) |
||
174 | |||
175 | /** |
||
176 | * ArrayAccess implementation of offsetUnset() |
||
177 | * |
||
178 | * @see remove() |
||
179 | * |
||
180 | * @param mixed $offset |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function offsetUnset($offset) |
||
187 | |||
188 | /** |
||
189 | * Checks whether the collection contains a specific key/index. |
||
190 | * |
||
191 | * @param mixed $key The key to check for. |
||
192 | * @return boolean TRUE if the given key/index exists, FALSE otherwise. |
||
193 | */ |
||
194 | public function containsKey($key) |
||
200 | |||
201 | /** |
||
202 | * Checks whether the given element is contained in the collection. |
||
203 | * Only element values are compared, not keys. The comparison of two elements |
||
204 | * is strict, that means not only the value but also the type must match. |
||
205 | * For objects this means reference equality. |
||
206 | * |
||
207 | * @param mixed $element |
||
208 | * @return boolean TRUE if the given element is contained in the collection, |
||
209 | * FALSE otherwise. |
||
210 | */ |
||
211 | public function contains($element) |
||
223 | |||
224 | /** |
||
225 | * Tests for the existence of an element that satisfies the given predicate. |
||
226 | * |
||
227 | * @param Closure $p The predicate. |
||
228 | * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise. |
||
229 | */ |
||
230 | View Code Duplication | public function exists(Closure $p) |
|
241 | |||
242 | /** |
||
243 | * Searches for a given element and, if found, returns the corresponding key/index |
||
244 | * of that element. The comparison of two elements is strict, that means not |
||
245 | * only the value but also the type must match. |
||
246 | * For objects this means reference equality. |
||
247 | * |
||
248 | * @param mixed $element The element to search for. |
||
249 | * @return mixed The key/index of the element or FALSE if the element was not found. |
||
250 | */ |
||
251 | public function indexOf($element) |
||
257 | |||
258 | /** |
||
259 | * Gets the element with the given key/index. |
||
260 | * |
||
261 | * @param mixed $key The key. |
||
262 | * @return mixed The element or NULL, if no element exists for the given key. |
||
263 | */ |
||
264 | public function get($key) |
||
273 | |||
274 | /** |
||
275 | * Gets all keys/indexes of the collection elements. |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getKeys() |
||
285 | |||
286 | /** |
||
287 | * Gets all elements. |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getValues() |
||
297 | |||
298 | /** |
||
299 | * Returns the number of elements in the collection. |
||
300 | * |
||
301 | * Implementation of the Countable interface. |
||
302 | * |
||
303 | * @return integer The number of elements in the collection. |
||
304 | */ |
||
305 | 2 | public function count() |
|
311 | |||
312 | /** |
||
313 | * Adds/sets an element in the collection at the index / with the specified key. |
||
314 | * |
||
315 | * When the collection is a Map this is like put(key,value)/add(key,value). |
||
316 | * When the collection is a List this is like add(position,value). |
||
317 | * |
||
318 | * @param mixed $key |
||
319 | * @param mixed $value |
||
320 | */ |
||
321 | public function set($key, $value) |
||
325 | |||
326 | /** |
||
327 | * Adds an element to the collection. |
||
328 | * |
||
329 | * @param mixed $value |
||
330 | * @return boolean Always TRUE. |
||
331 | */ |
||
332 | public function add($value) |
||
336 | |||
337 | /** |
||
338 | * Checks whether the collection is empty. |
||
339 | * |
||
340 | * Note: This is preferable over count() == 0. |
||
341 | * |
||
342 | * @return boolean TRUE if the collection is empty, FALSE otherwise. |
||
343 | */ |
||
344 | public function isEmpty() |
||
350 | |||
351 | /** |
||
352 | * Gets an iterator for iterating over the elements in the collection. |
||
353 | * |
||
354 | * @return ArrayIterator |
||
355 | */ |
||
356 | 2 | public function getIterator() |
|
362 | |||
363 | /** |
||
364 | * Applies the given function to each element in the collection and returns |
||
365 | * a new collection with the elements returned by the function. |
||
366 | * |
||
367 | * @param Closure $func |
||
368 | * @return Collection |
||
369 | */ |
||
370 | public function map(Closure $func) |
||
376 | |||
377 | /** |
||
378 | * Returns all the elements of this collection that satisfy the predicate p. |
||
379 | * The order of the elements is preserved. |
||
380 | * |
||
381 | * @param Closure $p The predicate used for filtering. |
||
382 | * @return Collection A collection with the results of the filter operation. |
||
383 | */ |
||
384 | public function filter(Closure $p) |
||
390 | |||
391 | /** |
||
392 | * Applies the given predicate p to all elements of this collection, |
||
393 | * returning true, if the predicate yields true for all elements. |
||
394 | * |
||
395 | * @param Closure $p The predicate. |
||
396 | * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. |
||
397 | */ |
||
398 | View Code Duplication | public function forAll(Closure $p) |
|
410 | |||
411 | /** |
||
412 | * Partitions this collection in two collections according to a predicate. |
||
413 | * Keys are preserved in the resulting collections. |
||
414 | * |
||
415 | * @param Closure $p The predicate on which to partition. |
||
416 | * @return array An array with two elements. The first element contains the collection |
||
417 | * of elements where the predicate returned TRUE, the second element |
||
418 | * contains the collection of elements where the predicate returned FALSE. |
||
419 | */ |
||
420 | public function partition(Closure $p) |
||
434 | |||
435 | /** |
||
436 | * Returns a string representation of this object. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | public function __toString() |
||
444 | |||
445 | /** |
||
446 | * Clears the collection. |
||
447 | */ |
||
448 | public function clear() |
||
452 | |||
453 | /** |
||
454 | * Extract a slice of $length elements starting at position $offset from the Collection. |
||
455 | * |
||
456 | * If $length is null it returns all elements from $offset to the end of the Collection. |
||
457 | * Keys have to be preserved by this method. Calling this method will only return the |
||
458 | * selected slice and NOT change the elements contained in the collection slice is called on. |
||
459 | * |
||
460 | * @param int $offset |
||
461 | * @param int $length |
||
462 | * @return array |
||
463 | */ |
||
464 | public function slice($offset, $length = null) |
||
470 | |||
471 | /** |
||
472 | * Select all elements from a selectable that match the criteria and |
||
473 | * return a new collection containing these elements. |
||
474 | * |
||
475 | * @param Criteria $criteria |
||
476 | * @return Collection |
||
477 | */ |
||
478 | public function matching(Criteria $criteria) |
||
509 | |||
510 | 4 | private function initialize() |
|
557 | } |
||
558 |
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.