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 |
||
9 | class FileProvider extends AbstractProvider |
||
10 | { |
||
11 | /** |
||
12 | * @param FormMapper $formMapper |
||
13 | */ |
||
14 | public function buildProviderCreateForm(FormMapper $formMapper) |
||
18 | |||
19 | /** |
||
20 | * @param FormMapper $formMapper |
||
21 | */ |
||
22 | public function buildProviderEditForm(FormMapper $formMapper) |
||
26 | |||
27 | /** |
||
28 | * @param Media $media |
||
29 | */ |
||
30 | public function update(Media $media) |
||
44 | |||
45 | /** |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getIcon() |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getTitle() |
||
60 | |||
61 | public function getType() |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getEmbedTemplate() |
||
73 | |||
74 | /** |
||
75 | * @param Media $media |
||
76 | */ |
||
77 | protected function setFileImage(Media $media) |
||
95 | |||
96 | /** |
||
97 | * @param $extension |
||
98 | * @return string |
||
99 | */ |
||
100 | protected function getImageByExtension($extension) |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function getImageLocation() |
||
146 | |||
147 | /** |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function supportsDownload() |
||
154 | |||
155 | /** |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function supportsEmbed() |
||
162 | |||
163 | /** |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function supportsImage() |
||
170 | } |
||
171 |
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.