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:
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 UsersStorage |
||
39 | */ |
||
40 | protected $users; |
||
41 | /** |
||
42 | * @var DocumentTypesStorage |
||
43 | */ |
||
44 | protected $documentTypes; |
||
45 | /** |
||
46 | * @var BricksStorage |
||
47 | */ |
||
48 | protected $bricks; |
||
49 | /** |
||
50 | * @var ApplicationComponentsStorage |
||
51 | */ |
||
52 | protected $applicationComponents; |
||
53 | /** |
||
54 | * @var DocumentStorage |
||
55 | */ |
||
56 | protected $documents; |
||
57 | /** |
||
58 | * @var String |
||
59 | */ |
||
60 | private $storageDir; |
||
61 | /** |
||
62 | * @var Repository |
||
63 | */ |
||
64 | private $repository; |
||
65 | |||
66 | /** |
||
67 | * JsonStorage constructor. |
||
68 | * |
||
69 | * @param string $storageDir |
||
70 | */ |
||
71 | public function __construct($storageDir) |
||
76 | |||
77 | /** |
||
78 | * Retrieve the data from the storagepath |
||
79 | * so it can be interacted with |
||
80 | * |
||
81 | * @throws \Exception |
||
82 | */ |
||
83 | private function config() |
||
100 | |||
101 | /** |
||
102 | * @return \library\storage\storage\UsersStorage |
||
103 | */ |
||
104 | public function getUsers() |
||
111 | |||
112 | /** |
||
113 | * Get documents |
||
114 | * |
||
115 | * @return DocumentStorage |
||
116 | */ |
||
117 | public function getDocuments() |
||
124 | |||
125 | /** |
||
126 | * Add new document in given path |
||
127 | * |
||
128 | * @param array $postValues |
||
129 | * |
||
130 | * @throws \Exception |
||
131 | */ |
||
132 | View Code Duplication | public function addDocumentFolder($postValues) |
|
142 | |||
143 | /** |
||
144 | * Delete a folder by its compound slug |
||
145 | * |
||
146 | * @param $slug |
||
147 | * |
||
148 | * @throws \Exception |
||
149 | */ |
||
150 | public function deleteDocumentFolderBySlug($slug) |
||
155 | |||
156 | /** |
||
157 | * Retrieve a folder by its compound slug |
||
158 | * |
||
159 | * @param $slug |
||
160 | * |
||
161 | * @return mixed |
||
162 | * @throws \Exception |
||
163 | */ |
||
164 | public function getDocumentFolderBySlug($slug) |
||
170 | |||
171 | /** |
||
172 | * Save changes to folder |
||
173 | * |
||
174 | * @param $postValues |
||
175 | * |
||
176 | * @throws \Exception |
||
177 | */ |
||
178 | public function saveDocumentFolder($postValues) |
||
182 | |||
183 | /** |
||
184 | * @return SitemapStorage |
||
185 | */ |
||
186 | public function getSitemap() |
||
193 | |||
194 | /** |
||
195 | * Get all images |
||
196 | * |
||
197 | * @return ImagesStorage |
||
198 | */ |
||
199 | public function getImages() |
||
206 | |||
207 | /** |
||
208 | * Get all files |
||
209 | * |
||
210 | * @return FilesStorage |
||
211 | */ |
||
212 | public function getFiles() |
||
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getStorageDir() |
||
227 | |||
228 | /** |
||
229 | * @return \PDO |
||
230 | */ |
||
231 | public function getContentDbHandle() |
||
235 | |||
236 | /** |
||
237 | * @return DocumentTypesStorage |
||
238 | */ |
||
239 | public function getDocumentTypes() |
||
246 | |||
247 | /** |
||
248 | * @return BricksStorage |
||
249 | */ |
||
250 | View Code Duplication | public function getBricks() |
|
257 | |||
258 | /** |
||
259 | * Get the image set |
||
260 | * |
||
261 | * @return ImageSetStorage |
||
262 | */ |
||
263 | View Code Duplication | public function getImageSet() |
|
270 | |||
271 | /** |
||
272 | * @return ApplicationComponentsStorage |
||
273 | */ |
||
274 | public function getApplicationComponents() |
||
281 | } |
||
282 | } |
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.