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 AddonCollection 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 AddonCollection, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Anomaly\Streams\Platform\Addon; |
||
7 | class AddonCollection extends Collection |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * Create a new AddonCollection instance. |
||
12 | * |
||
13 | * @param array $items |
||
14 | */ |
||
15 | public function __construct($items = []) |
||
26 | |||
27 | /** |
||
28 | * Return all addon namespaces. |
||
29 | * |
||
30 | * @param null $key |
||
31 | * @return array |
||
32 | */ |
||
33 | public function namespaces($key = null) |
||
43 | |||
44 | /** |
||
45 | * Return only core addons. |
||
46 | * |
||
47 | * @return AddonCollection |
||
48 | */ |
||
49 | public function core() |
||
62 | |||
63 | /** |
||
64 | * Return only testing addons. |
||
65 | * |
||
66 | * @return AddonCollection |
||
67 | */ |
||
68 | public function testing() |
||
81 | |||
82 | /** |
||
83 | * Get an addon. |
||
84 | * |
||
85 | * @param mixed $key |
||
86 | * @param null $default |
||
87 | * @return Addon|mixed|null |
||
88 | */ |
||
89 | public function get($key, $default = null) |
||
97 | |||
98 | /** |
||
99 | * Find an addon by it's slug. |
||
100 | * |
||
101 | * @param $slug |
||
102 | * |
||
103 | * @return null|Addon |
||
104 | */ |
||
105 | public function findBySlug($slug) |
||
116 | |||
117 | /** |
||
118 | * Order addon's by their slug. |
||
119 | * |
||
120 | * @param string $direction |
||
121 | * |
||
122 | * @return AddonCollection |
||
123 | */ |
||
124 | public function orderBySlug($direction = 'asc') |
||
141 | |||
142 | /** |
||
143 | * Disperse addons to their |
||
144 | * respective collections. |
||
145 | */ |
||
146 | public function disperse() |
||
163 | |||
164 | /** |
||
165 | * Fire the registered |
||
166 | * method on all addons. |
||
167 | */ |
||
168 | public function registered() |
||
176 | |||
177 | /** |
||
178 | * Return addons only with the provided configuration. |
||
179 | * |
||
180 | * @param $key |
||
181 | * @return AddonCollection |
||
182 | */ |
||
183 | public function withConfig($key) |
||
196 | |||
197 | /** |
||
198 | * Return addons only with any of |
||
199 | * the provided configuration. |
||
200 | * |
||
201 | * @param array $keys |
||
202 | * @return AddonCollection |
||
203 | */ |
||
204 | public function withAnyConfig(array $keys) |
||
217 | |||
218 | /** |
||
219 | * Sort through each item with a callback. |
||
220 | * |
||
221 | * @param callable|null $callback |
||
222 | * @return static |
||
223 | */ |
||
224 | public function sort(callable $callback = null) |
||
236 | |||
237 | /** |
||
238 | * Return only installable addons. |
||
239 | * |
||
240 | * @return AddonCollection |
||
241 | */ |
||
242 | public function installable() |
||
250 | |||
251 | /** |
||
252 | * Return enabled addons. |
||
253 | * |
||
254 | * @return AddonCollection |
||
255 | */ |
||
256 | public function enabled() |
||
266 | |||
267 | /** |
||
268 | * Return installed addons. |
||
269 | * |
||
270 | * @return AddonCollection |
||
271 | */ |
||
272 | public function installed() |
||
282 | |||
283 | /** |
||
284 | * Return uninstalled addons. |
||
285 | * |
||
286 | * @return AddonCollection |
||
287 | */ |
||
288 | public function uninstalled() |
||
298 | |||
299 | /** |
||
300 | * Call a method. |
||
301 | * |
||
302 | * @param $method |
||
303 | * @param $arguments |
||
304 | * @return AddonCollection |
||
305 | */ |
||
306 | View Code Duplication | public function __call($method, $arguments) |
|
316 | |||
317 | /** |
||
318 | * Get a property. |
||
319 | * |
||
320 | * @param $name |
||
321 | * @return AddonCollection |
||
322 | */ |
||
323 | View Code Duplication | public function __get($name) |
|
333 | } |
||
334 |
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.