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 |
||
43 | class Exif implements ExifInterface, JsonSerializable |
||
44 | { |
||
45 | /** |
||
46 | * @var Aperture |
||
47 | */ |
||
48 | protected $aperture; |
||
49 | |||
50 | /** |
||
51 | * @var Author |
||
52 | */ |
||
53 | protected $author; |
||
54 | |||
55 | /** |
||
56 | * @var Coordinates |
||
57 | */ |
||
58 | protected $coordinates; |
||
59 | |||
60 | /** |
||
61 | * @var DateTimeImmutable |
||
62 | */ |
||
63 | protected $creationDate; |
||
64 | |||
65 | /** |
||
66 | * @var Dimensions |
||
67 | */ |
||
68 | protected $dimensions; |
||
69 | |||
70 | /** |
||
71 | * @var ExposureTime |
||
72 | */ |
||
73 | protected $exposureTime; |
||
74 | |||
75 | /** |
||
76 | * @var Filename |
||
77 | */ |
||
78 | protected $filename; |
||
79 | |||
80 | /** |
||
81 | * @var Filesize |
||
82 | */ |
||
83 | protected $filesize; |
||
84 | |||
85 | /** |
||
86 | * @var FocalLength |
||
87 | */ |
||
88 | protected $focalLength; |
||
89 | |||
90 | /** |
||
91 | * @var FocusDistance |
||
92 | */ |
||
93 | protected $focusDistance; |
||
94 | |||
95 | /** |
||
96 | * @var IsoSpeed |
||
97 | */ |
||
98 | protected $isoSpeed; |
||
99 | |||
100 | /** |
||
101 | * @var Make |
||
102 | */ |
||
103 | protected $make; |
||
104 | |||
105 | /** |
||
106 | * @var Model |
||
107 | */ |
||
108 | protected $model; |
||
109 | |||
110 | /** |
||
111 | * @var MimeType |
||
112 | */ |
||
113 | protected $mimeType; |
||
114 | |||
115 | /** |
||
116 | * @var Resolution |
||
117 | */ |
||
118 | protected $resolution; |
||
119 | |||
120 | /** |
||
121 | * @var Software |
||
122 | */ |
||
123 | protected $software; |
||
124 | |||
125 | /** |
||
126 | * {@inheritDoc} |
||
127 | */ |
||
128 | public function getAperture() |
||
132 | |||
133 | /** |
||
134 | * {@inheritDoc} |
||
135 | */ |
||
136 | public function withAperture(Aperture $aperture) |
||
143 | |||
144 | /** |
||
145 | * {@inheritDoc} |
||
146 | */ |
||
147 | public function getMimeType() |
||
151 | |||
152 | /** |
||
153 | * {@inheritDoc} |
||
154 | */ |
||
155 | public function withMimeType(MimeType $mimeType) |
||
162 | |||
163 | /** |
||
164 | * {@inheritDoc} |
||
165 | */ |
||
166 | public function getFilename() |
||
170 | |||
171 | /** |
||
172 | * {@inheritDoc} |
||
173 | */ |
||
174 | public function withFilename(Filename $filename) |
||
181 | |||
182 | /** |
||
183 | * {@inheritDoc} |
||
184 | */ |
||
185 | public function getFilesize() |
||
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | */ |
||
193 | public function withFilesize(Filesize $filesize) |
||
200 | |||
201 | /** |
||
202 | * {@inheritDoc} |
||
203 | */ |
||
204 | public function getMake() |
||
208 | |||
209 | /** |
||
210 | * {@inheritDoc} |
||
211 | */ |
||
212 | public function withMake(Make $make) |
||
219 | |||
220 | /** |
||
221 | * {@inheritDoc} |
||
222 | */ |
||
223 | public function getModel() |
||
227 | |||
228 | /** |
||
229 | * {@inheritDoc} |
||
230 | */ |
||
231 | public function withModel(Model $model) |
||
238 | |||
239 | /** |
||
240 | * {@inheritDoc} |
||
241 | */ |
||
242 | public function getSoftware() |
||
246 | |||
247 | /** |
||
248 | * {@inheritDoc} |
||
249 | */ |
||
250 | public function withSoftware(Software $software) |
||
257 | |||
258 | /** |
||
259 | * {@inheritDoc} |
||
260 | */ |
||
261 | public function getAuthor() |
||
265 | |||
266 | /** |
||
267 | * {@inheritDoc} |
||
268 | */ |
||
269 | public function withAuthor(Author $author) |
||
276 | |||
277 | /** |
||
278 | * {@inheritDoc} |
||
279 | */ |
||
280 | public function getDimensions() |
||
284 | |||
285 | /** |
||
286 | * {@inheritDoc} |
||
287 | */ |
||
288 | public function withDimensions(Dimensions $dimensions) |
||
295 | |||
296 | /** |
||
297 | * {@inheritDoc} |
||
298 | */ |
||
299 | public function getFocalLength() |
||
303 | |||
304 | /** |
||
305 | * {@inheritDoc} |
||
306 | */ |
||
307 | public function withFocalLength(FocalLength $focalLength) |
||
314 | |||
315 | /** |
||
316 | * {@inheritDoc} |
||
317 | */ |
||
318 | public function getFocusDistance() |
||
322 | |||
323 | /** |
||
324 | * {@inheritDoc} |
||
325 | */ |
||
326 | public function withFocusDistance(FocusDistance $focusDistance) |
||
333 | |||
334 | /** |
||
335 | * {@inheritDoc} |
||
336 | */ |
||
337 | public function getExposureTime() |
||
341 | |||
342 | /** |
||
343 | * {@inheritDoc} |
||
344 | */ |
||
345 | public function withExposureTime(ExposureTime $exposureTime) |
||
352 | |||
353 | /** |
||
354 | * {@inheritDoc} |
||
355 | */ |
||
356 | public function getIsoSpeed() |
||
360 | |||
361 | /** |
||
362 | * {@inheritDoc} |
||
363 | */ |
||
364 | public function withIsoSpeed(IsoSpeed $isoSpeed) |
||
371 | |||
372 | /** |
||
373 | * {@inheritDoc} |
||
374 | */ |
||
375 | public function getCreationDate() |
||
379 | |||
380 | /** |
||
381 | * {@inheritDoc} |
||
382 | */ |
||
383 | public function withCreationDate(DateTimeImmutable $date) |
||
390 | |||
391 | /** |
||
392 | * {@inheritDoc} |
||
393 | */ |
||
394 | public function getResolution() |
||
398 | |||
399 | /** |
||
400 | * {@inheritDoc} |
||
401 | */ |
||
402 | public function withResolution(Resolution $resolution) |
||
409 | |||
410 | /** |
||
411 | * {@inheritDoc} |
||
412 | */ |
||
413 | public function getCoordinates() |
||
417 | |||
418 | /** |
||
419 | * {@inheritDoc} |
||
420 | */ |
||
421 | public function withCoordinates(Coordinates $coordinates) |
||
428 | |||
429 | /** |
||
430 | * @inheritDoc |
||
431 | * |
||
432 | * @return array |
||
433 | */ |
||
434 | View Code Duplication | public function jsonSerialize() |
|
448 | } |
||
449 |
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.