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 ManagesItemsTrait 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 ManagesItemsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | trait ManagesItemsTrait |
||
17 | { |
||
18 | /** |
||
19 | * The items stored in the manager |
||
20 | * @var array $items Items governed by manager |
||
21 | */ |
||
22 | protected $_items = []; |
||
23 | |||
24 | /** |
||
25 | * Name of the property to hold the data items. Internal use only |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $nameOfItemsRepository = '_items'; |
||
29 | |||
30 | /** @var array Array of protected aliases */ |
||
31 | protected $protectedItems = []; |
||
32 | |||
33 | /* The user may also set $dataItemsName */ |
||
34 | |||
35 | /** |
||
36 | * Initializes a new manager instance. |
||
37 | * |
||
38 | * This is useful for implementations that have their own __construct method |
||
39 | * This is an alias for reset() |
||
40 | * |
||
41 | * @param array $items |
||
42 | * @return $this |
||
43 | */ |
||
44 | public function initManager($items = null) |
||
64 | |||
65 | /** |
||
66 | * Hydrate with external data, optionally append |
||
67 | * |
||
68 | * @param $data array The data to be hydrated into the manager |
||
69 | * @param bool $append When true, data will be appended to the current set |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function hydrate($data, $append = false) |
||
82 | |||
83 | /** |
||
84 | * Adds a single item. |
||
85 | * |
||
86 | * Allow for dot notation (one.two.three) and item nesting. |
||
87 | * |
||
88 | * @param string $alias Key to be stored |
||
89 | * @param mixed $item Value to be stored |
||
90 | * @param array $options THIS IS NOT USED HERE |
||
91 | * @return $this |
||
|
|||
92 | */ |
||
93 | public function add($alias, $item = null, array $options = null) |
||
97 | |||
98 | /** |
||
99 | * Adds a single item. |
||
100 | * |
||
101 | * Allow for dot notation (one.two.three) and item nesting. |
||
102 | * |
||
103 | * @param string $alias Key to be stored |
||
104 | * @param mixed $item Value to be stored |
||
105 | * @param array $options THIS IS NOT USED HERE |
||
106 | * @return $this |
||
107 | */ |
||
108 | protected function addItem($alias, $item = null, array $options = null) |
||
146 | |||
147 | /** |
||
148 | * Updates an item |
||
149 | * |
||
150 | * @param string $alias |
||
151 | * @param null $item |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function set($alias, $item = null) |
||
159 | |||
160 | /** |
||
161 | * Push a value or values onto the end of an array inside manager |
||
162 | * @param string $alias The level of nested data |
||
163 | * @param mixed $value The first value to append |
||
164 | * @param null|mixed $other Optional other values to apend |
||
165 | * @return int Number of items pushed |
||
166 | * @throws ItemNotFoundException If pushing to unset array |
||
167 | */ |
||
168 | public function push($alias, $value, $other = null) |
||
190 | |||
191 | /** |
||
192 | * Get a single item |
||
193 | * |
||
194 | * Note: When editing, update ManagesIocTrait::getRaw() |
||
195 | * |
||
196 | * @param string $alias |
||
197 | * @param string $fallback Defaults to '_michaels_no_fallback' so null can be a fallback |
||
198 | * @throws \Michaels\Manager\Exceptions\ItemNotFoundException If item not found |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function get($alias, $fallback = '_michaels_no_fallback') |
||
205 | |||
206 | /** |
||
207 | * Get a single item |
||
208 | * |
||
209 | * @param string $alias |
||
210 | * @param string $fallback Defaults to '_michaels_no_fallback' so null can be a fallback |
||
211 | * @throws \Michaels\Manager\Exceptions\ItemNotFoundException If item not found |
||
212 | * @return mixed |
||
213 | */ |
||
214 | public function getRaw($alias, $fallback = '_michaels_no_fallback') |
||
229 | |||
230 | /** |
||
231 | * Return an item if it exist |
||
232 | * @param $alias |
||
233 | * @return NoItemFoundMessage |
||
234 | */ |
||
235 | public function getIfExists($alias) |
||
248 | |||
249 | /** |
||
250 | * Return an item if it exist |
||
251 | * Alias of getIfExists() |
||
252 | * |
||
253 | * @param $alias |
||
254 | * @return NoItemFoundMessage |
||
255 | */ |
||
256 | public function getIfHas($alias) |
||
260 | |||
261 | /** |
||
262 | * Return all items as array |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | public function getAll() |
||
271 | |||
272 | /** |
||
273 | * Return all items as array |
||
274 | * Alias of getAll() |
||
275 | * @return array |
||
276 | */ |
||
277 | public function all() |
||
281 | |||
282 | /** |
||
283 | * Confirm or deny that an item exists |
||
284 | * |
||
285 | * @param $alias |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function exists($alias) |
||
301 | |||
302 | /** |
||
303 | * Confirm or deny that an item exists |
||
304 | * Alias of exists() |
||
305 | * |
||
306 | * @param $alias |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function has($alias) |
||
313 | |||
314 | |||
315 | /** |
||
316 | * Confirm that manager has no items |
||
317 | * @return boolean |
||
318 | */ |
||
319 | public function isEmpty() |
||
324 | |||
325 | /** |
||
326 | * Deletes an item |
||
327 | * |
||
328 | * @param $alias |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function remove($alias) |
||
347 | |||
348 | /** |
||
349 | * Clear the manager |
||
350 | * @return $this |
||
351 | */ |
||
352 | public function clear() |
||
358 | |||
359 | /** |
||
360 | * Reset the manager with an array of items |
||
361 | * Alias of initManager() |
||
362 | * |
||
363 | * @param array $items |
||
364 | * @return mixed |
||
365 | */ |
||
366 | public function reset($items) |
||
370 | |||
371 | /** |
||
372 | * Guard an alias from being modified |
||
373 | * @param $item |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function protect($item) |
||
381 | |||
382 | /** |
||
383 | * Merge a set of defaults with the current items |
||
384 | * @param array $defaults |
||
385 | * @return $this |
||
386 | */ |
||
387 | public function loadDefaults(array $defaults) |
||
392 | |||
393 | /** |
||
394 | * Returns the name of the property that holds data items |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getItemsName() |
||
401 | |||
402 | /** |
||
403 | * Sets the name of the property that holds data items |
||
404 | * @param $nameOfItemsRepository |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function setItemsName($nameOfItemsRepository) |
||
412 | |||
413 | /** |
||
414 | * Get the collection of items as JSON. |
||
415 | * |
||
416 | * @param int $options |
||
417 | * @return string |
||
418 | */ |
||
419 | public function toJson($options = 0) |
||
423 | |||
424 | /** |
||
425 | * When manager instance is used as a string, return json of items |
||
426 | * @return string |
||
427 | */ |
||
428 | public function __toString() |
||
432 | |||
433 | /** |
||
434 | * Prepare the returned value |
||
435 | * @param $value |
||
436 | * @return mixed |
||
437 | */ |
||
438 | protected function prepareReturnedValue($value) |
||
448 | |||
449 | /** |
||
450 | * Recursively merge defaults array and items array |
||
451 | * @param array $defaults |
||
452 | * @param string $level |
||
453 | */ |
||
454 | protected function mergeDefaults(array $defaults, $level = '') |
||
471 | |||
472 | /** |
||
473 | * Cycle through the nests to see if an item is protected |
||
474 | * @param $item |
||
475 | */ |
||
476 | protected function checkIfProtected($item) |
||
491 | |||
492 | /** |
||
493 | * Throws an exception if item is protected |
||
494 | * @param $item |
||
495 | */ |
||
496 | protected function performProtectedCheck($item) |
||
502 | } |
||
503 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.