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 |
||
22 | class Storage |
||
23 | { |
||
24 | /** |
||
25 | * @var SitemapStorage |
||
26 | */ |
||
27 | protected $sitemap; |
||
28 | /** |
||
29 | * @var ImagesStorage |
||
30 | */ |
||
31 | protected $images; |
||
32 | /** |
||
33 | * @var ImageSetStorage |
||
34 | */ |
||
35 | protected $imageSet; |
||
36 | /** |
||
37 | * @var FilesStorage |
||
38 | */ |
||
39 | protected $files; |
||
40 | /** |
||
41 | * @var UsersStorage |
||
42 | */ |
||
43 | protected $users; |
||
44 | /** |
||
45 | * @var DocumentTypesStorage |
||
46 | */ |
||
47 | protected $documentTypes; |
||
48 | /** |
||
49 | * @var BricksStorage |
||
50 | */ |
||
51 | protected $bricks; |
||
52 | /** |
||
53 | * @var ApplicationComponentsStorage |
||
54 | */ |
||
55 | protected $applicationComponents; |
||
56 | |||
57 | /** |
||
58 | * @var ValuelistsStorage |
||
59 | */ |
||
60 | protected $valuelists; |
||
61 | /** |
||
62 | * @var DocumentStorage |
||
63 | */ |
||
64 | protected $documents; |
||
65 | /** |
||
66 | * @var RedirectsStorage |
||
67 | */ |
||
68 | protected $redirects; |
||
69 | |||
70 | /** |
||
71 | * @var ActivityLogStorage |
||
72 | */ |
||
73 | protected $activityLog; |
||
74 | /** |
||
75 | * @var String |
||
76 | */ |
||
77 | protected $imagesDir; |
||
78 | /** |
||
79 | * @var String |
||
80 | */ |
||
81 | protected $filesDir; |
||
82 | |||
83 | /** |
||
84 | * @var String |
||
85 | */ |
||
86 | private $storageDir; |
||
87 | /** |
||
88 | * @var Repository |
||
89 | */ |
||
90 | private $repository; |
||
91 | |||
92 | /** |
||
93 | * JsonStorage constructor. |
||
94 | * |
||
95 | * @param string $storageDir |
||
96 | * @param $imagesDir |
||
97 | * @param $filesDir |
||
98 | */ |
||
99 | public function __construct($storageDir, $imagesDir, $filesDir) |
||
100 | { |
||
101 | $this->storageDir = $storageDir; |
||
102 | $this->imagesDir = $imagesDir; |
||
103 | $this->filesDir = $filesDir; |
||
104 | $this->config(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Retrieve the data from the storagepath |
||
109 | * so it can be interacted with |
||
110 | * |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | private function config() |
||
114 | { |
||
115 | $storagePath = $this->storageDir; |
||
116 | if (realpath($storagePath) === false) { |
||
117 | throw new \Exception('Storage doesnt seem to be initialized, consider running composer install to do so.'); |
||
118 | } else { |
||
119 | $this->repository = new Repository($storagePath); |
||
120 | } |
||
121 | |||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return \CloudControl\Cms\storage\storage\UsersStorage |
||
126 | */ |
||
127 | public function getUsers() |
||
134 | |||
135 | /** |
||
136 | * Get documents |
||
137 | * |
||
138 | * @return DocumentStorage |
||
139 | */ |
||
140 | public function getDocuments() |
||
147 | |||
148 | /** |
||
149 | * @return SitemapStorage |
||
150 | */ |
||
151 | public function getSitemap() |
||
158 | |||
159 | /** |
||
160 | * Get all images |
||
161 | * |
||
162 | * @return ImagesStorage |
||
163 | */ |
||
164 | public function getImages() |
||
172 | |||
173 | /** |
||
174 | * Get all files |
||
175 | * |
||
176 | * @return FilesStorage |
||
177 | */ |
||
178 | public function getFiles() |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getStorageDir() |
||
193 | |||
194 | /** |
||
195 | * @return \PDO |
||
196 | */ |
||
197 | public function getContentDbHandle() |
||
201 | |||
202 | /** |
||
203 | * @return DocumentTypesStorage |
||
204 | */ |
||
205 | public function getDocumentTypes() |
||
212 | |||
213 | /** |
||
214 | * @return BricksStorage |
||
215 | */ |
||
216 | View Code Duplication | public function getBricks() |
|
223 | |||
224 | /** |
||
225 | * Get the image set |
||
226 | * |
||
227 | * @return ImageSetStorage |
||
228 | */ |
||
229 | View Code Duplication | public function getImageSet() |
|
236 | |||
237 | /** |
||
238 | * @return ApplicationComponentsStorage |
||
239 | */ |
||
240 | public function getApplicationComponents() |
||
247 | |||
248 | /** |
||
249 | * @return \CloudControl\Cms\storage\Repository |
||
250 | */ |
||
251 | public function getRepository() |
||
255 | |||
256 | /** |
||
257 | * @return \CloudControl\Cms\storage\storage\ValuelistsStorage |
||
258 | */ |
||
259 | public function getValuelists() |
||
266 | |||
267 | /** |
||
268 | * @return \CloudControl\Cms\storage\storage\RedirectsStorage |
||
269 | */ |
||
270 | public function getRedirects() |
||
277 | |||
278 | /** |
||
279 | * @return ActivityLogStorage |
||
280 | */ |
||
281 | public function getActivityLog() |
||
288 | |||
289 | |||
290 | } |
||
291 | } |
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.