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:
Complex classes like AbstractProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class AbstractProvider implements ProviderInterface |
||
22 | { |
||
23 | use ErrorHandlerTrait; |
||
24 | |||
25 | const SUPPORT_EMBED = 'embed'; |
||
26 | const SUPPORT_IMAGE = 'image'; |
||
27 | const SUPPORT_DOWNLOAD = 'download'; |
||
28 | |||
29 | const TYPE_AUDIO = 'audio'; |
||
30 | const TYPE_IMAGE = 'image'; |
||
31 | const TYPE_FILE = 'file'; |
||
32 | const TYPE_VIDEO = 'video'; |
||
33 | |||
34 | /** |
||
35 | * @var Filesystem |
||
36 | */ |
||
37 | protected $filesystem; |
||
38 | |||
39 | /** |
||
40 | * @var TranslatorInterface |
||
41 | */ |
||
42 | private $translator; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $imageConstraintOptions = []; |
||
48 | |||
49 | /** |
||
50 | * @var HttpClientInterface |
||
51 | */ |
||
52 | private $httpClient; |
||
53 | |||
54 | /** |
||
55 | * @var FileLocator |
||
56 | */ |
||
57 | private $fileLocator; |
||
58 | |||
59 | /** |
||
60 | * @var AbstractMedia |
||
61 | */ |
||
62 | private $media; |
||
63 | |||
64 | /** |
||
65 | * @param Filesystem $filesystem |
||
66 | */ |
||
67 | 14 | public function setFilesystem(Filesystem $filesystem) |
|
68 | { |
||
69 | 14 | $this->filesystem = $filesystem; |
|
70 | 14 | } |
|
71 | |||
72 | /** |
||
73 | * @param TranslatorInterface $translator |
||
74 | */ |
||
75 | 13 | public function setTranslator(TranslatorInterface $translator) |
|
76 | { |
||
77 | 13 | $this->translator = $translator; |
|
78 | 13 | } |
|
79 | |||
80 | /** |
||
81 | * @return \Symfony\Component\Translation\TranslatorInterface |
||
82 | */ |
||
83 | 1 | public function getTranslator() |
|
84 | { |
||
85 | 1 | return $this->translator; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param array $options |
||
90 | */ |
||
91 | 13 | public function setImageConstraintOptions(array $options) |
|
92 | { |
||
93 | 13 | $this->imageConstraintOptions = $options; |
|
94 | 13 | } |
|
95 | |||
96 | /** |
||
97 | * @param HttpClientInterface $httpClient |
||
98 | */ |
||
99 | 13 | public function setHttpClient(HttpClientInterface $httpClient) |
|
100 | { |
||
101 | 13 | $this->httpClient = $httpClient; |
|
102 | 13 | } |
|
103 | |||
104 | /** |
||
105 | * @return FileLocator |
||
106 | */ |
||
107 | 2 | public function getFileLocator() |
|
108 | { |
||
109 | 2 | return $this->fileLocator; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param FileLocator $fileLocator |
||
114 | */ |
||
115 | 13 | public function setFileLocator(FileLocator $fileLocator) |
|
116 | { |
||
117 | 13 | $this->fileLocator = $fileLocator; |
|
118 | 13 | } |
|
119 | |||
120 | /** |
||
121 | * @return HttpClientInterface |
||
122 | */ |
||
123 | 4 | public function getHttpClient() |
|
124 | { |
||
125 | 4 | return $this->httpClient; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return Filesystem |
||
130 | */ |
||
131 | 8 | public function getFilesystem() |
|
132 | { |
||
133 | 8 | return $this->filesystem; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param AbstractMedia $media |
||
138 | * @return AbstractProvider |
||
139 | */ |
||
140 | 11 | public function setMedia($media) |
|
141 | { |
||
142 | 11 | $this->media = $media; |
|
143 | |||
144 | 11 | return $this; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param AbstractMedia $media |
||
149 | * @param $providerReferenceUpdated |
||
150 | */ |
||
151 | 7 | public function update(AbstractMedia $media, $providerReferenceUpdated) |
|
152 | { |
||
153 | 7 | $this->updateImage($media); |
|
154 | 7 | } |
|
155 | |||
156 | /** |
||
157 | * @param FormMapper $formMapper |
||
158 | */ |
||
159 | 11 | public function buildCreateForm(FormMapper $formMapper) |
|
166 | |||
167 | /** |
||
168 | * @param FormMapper $formMapper |
||
169 | */ |
||
170 | 7 | public function buildEditForm(FormMapper $formMapper) |
|
171 | { |
||
172 | $formMapper |
||
173 | 7 | ->tab('general') |
|
174 | 7 | ->add('provider', HiddenType::class); |
|
175 | |||
176 | 7 | $this->buildProviderEditFormBefore($formMapper); |
|
177 | |||
178 | 7 | $formMapper->add( |
|
179 | 7 | 'imageContent', |
|
180 | 7 | 'file', |
|
181 | [ |
||
182 | 7 | 'required' => false, |
|
183 | 'constraints' => [ |
||
184 | 7 | new Constraint\File(), |
|
185 | ], |
||
186 | 7 | 'label' => 'form.replacement_image', |
|
187 | ] |
||
188 | ) |
||
189 | 7 | ->add('title', TextType::class, ['label' => 'form.title']) |
|
190 | 7 | ->add( |
|
191 | 7 | 'description', |
|
192 | 7 | TextType::class, |
|
193 | 7 | ['label' => 'form.description', 'required' => false] |
|
194 | ) |
||
195 | 7 | ->add( |
|
196 | 7 | 'authorName', |
|
197 | 7 | TextType::class, |
|
198 | 7 | ['label' => 'form.authorName', 'required' => false] |
|
199 | ) |
||
200 | 7 | ->add( |
|
201 | 7 | 'copyright', |
|
202 | 7 | TextType::class, |
|
203 | 7 | ['label' => 'form.copyright', 'required' => false] |
|
204 | ) |
||
205 | 7 | ->end() |
|
206 | 7 | ->end() |
|
207 | 7 | ->tab('image') |
|
208 | 7 | ->add('focalPoint', MediaFocalPointType::class, [ |
|
209 | 7 | 'media' => $this->media |
|
210 | ]) |
||
211 | 7 | ->end() |
|
212 | 7 | ->end() |
|
213 | ; |
||
214 | |||
215 | 7 | $this->buildProviderEditFormAfter($formMapper); |
|
216 | 7 | } |
|
217 | |||
218 | /** |
||
219 | * @param FormMapper $formMapper |
||
220 | */ |
||
221 | public function buildProviderEditFormBefore(FormMapper $formMapper) |
||
224 | |||
225 | /** |
||
226 | * @param FormMapper $formMapper |
||
227 | */ |
||
228 | 6 | public function buildProviderEditFormAfter(FormMapper $formMapper) |
|
231 | |||
232 | /** |
||
233 | * @param AbstractMedia $media |
||
234 | * @param bool $useAsImage |
||
235 | * @return string|void |
||
236 | */ |
||
237 | 4 | protected function handleFileUpload(AbstractMedia $media, $useAsImage = false) |
|
275 | |||
276 | /** |
||
277 | * @param UploadedFile $file |
||
278 | * @return array |
||
279 | */ |
||
280 | 4 | protected function getFileMetaData(UploadedFile $file) |
|
281 | { |
||
282 | $fileData = [ |
||
283 | 4 | 'originalName' => $file->getClientOriginalName(), |
|
284 | 4 | 'originalExtension' => $file->getClientOriginalExtension(), |
|
285 | 4 | 'mimeType' => $file->getClientMimeType(), |
|
286 | 4 | 'size' => $file->getSize(), |
|
308 | |||
309 | /** |
||
310 | * @param AbstractMedia $media |
||
311 | */ |
||
312 | 7 | public function updateImage(AbstractMedia $media) |
|
328 | |||
329 | /** |
||
330 | * @param \Sonata\AdminBundle\Form\FormMapper $formMapper |
||
331 | * @param $name |
||
332 | * @param $label |
||
333 | * @param $required |
||
334 | * @param array $constraints |
||
335 | */ |
||
336 | 7 | protected function doAddFileField( |
|
366 | |||
367 | /** |
||
368 | * @param FormMapper $formMapper |
||
369 | * @param string $name |
||
370 | * @param string $label |
||
371 | * @param array $options |
||
372 | */ |
||
373 | 2 | View Code Duplication | public function addImageField( |
391 | |||
392 | /** |
||
393 | * @param FormMapper $formMapper |
||
394 | * @param $name |
||
395 | * @param $label |
||
396 | * @param array $options |
||
397 | */ |
||
398 | 3 | View Code Duplication | public function addRequiredImageField( |
416 | |||
417 | /** |
||
418 | * @param UploadedFile $file |
||
419 | * @return string |
||
420 | */ |
||
421 | 4 | protected function getFilenameByFile(UploadedFile $file) |
|
430 | |||
431 | /** |
||
432 | * @param UploadedFile $file |
||
433 | * @param $filename |
||
434 | * @throws FilesystemException |
||
435 | */ |
||
436 | 5 | protected function writeToFilesystem(UploadedFile $file, $filename) |
|
448 | |||
449 | /** |
||
450 | * @param $renderType |
||
451 | * @return boolean |
||
452 | */ |
||
453 | 11 | public function supports($renderType) |
|
467 | |||
468 | /** |
||
469 | * @param ErrorElement $errorElement |
||
470 | * @param AbstractMedia $media |
||
471 | */ |
||
472 | 7 | public function validate(ErrorElement $errorElement, AbstractMedia $media) |
|
475 | |||
476 | /** |
||
477 | * @return string |
||
478 | */ |
||
479 | 2 | public function getEmbedTemplate() |
|
486 | } |
||
487 |
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.