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 |
||
11 | class FileProvider extends AbstractProvider |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $fileConstraintOptions = []; |
||
17 | |||
18 | /** |
||
19 | * @param array $fileConstraintOptions |
||
20 | */ |
||
21 | 8 | public function __construct(array $fileConstraintOptions = []) |
|
25 | |||
26 | /** |
||
27 | * @param FormMapper $formMapper |
||
28 | */ |
||
29 | 3 | public function buildProviderCreateForm(FormMapper $formMapper) |
|
30 | { |
||
31 | 3 | $this->addRequiredFileField($formMapper, 'binaryContent', 'file'); |
|
32 | 3 | } |
|
33 | |||
34 | /** |
||
35 | * @param FormMapper $formMapper |
||
36 | */ |
||
37 | 2 | public function buildProviderEditFormBefore(FormMapper $formMapper) |
|
38 | { |
||
39 | 2 | $this->addFileField($formMapper, 'binaryContent', 'file'); |
|
40 | 2 | } |
|
41 | |||
42 | /** |
||
43 | * @param AbstractMedia $media |
||
44 | * @param bool $providerReferenceUpdated |
||
45 | */ |
||
46 | 2 | public function update(AbstractMedia $media, $providerReferenceUpdated) |
|
47 | { |
||
48 | 2 | if (!is_null($media->getBinaryContent())) { |
|
49 | 2 | if (empty($media->getImage())) { |
|
50 | 2 | $this->setFileImage($media); |
|
51 | } |
||
52 | 2 | $filename = $this->handleFileUpload($media); |
|
53 | 2 | if (!empty($filename)) { |
|
54 | 2 | $media->setProviderReference($filename); |
|
55 | } |
||
56 | } |
||
57 | |||
58 | 2 | parent::update($media, $providerReferenceUpdated); |
|
59 | 2 | } |
|
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | 8 | public function getIcon() |
|
65 | { |
||
66 | 8 | return 'fa fa-file'; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | 8 | public function getName() |
|
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | 3 | public function getType() |
|
84 | |||
85 | /** |
||
86 | * @param AbstractMedia $media |
||
87 | */ |
||
88 | 2 | protected function setFileImage(AbstractMedia $media) |
|
89 | { |
||
90 | /** |
||
91 | * @var UploadedFile $file |
||
92 | */ |
||
93 | 2 | $file = $media->getBinaryContent(); |
|
94 | 2 | if (empty($file)) { |
|
95 | return; |
||
96 | } |
||
97 | |||
98 | 2 | $imageFilename = $this->getImageByExtension($file->getClientOriginalExtension()); |
|
99 | 2 | $media->setImageContent( |
|
100 | 2 | new UploadedFile( |
|
101 | 2 | $this->getImageLocation().$imageFilename, |
|
102 | 2 | $imageFilename |
|
103 | ) |
||
104 | ); |
||
105 | 2 | } |
|
106 | |||
107 | /** |
||
108 | * @param FormMapper $formMapper |
||
109 | * @param string $name |
||
110 | * @param string $label |
||
111 | * @param array $options |
||
112 | */ |
||
113 | 2 | View Code Duplication | public function addFileField(FormMapper $formMapper, $name, $label, $options = []) |
|
|||
114 | { |
||
115 | 2 | $this->doAddFileField( |
|
116 | $formMapper, |
||
117 | $name, |
||
118 | $label, |
||
119 | 2 | false, |
|
120 | [ |
||
121 | 2 | new Constraint\File($this->getFileConstraintOptions($options)), |
|
122 | 2 | new Constraint\Callback([$this, 'validateExtension']), |
|
123 | ] |
||
124 | ); |
||
125 | 2 | } |
|
126 | |||
127 | /** |
||
128 | * @param FormMapper $formMapper |
||
129 | * @param string $name |
||
130 | * @param string $label |
||
131 | * @param array $options |
||
132 | */ |
||
133 | 3 | View Code Duplication | public function addRequiredFileField(FormMapper $formMapper, $name, $label, $options = []) |
134 | { |
||
135 | 3 | $this->doAddFileField( |
|
136 | $formMapper, |
||
137 | $name, |
||
138 | $label, |
||
139 | 3 | true, |
|
140 | [ |
||
141 | 3 | new Constraint\File($this->getFileConstraintOptions($options)), |
|
142 | 3 | new Constraint\Callback([$this, 'validateExtension']), |
|
143 | ] |
||
144 | ); |
||
145 | 3 | } |
|
146 | |||
147 | /** |
||
148 | * @param array $options |
||
149 | * @return array |
||
150 | */ |
||
151 | 3 | protected function getFileConstraintOptions(array $options = []) |
|
158 | |||
159 | /** |
||
160 | * @param $object |
||
161 | * @param ExecutionContextInterface $context |
||
162 | */ |
||
163 | 3 | public function validateExtension($object, ExecutionContextInterface $context) |
|
164 | { |
||
165 | 3 | if ($object instanceof UploadedFile && isset($this->fileConstraintOptions['extensions'])) { |
|
166 | 3 | if (!in_array($object->getClientOriginalExtension(), $this->fileConstraintOptions['extensions'])) { |
|
167 | 1 | $context->addViolation( |
|
168 | 1 | sprintf( |
|
169 | 1 | 'It\'s not allowed to upload a file with extension "%s"', |
|
170 | 1 | $object->getClientOriginalExtension() |
|
171 | ) |
||
172 | ); |
||
173 | } |
||
174 | } |
||
175 | 3 | } |
|
176 | |||
177 | /** |
||
178 | * @param $extension |
||
179 | * @return string |
||
180 | */ |
||
181 | 2 | protected function getImageByExtension($extension) |
|
182 | { |
||
183 | 2 | if (in_array($extension, ['zip', 'rar', 'tar', 'gz'])) { |
|
184 | return 'archive.png'; |
||
185 | } |
||
186 | 2 | View Code Duplication | if (in_array($extension, ['wav', 'mp3', 'flac', 'aac', 'aiff', 'm4a', 'ogg', 'oga', 'wma'])) { |
187 | return 'audio.png'; |
||
188 | } |
||
189 | 2 | if (in_array($extension, ['php', 'html', 'css', 'js', 'vb', 'phar', 'py', 'jar', 'json', 'yml'])) { |
|
190 | return 'code.png'; |
||
191 | } |
||
192 | 2 | View Code Duplication | if (in_array($extension, ['xls', 'xlt', 'xlm', 'xlsx', 'xlsm', 'xltx', 'xltm'])) { |
193 | return 'excel.png'; |
||
194 | } |
||
195 | 2 | if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'ai', 'psd'])) { |
|
196 | return 'image.png'; |
||
197 | } |
||
198 | 2 | if (in_array($extension, ['mp4', 'avi', 'mkv', 'mpg', 'mpeg'])) { |
|
199 | return 'movie.png'; |
||
200 | } |
||
201 | 2 | if (in_array($extension, ['pdf'])) { |
|
202 | return 'pdf.png'; |
||
203 | } |
||
204 | 2 | if (in_array( |
|
205 | $extension, |
||
206 | 2 | ['ppt', 'pot', 'pos', 'pps', 'pptx', 'pptm', 'potx', 'potm', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm'] |
|
207 | )) { |
||
208 | return 'powerpoint.png'; |
||
209 | } |
||
210 | 2 | if (in_array($extension, ['txt'])) { |
|
211 | 2 | return 'text.png'; |
|
212 | } |
||
213 | View Code Duplication | if (in_array($extension, ['doc', 'dot', 'wbk', 'docx', 'docm', 'dotx', 'dotm', 'docb'])) { |
|
214 | return 'word.png'; |
||
215 | } |
||
216 | |||
217 | return 'default.png'; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | 2 | protected function getImageLocation() |
|
227 | |||
228 | /** |
||
229 | * @return bool |
||
230 | */ |
||
231 | 2 | public function supportsDownload() |
|
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | 2 | public function supportsEmbed() |
|
243 | |||
244 | /** |
||
245 | * @return bool |
||
246 | */ |
||
247 | 2 | public function supportsImage() |
|
251 | } |
||
252 |
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.