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 |
||
18 | abstract class AbstractProvider implements ProviderInterface |
||
19 | { |
||
20 | const SUPPORT_EMBED = 'embed'; |
||
21 | const SUPPORT_IMAGE = 'image'; |
||
22 | const SUPPORT_DOWNLOAD = 'download'; |
||
23 | |||
24 | const TYPE_AUDIO = 'audio'; |
||
25 | const TYPE_IMAGE = 'image'; |
||
26 | const TYPE_FILE = 'file'; |
||
27 | const TYPE_VIDEO = 'video'; |
||
28 | |||
29 | /** |
||
30 | * @var Filesystem |
||
31 | */ |
||
32 | protected $filesystem; |
||
33 | |||
34 | /** |
||
35 | * @var TranslatorInterface |
||
36 | */ |
||
37 | private $translator; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $imageConstraintOptions = []; |
||
43 | |||
44 | /** |
||
45 | * @param Filesystem $filesystem |
||
46 | */ |
||
47 | public function setFilesystem(Filesystem $filesystem) |
||
51 | |||
52 | /** |
||
53 | * @param TranslatorInterface $translator |
||
54 | */ |
||
55 | public function setTranslator(TranslatorInterface $translator) |
||
59 | |||
60 | /** |
||
61 | * @return \Symfony\Component\Translation\TranslatorInterface |
||
62 | */ |
||
63 | public function getTranslator() |
||
67 | |||
68 | /** |
||
69 | * @param array $options |
||
70 | */ |
||
71 | public function setImageConstraintOptions(array $options) |
||
75 | |||
76 | /** |
||
77 | * @return Filesystem |
||
78 | */ |
||
79 | public function getFilesystem() |
||
83 | |||
84 | /** |
||
85 | * @param Media $media |
||
86 | * @param $providerReferenceUpdated |
||
87 | */ |
||
88 | public function update(Media $media, $providerReferenceUpdated) |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getTranslationDomain() |
||
100 | |||
101 | /** |
||
102 | * @param MediaInterface $media |
||
103 | * @param array $options |
||
104 | * @return array |
||
105 | */ |
||
106 | public function toArray(MediaInterface $media, array $options = []) |
||
117 | |||
118 | /** |
||
119 | * @return array |
||
120 | */ |
||
121 | protected function getFocalPoint() |
||
122 | { |
||
123 | return array_flip( |
||
124 | [ |
||
125 | 'top-left' => $this->translator->trans( |
||
126 | 'form.image_focal_point.top_left' |
||
127 | ), |
||
128 | 'top' => $this->translator->trans( |
||
129 | 'form.image_focal_point.top' |
||
130 | ), |
||
131 | 'top-right' => $this->translator->trans( |
||
132 | 'form.image_focal_point.top_right' |
||
133 | ), |
||
134 | 'left' => $this->translator->trans( |
||
135 | 'form.image_focal_point.left' |
||
136 | ), |
||
137 | 'center' => $this->translator->trans( |
||
138 | 'form.image_focal_point.center' |
||
139 | ), |
||
140 | 'right' => $this->translator->trans( |
||
141 | 'form.image_focal_point.right' |
||
142 | ), |
||
143 | 'bottom-left' => $this->translator->trans( |
||
144 | 'form.image_focal_point.bottom_left' |
||
145 | ), |
||
146 | 'bottom' => $this->translator->trans( |
||
147 | 'form.image_focal_point.bottom' |
||
148 | ), |
||
149 | 'bottom-right' => $this->translator->trans( |
||
150 | 'form.image_focal_point.bottom_right' |
||
151 | ), |
||
152 | ] |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param FormMapper $formMapper |
||
158 | */ |
||
159 | public function buildCreateForm(FormMapper $formMapper) |
||
166 | |||
167 | /** |
||
168 | * @param FormMapper $formMapper |
||
169 | */ |
||
170 | public function buildEditForm(FormMapper $formMapper) |
||
223 | |||
224 | /** |
||
225 | * @param FormMapper $formMapper |
||
226 | */ |
||
227 | public function buildProviderEditFormBefore(FormMapper $formMapper) |
||
230 | |||
231 | /** |
||
232 | * @param FormMapper $formMapper |
||
233 | */ |
||
234 | public function buildProviderEditFormAfter(FormMapper $formMapper) |
||
237 | |||
238 | /** |
||
239 | * @param Media $media |
||
240 | * @param bool $useAsImage |
||
241 | * @return string|void |
||
242 | */ |
||
243 | protected function handleFileUpload(Media $media, $useAsImage = false) |
||
281 | |||
282 | /** |
||
283 | * @param UploadedFile $file |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function getFileMetaData(UploadedFile $file) |
||
295 | |||
296 | /** |
||
297 | * @param Media $media |
||
298 | */ |
||
299 | public function updateImage(Media $media) |
||
315 | |||
316 | /** |
||
317 | * @param \Sonata\AdminBundle\Form\FormMapper $formMapper |
||
318 | * @param $name |
||
319 | * @param $label |
||
320 | * @param $required |
||
321 | * @param array $constraints |
||
322 | */ |
||
323 | protected function doAddFileField( |
||
353 | |||
354 | /** |
||
355 | * @param FormMapper $formMapper |
||
356 | * @param string $name |
||
357 | * @param string $label |
||
358 | * @param array $options |
||
359 | */ |
||
360 | View Code Duplication | public function addImageField( |
|
378 | |||
379 | /** |
||
380 | * @param FormMapper $formMapper |
||
381 | * @param $name |
||
382 | * @param $label |
||
383 | * @param array $options |
||
384 | */ |
||
385 | View Code Duplication | public function addRequiredImageField( |
|
403 | |||
404 | /** |
||
405 | * @param UploadedFile $file |
||
406 | * @return string |
||
407 | */ |
||
408 | protected function getFilenameByFile(UploadedFile $file) |
||
417 | |||
418 | /** |
||
419 | * @param UploadedFile $file |
||
420 | * @param $filename |
||
421 | * @throws \Exception |
||
422 | */ |
||
423 | protected function writeToFilesystem(UploadedFile $file, $filename) |
||
438 | |||
439 | /** |
||
440 | * @param $renderType |
||
441 | * @return boolean |
||
442 | */ |
||
443 | public function supports($renderType) |
||
457 | |||
458 | /** |
||
459 | * |
||
460 | */ |
||
461 | protected function disableErrorHandler() |
||
468 | |||
469 | /** |
||
470 | * |
||
471 | */ |
||
472 | protected function restoreErrorHandler() |
||
476 | |||
477 | /** |
||
478 | * @param ErrorElement $errorElement |
||
479 | * @param Media $media |
||
480 | */ |
||
481 | public function validate(ErrorElement $errorElement, Media $media) |
||
484 | |||
485 | /** |
||
486 | * @return string |
||
487 | */ |
||
488 | public function getEmbedTemplate() |
||
495 | |||
496 | /** |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getTitle() |
||
503 | } |
||
504 |
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.