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 namespace Nord\Lumen\FileManager\Doctrine\ORM; |
||
8 | class File implements FileContract |
||
9 | { |
||
10 | /** |
||
11 | * @ORM\Id |
||
12 | * @ORM\GeneratedValue |
||
13 | * @ORM\Column(type="integer", name="id") |
||
14 | * |
||
15 | * @var int |
||
16 | */ |
||
17 | private $autoIncrementId; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $id; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $name; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $extension; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $path; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $mimeType; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | private $byteSize; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $data; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $disk; |
||
58 | |||
59 | /** |
||
60 | * @var Carbon |
||
61 | */ |
||
62 | private $savedAt; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * File constructor. |
||
67 | * |
||
68 | * @param string $id |
||
69 | * @param string $name |
||
70 | * @param string $extension |
||
71 | * @param string $path |
||
72 | * @param string $mimeType |
||
73 | * @param int $byteSize |
||
74 | * @param array $data |
||
75 | * @param string $disk |
||
76 | * @param Carbon $savedAt |
||
77 | */ |
||
78 | View Code Duplication | public function __construct( |
|
99 | |||
100 | /** |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getAutoIncrementId() |
||
107 | |||
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getId() |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getName() |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getExtension() |
||
133 | |||
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getMimeType() |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @return int |
||
146 | */ |
||
147 | public function getByteSize() |
||
151 | |||
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | public function getData() |
||
160 | |||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getDisk() |
||
169 | |||
170 | |||
171 | /** |
||
172 | * @return Carbon |
||
173 | */ |
||
174 | public function getSavedAt() |
||
178 | |||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getSavedAtAsTimestamp() |
||
187 | |||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getFilename() |
||
196 | |||
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | public function getFilePath() |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | public function getUrl(array $options = []) |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @param string $id |
||
218 | * |
||
219 | * @throws \Exception |
||
220 | */ |
||
221 | private function setId($id) |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param string $name |
||
233 | * |
||
234 | * @throws \Exception |
||
235 | */ |
||
236 | private function setName($name) |
||
244 | |||
245 | |||
246 | /** |
||
247 | * @param string $extension |
||
248 | * |
||
249 | * @throws \Exception |
||
250 | */ |
||
251 | private function setExtension($extension) |
||
259 | |||
260 | |||
261 | /** |
||
262 | * @param string $path |
||
263 | */ |
||
264 | private function setPath($path) |
||
268 | |||
269 | |||
270 | /** |
||
271 | * @param string $mimeType |
||
272 | */ |
||
273 | private function setMimeType($mimeType) |
||
277 | |||
278 | |||
279 | /** |
||
280 | * @param int $byteSize |
||
281 | */ |
||
282 | private function setByteSize($byteSize) |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @param array $data |
||
290 | */ |
||
291 | private function setData(array $data) |
||
295 | |||
296 | |||
297 | /** |
||
298 | * @param string $storage |
||
299 | */ |
||
300 | private function setDisk($storage) |
||
304 | |||
305 | |||
306 | /** |
||
307 | * @param Carbon $savedAt |
||
308 | */ |
||
309 | private function setSavedAt(Carbon $savedAt) |
||
313 | |||
314 | |||
315 | /** |
||
316 | * @return string |
||
317 | */ |
||
318 | private function getPath() |
||
322 | } |
||
323 |
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.