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 Storage 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 Storage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Storage |
||
20 | { |
||
21 | /** |
||
22 | * @var SitemapStorage |
||
23 | */ |
||
24 | protected $sitemap; |
||
25 | /** |
||
26 | * @var ImagesStorage |
||
27 | */ |
||
28 | protected $images; |
||
29 | /** |
||
30 | * @var ImageSetStorage |
||
31 | */ |
||
32 | protected $imageSet; |
||
33 | /** |
||
34 | * @var FilesStorage |
||
35 | */ |
||
36 | protected $files; |
||
37 | /** |
||
38 | * @var String |
||
39 | */ |
||
40 | private $storageDir; |
||
41 | /** |
||
42 | * @var Repository |
||
43 | */ |
||
44 | private $repository; |
||
45 | |||
46 | /** |
||
47 | * JsonStorage constructor. |
||
48 | * |
||
49 | * @param string $storageDir |
||
50 | */ |
||
51 | public function __construct($storageDir) |
||
56 | |||
57 | /** |
||
58 | * Retrieve the data from the storagepath |
||
59 | * so it can be interacted with |
||
60 | * |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | private function config() |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Get user by username |
||
84 | * |
||
85 | * @param $username |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | View Code Duplication | public function getUserByUsername($username) |
|
103 | |||
104 | /** |
||
105 | * Get user by slug |
||
106 | * |
||
107 | * @param $slug |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | View Code Duplication | public function getUserBySlug($slug) |
|
125 | |||
126 | /** |
||
127 | * Get all users |
||
128 | * |
||
129 | * @return mixed |
||
130 | */ |
||
131 | public function getUsers() |
||
135 | |||
136 | /** |
||
137 | * Save user |
||
138 | * |
||
139 | * @param $slug |
||
140 | * @param $postValues |
||
141 | * |
||
142 | * @throws \Exception |
||
143 | */ |
||
144 | public function saveUser($slug, $postValues) |
||
163 | |||
164 | /** |
||
165 | * Add user |
||
166 | * |
||
167 | * @param $postValues |
||
168 | * |
||
169 | * @throws \Exception |
||
170 | */ |
||
171 | public function addUser($postValues) |
||
184 | |||
185 | /** |
||
186 | * Delete user by slug |
||
187 | * |
||
188 | * @param $slug |
||
189 | * |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | public function deleteUserBySlug($slug) |
||
207 | |||
208 | /* |
||
209 | * |
||
210 | * Documents |
||
211 | * |
||
212 | */ |
||
213 | /** |
||
214 | * Get documents |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getDocuments() |
||
222 | |||
223 | public function getTotalDocumentCount() |
||
227 | |||
228 | /** |
||
229 | * @param string $slug |
||
230 | * |
||
231 | * @return mixed |
||
232 | * @throws \Exception |
||
233 | */ |
||
234 | public function getDocumentBySlug($slug) |
||
240 | |||
241 | /** |
||
242 | * @param $postValues |
||
243 | */ |
||
244 | public function saveDocument($postValues) |
||
258 | |||
259 | View Code Duplication | public function addDocument($postValues) |
|
270 | |||
271 | public function deleteDocumentBySlug($slug) |
||
276 | |||
277 | /** |
||
278 | * Add new document in given path |
||
279 | * |
||
280 | * @param array $postValues |
||
281 | * |
||
282 | * @throws \Exception |
||
283 | */ |
||
284 | View Code Duplication | public function addDocumentFolder($postValues) |
|
294 | |||
295 | /** |
||
296 | * Delete a folder by its compound slug |
||
297 | * |
||
298 | * @param $slug |
||
299 | * |
||
300 | * @throws \Exception |
||
301 | */ |
||
302 | public function deleteDocumentFolderBySlug($slug) |
||
307 | |||
308 | /** |
||
309 | * Retrieve a folder by its compound slug |
||
310 | * |
||
311 | * @param $slug |
||
312 | * |
||
313 | * @return mixed |
||
314 | * @throws \Exception |
||
315 | */ |
||
316 | public function getDocumentFolderBySlug($slug) |
||
322 | |||
323 | /** |
||
324 | * Save changes to folder |
||
325 | * |
||
326 | * @param $postValues |
||
327 | * |
||
328 | * @throws \Exception |
||
329 | */ |
||
330 | public function saveDocumentFolder($postValues) |
||
334 | |||
335 | /** |
||
336 | * Convert path to indeces |
||
337 | * |
||
338 | * @param $path |
||
339 | * |
||
340 | * @return array |
||
341 | * @throws \Exception |
||
342 | */ |
||
343 | private function getDocumentContainerByPath($path) |
||
347 | |||
348 | /** |
||
349 | * @return SitemapStorage |
||
350 | */ |
||
351 | public function getSitemap() |
||
358 | |||
359 | /* |
||
360 | * |
||
361 | * Images |
||
362 | * |
||
363 | */ |
||
364 | /** |
||
365 | * Get all images |
||
366 | * |
||
367 | * @return ImagesStorage |
||
368 | */ |
||
369 | public function getImages() |
||
376 | |||
377 | /* |
||
378 | * |
||
379 | * Files |
||
380 | * |
||
381 | */ |
||
382 | /** |
||
383 | * Get all files |
||
384 | * |
||
385 | * @return FilesStorage |
||
386 | */ |
||
387 | public function getFiles() |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getStorageDir() |
||
402 | |||
403 | public function getContentDbHandle() |
||
407 | |||
408 | /* |
||
409 | * |
||
410 | * Configuration |
||
411 | * |
||
412 | */ |
||
413 | /** |
||
414 | * @return array |
||
415 | */ |
||
416 | public function getDocumentTypes() |
||
420 | |||
421 | /** |
||
422 | * Add a document type from post values |
||
423 | * |
||
424 | * @param $postValues |
||
425 | * |
||
426 | * @throws \Exception |
||
427 | */ |
||
428 | public function addDocumentType($postValues) |
||
438 | |||
439 | /** |
||
440 | * Delete document type |
||
441 | * |
||
442 | * @param $slug |
||
443 | * |
||
444 | * @throws \Exception |
||
445 | */ |
||
446 | View Code Duplication | public function deleteDocumentTypeBySlug($slug) |
|
458 | |||
459 | /** |
||
460 | * Get document type by its slug |
||
461 | * |
||
462 | * @param $slug |
||
463 | * @param bool $getBricks |
||
464 | * |
||
465 | * @return mixed |
||
466 | */ |
||
467 | public function getDocumentTypeBySlug($slug, $getBricks = false) |
||
489 | |||
490 | /** |
||
491 | * Save changes to a document type |
||
492 | * |
||
493 | * @param $slug |
||
494 | * @param $postValues |
||
495 | * |
||
496 | * @throws \Exception |
||
497 | */ |
||
498 | View Code Duplication | public function saveDocumentType($slug, $postValues) |
|
511 | |||
512 | /* |
||
513 | * |
||
514 | * Bricks |
||
515 | * |
||
516 | */ |
||
517 | /** |
||
518 | * @return array |
||
519 | */ |
||
520 | public function getBricks() |
||
524 | |||
525 | /** |
||
526 | * Add a brick |
||
527 | * |
||
528 | * @param $postValues |
||
529 | * |
||
530 | * @throws \Exception |
||
531 | */ |
||
532 | public function addBrick($postValues) |
||
542 | |||
543 | /** |
||
544 | * Get a brick by its slug |
||
545 | * |
||
546 | * @param $slug |
||
547 | * |
||
548 | * @return \stdClass |
||
549 | */ |
||
550 | public function getBrickBySlug($slug) |
||
561 | |||
562 | /** |
||
563 | * Save changes to a brick |
||
564 | * |
||
565 | * @param $slug |
||
566 | * @param $postValues |
||
567 | * |
||
568 | * @throws \Exception |
||
569 | */ |
||
570 | public function saveBrick($slug, $postValues) |
||
583 | |||
584 | /** |
||
585 | * Delete a brick by its slug |
||
586 | * |
||
587 | * @param $slug |
||
588 | * |
||
589 | * @throws \Exception |
||
590 | */ |
||
591 | public function deleteBrickBySlug($slug) |
||
604 | |||
605 | /* |
||
606 | * |
||
607 | * Misc |
||
608 | * |
||
609 | */ |
||
610 | /** |
||
611 | * Save changes made to the repository |
||
612 | * |
||
613 | * @throws \Exception |
||
614 | */ |
||
615 | private function save() |
||
619 | |||
620 | /* |
||
621 | * |
||
622 | * Image Set |
||
623 | * |
||
624 | */ |
||
625 | |||
626 | /** |
||
627 | * Get the image set |
||
628 | * |
||
629 | * @return ImageSetStorage |
||
630 | */ |
||
631 | View Code Duplication | public function getImageSet() |
|
638 | |||
639 | /** |
||
640 | * @return array |
||
641 | */ |
||
642 | public function getApplicationComponents() |
||
646 | |||
647 | public function addApplicationComponent($postValues) |
||
656 | |||
657 | public function getApplicationComponentBySlug($slug) |
||
668 | |||
669 | public function saveApplicationComponent($slug, $postValues) |
||
682 | |||
683 | public function deleteApplicationComponentBySlug($slug) |
||
695 | |||
696 | } |
||
697 | } |
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.