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 SoundCloudProvider extends AbstractProvider implements ProviderInterface |
||
12 | { |
||
13 | const URL_OEMBED = 'http://soundcloud.com/oembed?format=json&url=https://soundcloud.com/%s'; |
||
14 | |||
15 | /** |
||
16 | * @param FormMapper $formMapper |
||
17 | */ |
||
18 | public function buildProviderCreateForm(FormMapper $formMapper) |
||
22 | |||
23 | /** |
||
24 | * @param FormMapper $formMapper |
||
25 | */ |
||
26 | public function buildProviderEditForm(FormMapper $formMapper) |
||
30 | |||
31 | /** |
||
32 | * @param Media $media |
||
33 | * @throws \Exception |
||
34 | */ |
||
35 | public function update(Media $media) |
||
62 | |||
63 | /** |
||
64 | * @param Media $media |
||
65 | * @param $thumbnailUrl |
||
66 | */ |
||
67 | public function setImage(Media $media, $thumbnailUrl) |
||
76 | |||
77 | /** |
||
78 | * @param $reference |
||
79 | * @return mixed |
||
80 | * @throws \Exception |
||
81 | */ |
||
82 | View Code Duplication | protected function getDataByReference($reference) |
|
96 | |||
97 | /** |
||
98 | * @param $value |
||
99 | * @return string |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | protected function parseReference($value) |
||
112 | |||
113 | /** |
||
114 | * @param array $data |
||
115 | */ |
||
116 | protected function extractEmbedUrl(array $data) |
||
126 | |||
127 | /** |
||
128 | * @param MediaInterface $media |
||
129 | * @param array $options |
||
130 | * @return array |
||
131 | */ |
||
132 | public function toArray(MediaInterface $media, array $options = []) |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getIcon() |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getTitle() |
||
154 | |||
155 | public function getType() |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getEmbedTemplate() |
||
167 | |||
168 | /** |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function supportsDownload() |
||
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function supportsEmbed() |
||
183 | |||
184 | /** |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function supportsImage() |
||
191 | } |
||
192 |
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.