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 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 |
||
10 | class Collection extends \ArrayObject implements CollectionInterface, Checkable |
||
11 | { |
||
12 | |||
13 | use CallableUnifierTrait; |
||
14 | |||
15 | /** |
||
16 | * @param iterable $iterable |
||
17 | * @return static |
||
18 | */ |
||
19 | 69 | View Code Duplication | public static function from($iterable) |
37 | |||
38 | /** |
||
39 | * Alias for getArrayCopy() |
||
40 | * |
||
41 | * @return array |
||
42 | * @see getArrayCopy |
||
43 | */ |
||
44 | 24 | public function toArray() |
|
48 | |||
49 | /** |
||
50 | * Return reindexed items |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | 44 | public function values() |
|
58 | |||
59 | /** |
||
60 | * Execute a callback function on each item |
||
61 | * |
||
62 | * @param callable $callable |
||
63 | */ |
||
64 | 2 | public function each(callable $callable) |
|
71 | |||
72 | /** |
||
73 | * Map a function over a collection |
||
74 | * |
||
75 | * @param callable $callable |
||
76 | * @return static |
||
77 | */ |
||
78 | 14 | public function map(callable $callable) |
|
82 | |||
83 | /** |
||
84 | * @param callable $callable |
||
85 | * @return static |
||
86 | */ |
||
87 | 11 | public function filter(callable $callable) |
|
91 | |||
92 | /** |
||
93 | * Alias for filter() |
||
94 | * |
||
95 | * @see filter() |
||
96 | * |
||
97 | * @param callable $callable |
||
98 | * @return static |
||
99 | */ |
||
100 | 3 | public function select(callable $callable) |
|
104 | |||
105 | /** |
||
106 | * @param callable $callable |
||
107 | * @return static |
||
108 | */ |
||
109 | 9 | public function reject(callable $callable) |
|
119 | |||
120 | /** |
||
121 | * Remove duplicate elements |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | 1 | public function distinct() |
|
129 | |||
130 | /** |
||
131 | * |
||
132 | * @param callable $callable |
||
133 | * @return static |
||
134 | */ |
||
135 | 2 | public function sort(callable $callable) |
|
141 | |||
142 | /** |
||
143 | * @param int $offset |
||
144 | * @param int $length |
||
145 | * @param bool $preserveKeys |
||
146 | * @return static |
||
147 | */ |
||
148 | 6 | public function slice($offset, $length = null, $preserveKeys = false) |
|
152 | |||
153 | /** |
||
154 | * @param int $length |
||
155 | * @param bool $preserveKeys |
||
156 | * @return static |
||
157 | */ |
||
158 | 2 | public function take($length, $preserveKeys = false) |
|
162 | |||
163 | /** |
||
164 | * @param Collection $collection |
||
165 | * @return static |
||
166 | */ |
||
167 | 2 | public function difference(self $collection) |
|
171 | |||
172 | /** |
||
173 | * @param Collection $collection |
||
174 | * @return static |
||
175 | */ |
||
176 | 2 | public function intersection(self $collection) |
|
180 | |||
181 | /** |
||
182 | * @param Collection ...$collections |
||
183 | * @return static |
||
184 | */ |
||
185 | 9 | public function merge(...$collections) |
|
192 | |||
193 | /** |
||
194 | * Group a collection using a \Closure |
||
195 | */ |
||
196 | 1 | public function groupBy(callable $groupBy, $preserveKeys = false) |
|
217 | |||
218 | /** |
||
219 | * Concatenates collections into a single collection |
||
220 | * |
||
221 | * @return static |
||
222 | */ |
||
223 | 4 | public function concat() |
|
235 | |||
236 | /** |
||
237 | * Map a function over a collection and flatten the result by one-level |
||
238 | * |
||
239 | * @param callable $callable |
||
240 | * @return static |
||
241 | */ |
||
242 | 3 | public function flatMap(callable $callable) |
|
246 | |||
247 | /** |
||
248 | * Alias for flatMap() |
||
249 | * |
||
250 | * @see flatMap |
||
251 | */ |
||
252 | 2 | public function mapcat(callable $callable) |
|
256 | |||
257 | /** |
||
258 | * Get all items but the first |
||
259 | * |
||
260 | * @return static |
||
261 | */ |
||
262 | 1 | public function tail() |
|
266 | |||
267 | |||
268 | /** |
||
269 | * Get the first item |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | 1 | public function head() |
|
278 | |||
279 | /** |
||
280 | * @param callable $callable |
||
281 | * @param mixed $initial |
||
282 | * @return mixed |
||
283 | */ |
||
284 | 14 | View Code Duplication | public function reduce(callable $callable, $initial) |
294 | |||
295 | /** |
||
296 | * Returns true if all items satisfy the callable condition |
||
297 | * |
||
298 | * @param callable $callable |
||
299 | */ |
||
300 | 2 | View Code Duplication | public function every(callable $callable) |
310 | |||
311 | /** |
||
312 | * Returns true if at least one item satisfies the callable condition |
||
313 | * |
||
314 | * @param callable $callable |
||
315 | */ |
||
316 | 5 | View Code Duplication | public function some(callable $callable) |
326 | |||
327 | /** |
||
328 | * Returns true no item satisfies the callable condition |
||
329 | * |
||
330 | * @param callable $callable |
||
331 | */ |
||
332 | 2 | public function none(callable $callable) |
|
336 | } |
||
337 |
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.