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 YouTubeProvider extends AbstractProvider implements ProviderInterface |
||
12 | { |
||
13 | const URL_OEMBED = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json'; |
||
14 | const URL_IMAGE_MAX_RES = 'https://i.ytimg.com/vi/%s/maxresdefault.jpg'; |
||
15 | const URL_IMAGE_HQ = 'https://i.ytimg.com/vi/%s/hqdefault.jpg'; |
||
16 | |||
17 | /** |
||
18 | * @param FormMapper $formMapper |
||
19 | */ |
||
20 | public function buildProviderCreateForm(FormMapper $formMapper) |
||
24 | |||
25 | /** |
||
26 | * @param FormMapper $formMapper |
||
27 | */ |
||
28 | public function buildProviderEditForm(FormMapper $formMapper) |
||
32 | |||
33 | /** |
||
34 | * @param ErrorElement $errorElement |
||
35 | * @param Media $media |
||
36 | */ |
||
37 | public function validate(ErrorElement $errorElement, Media $media) |
||
46 | |||
47 | /** |
||
48 | * @param Media $media |
||
49 | * @param bool $providerReferenceUpdated |
||
50 | */ |
||
51 | public function update(Media $media, $providerReferenceUpdated) |
||
74 | |||
75 | /** |
||
76 | * @param Media $media |
||
77 | */ |
||
78 | public function refreshThumbnail(Media $media) |
||
88 | |||
89 | /** |
||
90 | * @param string $id |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getThumbnailUrlByYouTubeId($id) |
||
106 | |||
107 | /** |
||
108 | * @param $id |
||
109 | * @return mixed |
||
110 | * @throws \Exception |
||
111 | */ |
||
112 | View Code Duplication | protected function getDataByYouTubeId($id) |
|
124 | |||
125 | /** |
||
126 | * @param $value |
||
127 | * @return string |
||
128 | * @throws \Exception |
||
129 | */ |
||
130 | protected function parseYouTubeId($value) |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getIcon() |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getTitle() |
||
170 | |||
171 | public function getType() |
||
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getEmbedTemplate() |
||
183 | |||
184 | /** |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function supportsDownload() |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function supportsEmbed() |
||
199 | |||
200 | /** |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function supportsImage() |
||
207 | } |
||
208 |
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.