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 JsonStorage 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 JsonStorage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class JsonStorage implements Storage |
||
13 | { |
||
14 | /** |
||
15 | * @var String |
||
16 | */ |
||
17 | private $storageDir; |
||
18 | /** |
||
19 | * @var Repository |
||
20 | */ |
||
21 | private $repository; |
||
22 | |||
23 | /** |
||
24 | * JsonStorage constructor. |
||
25 | * |
||
26 | * @param string $storageDir |
||
27 | */ |
||
28 | public function __construct($storageDir) |
||
29 | { |
||
30 | $this->storageDir = $storageDir; |
||
31 | $this->config(); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Retrieve the data from the storagepath |
||
36 | * so it can be interacted with |
||
37 | * |
||
38 | * @throws \Exception |
||
39 | */ |
||
40 | private function config() |
||
41 | { |
||
42 | $storagePath = __DIR__ . '/../../' . $this->storageDir; |
||
43 | if (realpath($storagePath) === false) { |
||
44 | initFramework(); |
||
45 | if (Repository::create($storagePath)) { |
||
46 | $repository = new Repository($storagePath); |
||
47 | $repository->init(); |
||
48 | $this->repository = $repository; |
||
49 | } else { |
||
50 | throw new \Exception('Could not create repository directory: ' . $storagePath); |
||
51 | } |
||
52 | } else { |
||
53 | $this->repository = new Repository($storagePath); |
||
54 | } |
||
55 | |||
56 | } |
||
57 | |||
58 | |||
59 | |||
60 | /** |
||
61 | * Get user by username |
||
62 | * |
||
63 | * @param $username |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | View Code Duplication | public function getUserByUsername($username) |
|
81 | |||
82 | /** |
||
83 | * Get user by slug |
||
84 | * |
||
85 | * @param $slug |
||
86 | * @return array |
||
87 | */ |
||
88 | View Code Duplication | public function getUserBySlug($slug) |
|
102 | |||
103 | /** |
||
104 | * Get all users |
||
105 | * |
||
106 | * @return mixed |
||
107 | */ |
||
108 | public function getUsers() |
||
112 | |||
113 | /** |
||
114 | * Save user |
||
115 | * |
||
116 | * @param $slug |
||
117 | * @param $postValues |
||
118 | * @throws \Exception |
||
119 | */ |
||
120 | public function saveUser($slug, $postValues) |
||
139 | |||
140 | /** |
||
141 | * Add user |
||
142 | * |
||
143 | * @param $postValues |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | public function addUser($postValues) |
||
159 | |||
160 | /** |
||
161 | * Delete user by slug |
||
162 | * @param $slug |
||
163 | * @throws \Exception |
||
164 | */ |
||
165 | public function deleteUserBySlug($slug) |
||
180 | |||
181 | /** |
||
182 | * Create user from POST values |
||
183 | * @param $postValues |
||
184 | * @return \stdClass |
||
185 | * @throws \Exception |
||
186 | */ |
||
187 | private function createUserFromPostValues($postValues) |
||
212 | |||
213 | /* |
||
214 | * |
||
215 | * Documents |
||
216 | * |
||
217 | */ |
||
218 | /** |
||
219 | * Get documents |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getDocuments() |
||
227 | |||
228 | public function getTotalDocumentCount() |
||
232 | |||
233 | /** |
||
234 | * @param string $slug |
||
235 | * @return mixed |
||
236 | * @throws \Exception |
||
237 | */ |
||
238 | public function getDocumentBySlug($slug) |
||
243 | |||
244 | /** |
||
245 | * @param $postValues |
||
246 | */ |
||
247 | public function saveDocument($postValues) |
||
261 | |||
262 | View Code Duplication | public function addDocument($postValues) |
|
273 | |||
274 | public function deleteDocumentBySlug($slug) |
||
279 | |||
280 | /** |
||
281 | * Add new document in given path |
||
282 | * |
||
283 | * @param array $postValues |
||
284 | * |
||
285 | * @throws \Exception |
||
286 | */ |
||
287 | View Code Duplication | public function addDocumentFolder($postValues) |
|
297 | |||
298 | /** |
||
299 | * Delete a folder by its compound slug |
||
300 | * |
||
301 | * @param $slug |
||
302 | * |
||
303 | * @throws \Exception |
||
304 | */ |
||
305 | public function deleteDocumentFolderBySlug($slug) |
||
310 | |||
311 | /** |
||
312 | * Retrieve a folder by its compound slug |
||
313 | * |
||
314 | * @param $slug |
||
315 | * |
||
316 | * @return mixed |
||
317 | * @throws \Exception |
||
318 | */ |
||
319 | public function getDocumentFolderBySlug($slug) |
||
324 | |||
325 | /** |
||
326 | * Save changes to folder |
||
327 | * |
||
328 | * @param $postValues |
||
329 | * |
||
330 | * @throws \Exception |
||
331 | */ |
||
332 | public function saveDocumentFolder($postValues) |
||
336 | |||
337 | /** |
||
338 | * Convert path to indeces |
||
339 | * |
||
340 | * @param $path |
||
341 | * |
||
342 | * @return array |
||
343 | * @throws \Exception |
||
344 | */ |
||
345 | private function getDocumentContainerByPath($path) |
||
349 | |||
350 | /** |
||
351 | * Create folder from post values |
||
352 | * |
||
353 | * @param $postValues |
||
354 | * |
||
355 | * @return \stdClass |
||
356 | * @throws \Exception |
||
357 | */ |
||
358 | private function createDocumentFolderFromPostValues($postValues) |
||
372 | |||
373 | /* |
||
374 | * |
||
375 | * Sitemap |
||
376 | * |
||
377 | */ |
||
378 | /** |
||
379 | * @return array |
||
380 | */ |
||
381 | public function getSitemap() |
||
385 | |||
386 | /** |
||
387 | * Add a sitemap item |
||
388 | * |
||
389 | * @param $postValues |
||
390 | * |
||
391 | * @throws \Exception |
||
392 | */ |
||
393 | public function addSitemapItem($postValues) |
||
401 | |||
402 | /** |
||
403 | * Save changes to a sitemap item |
||
404 | * |
||
405 | * @param $slug |
||
406 | * @param $postValues |
||
407 | * |
||
408 | * @throws \Exception |
||
409 | */ |
||
410 | View Code Duplication | public function saveSitemapItem($slug, $postValues) |
|
423 | |||
424 | /** |
||
425 | * Delete a sitemap item by its slug |
||
426 | * |
||
427 | * @param $slug |
||
428 | * |
||
429 | * @throws \Exception |
||
430 | */ |
||
431 | View Code Duplication | public function deleteSitemapItemBySlug($slug) |
|
443 | |||
444 | /** |
||
445 | * Create a sitemap item from post values |
||
446 | * |
||
447 | * @param $postValues |
||
448 | * |
||
449 | * @return \stdClass |
||
450 | * @throws \Exception |
||
451 | */ |
||
452 | private function createSitemapItemFromPostValues($postValues) |
||
473 | |||
474 | /** |
||
475 | * Save changes to a sitemap item |
||
476 | * |
||
477 | * @param $postValues |
||
478 | * |
||
479 | * @throws \Exception |
||
480 | */ |
||
481 | public function saveSitemap($postValues) |
||
496 | |||
497 | /** |
||
498 | * Get a sitemap item by its slug |
||
499 | * |
||
500 | * @param $slug |
||
501 | * |
||
502 | * @return mixed |
||
503 | */ |
||
504 | public function getSitemapItemBySlug($slug) |
||
514 | |||
515 | /* |
||
516 | * |
||
517 | * Images |
||
518 | * |
||
519 | */ |
||
520 | /** |
||
521 | * Get all images |
||
522 | * |
||
523 | * @return array |
||
524 | */ |
||
525 | public function getImages() |
||
529 | |||
530 | public function addImage($postValues) |
||
560 | |||
561 | View Code Duplication | public function deleteImageByName($filename) |
|
584 | |||
585 | /** |
||
586 | * @param $filename |
||
587 | * @return null |
||
588 | */ |
||
589 | public function getImageByName($filename) |
||
599 | |||
600 | /* |
||
601 | * |
||
602 | * Files |
||
603 | * |
||
604 | */ |
||
605 | /** |
||
606 | * Get all files |
||
607 | * |
||
608 | * @return array |
||
609 | */ |
||
610 | public function getFiles() |
||
616 | |||
617 | /** |
||
618 | * @return string |
||
619 | */ |
||
620 | public function getStorageDir() |
||
624 | |||
625 | public function getContentDbHandle() |
||
629 | |||
630 | private function compareFiles($a, $b) |
||
634 | |||
635 | public function addFile($postValues) |
||
660 | |||
661 | private function validateFilename($filename, $path) |
||
689 | |||
690 | /** |
||
691 | * @param $filename |
||
692 | * @return null |
||
693 | */ |
||
694 | public function getFileByName($filename) |
||
704 | |||
705 | /** |
||
706 | * @param $filename |
||
707 | * @throws \Exception |
||
708 | */ |
||
709 | View Code Duplication | public function deleteFileByName($filename) |
|
728 | |||
729 | /* |
||
730 | * |
||
731 | * Configuration |
||
732 | * |
||
733 | */ |
||
734 | /** |
||
735 | * @return array |
||
736 | */ |
||
737 | public function getDocumentTypes() |
||
741 | |||
742 | /** |
||
743 | * Add a document type from post values |
||
744 | * |
||
745 | * @param $postValues |
||
746 | * |
||
747 | * @throws \Exception |
||
748 | */ |
||
749 | public function addDocumentType($postValues) |
||
759 | |||
760 | /** |
||
761 | * Create a document type from post values |
||
762 | * |
||
763 | * @param $postValues |
||
764 | * |
||
765 | * @return \stdClass |
||
766 | * @throws \Exception |
||
767 | */ |
||
768 | public function createDocumentTypeFromPostValues($postValues) |
||
805 | |||
806 | /** |
||
807 | * Delete document type |
||
808 | * |
||
809 | * @param $slug |
||
810 | * |
||
811 | * @throws \Exception |
||
812 | */ |
||
813 | View Code Duplication | public function deleteDocumentTypeBySlug($slug) |
|
825 | |||
826 | /** |
||
827 | * Get document type by its slug |
||
828 | * |
||
829 | * @param $slug |
||
830 | * @param bool $getBricks |
||
831 | * |
||
832 | * @return mixed |
||
833 | */ |
||
834 | public function getDocumentTypeBySlug($slug, $getBricks = false) |
||
854 | |||
855 | /** |
||
856 | * Save changes to a document type |
||
857 | * |
||
858 | * @param $slug |
||
859 | * @param $postValues |
||
860 | * |
||
861 | * @throws \Exception |
||
862 | */ |
||
863 | View Code Duplication | public function saveDocumentType($slug, $postValues) |
|
876 | |||
877 | /* |
||
878 | * |
||
879 | * Bricks |
||
880 | * |
||
881 | */ |
||
882 | /** |
||
883 | * @return array |
||
884 | */ |
||
885 | public function getBricks() |
||
889 | |||
890 | /** |
||
891 | * Add a brick |
||
892 | * |
||
893 | * @param $postValues |
||
894 | * |
||
895 | * @throws \Exception |
||
896 | */ |
||
897 | public function addBrick($postValues) |
||
907 | |||
908 | /** |
||
909 | * Create a brick from post values |
||
910 | * |
||
911 | * @param $postValues |
||
912 | * |
||
913 | * @return \stdClass |
||
914 | * @throws \Exception |
||
915 | */ |
||
916 | public function createBrickFromPostValues($postValues) |
||
940 | |||
941 | /** |
||
942 | * Get a brick by its slug |
||
943 | * |
||
944 | * @param $slug |
||
945 | * |
||
946 | * @return \stdClass |
||
947 | */ |
||
948 | public function getBrickBySlug($slug) |
||
958 | |||
959 | /** |
||
960 | * Save changes to a brick |
||
961 | * |
||
962 | * @param $slug |
||
963 | * @param $postValues |
||
964 | * |
||
965 | * @throws \Exception |
||
966 | */ |
||
967 | View Code Duplication | public function saveBrick($slug, $postValues) |
|
980 | |||
981 | /** |
||
982 | * Delete a brick by its slug |
||
983 | * |
||
984 | * @param $slug |
||
985 | * |
||
986 | * @throws \Exception |
||
987 | */ |
||
988 | View Code Duplication | public function deleteBrickBySlug($slug) |
|
1001 | |||
1002 | /* |
||
1003 | * |
||
1004 | * Misc |
||
1005 | * |
||
1006 | */ |
||
1007 | /** |
||
1008 | * Save changes made to the repository |
||
1009 | * |
||
1010 | * @throws \Exception |
||
1011 | */ |
||
1012 | private function save() { |
||
1015 | |||
1016 | /* |
||
1017 | * |
||
1018 | * Image Set |
||
1019 | * |
||
1020 | */ |
||
1021 | |||
1022 | /** |
||
1023 | * Get the image set |
||
1024 | * |
||
1025 | * @return array |
||
1026 | */ |
||
1027 | public function getImageSet() |
||
1031 | |||
1032 | /** |
||
1033 | * Get Image by slug |
||
1034 | * |
||
1035 | * @param $slug |
||
1036 | * |
||
1037 | * @return \stdClass |
||
1038 | */ |
||
1039 | public function getImageSetBySlug($slug) |
||
1049 | |||
1050 | /** |
||
1051 | * Save Image Set by it's slug |
||
1052 | * |
||
1053 | * @param $slug |
||
1054 | * @param $postValues |
||
1055 | * |
||
1056 | * @throws \Exception |
||
1057 | */ |
||
1058 | public function saveImageSet($slug, $postValues) |
||
1071 | |||
1072 | /** |
||
1073 | * Ceate image set from post values |
||
1074 | * |
||
1075 | * @param $postValues |
||
1076 | * |
||
1077 | * @return \stdClass |
||
1078 | * @throws \Exception |
||
1079 | */ |
||
1080 | private function createImageSetFromPostValues($postValues) |
||
1096 | |||
1097 | /** |
||
1098 | * Add image set |
||
1099 | * |
||
1100 | * @param $postValues |
||
1101 | * |
||
1102 | * @throws \Exception |
||
1103 | */ |
||
1104 | public function addImageSet($postValues) |
||
1114 | |||
1115 | /** |
||
1116 | * Delete Image Set by its slug |
||
1117 | * |
||
1118 | * @param $slug |
||
1119 | * |
||
1120 | * @throws \Exception |
||
1121 | */ |
||
1122 | public function deleteImageSetBySlug($slug) |
||
1135 | |||
1136 | /** |
||
1137 | * Get the image set with the smallest size |
||
1138 | * |
||
1139 | * @return \stdClass |
||
1140 | */ |
||
1141 | public function getSmallestImageSet() |
||
1163 | |||
1164 | /** |
||
1165 | * @return array |
||
1166 | */ |
||
1167 | public function getApplicationComponents() |
||
1171 | |||
1172 | public function addApplicationComponent($postValues) |
||
1181 | |||
1182 | private function createApplicationComponentFromPostValues($postValues) |
||
1200 | |||
1201 | public function getApplicationComponentBySlug($slug) |
||
1211 | |||
1212 | View Code Duplication | public function saveApplicationComponent($slug, $postValues) |
|
1225 | |||
1226 | View Code Duplication | public function deleteApplicationComponentBySlug($slug) |
|
1238 | |||
1239 | } |
||
1240 | } |
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.