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 RemoteVideoHandler 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 RemoteVideoHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class RemoteVideoHandler extends AbstractMediaHandler |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | const CONTENT_TYPE = 'remote/video'; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | const TYPE = 'video'; |
||
24 | |||
25 | /** |
||
26 | * @return string |
||
27 | */ |
||
28 | public function getName() |
||
32 | |||
33 | /** |
||
34 | * @return string |
||
35 | */ |
||
36 | public function getType() |
||
40 | |||
41 | /** |
||
42 | * @return RemoteVideoType |
||
43 | */ |
||
44 | public function getFormType() |
||
48 | |||
49 | /** |
||
50 | * @param mixed $object |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | View Code Duplication | public function canHandle($object) |
|
62 | |||
63 | /** |
||
64 | * @param Media $media |
||
65 | * |
||
66 | * @return RemoteVideoHelper |
||
67 | */ |
||
68 | public function getFormHelper(Media $media) |
||
72 | |||
73 | /** |
||
74 | * @param Media $media |
||
75 | * |
||
76 | * @throws \RuntimeException when the file does not exist |
||
77 | */ |
||
78 | public function prepareMedia(Media $media) |
||
109 | |||
110 | /** |
||
111 | * @param $link |
||
112 | * |
||
113 | * @throws VideoException |
||
114 | * |
||
115 | * @return mixed |
||
116 | */ |
||
117 | View Code Duplication | public function isolateVimeoVideoCode($link) |
|
127 | |||
128 | /** |
||
129 | * @param $link |
||
130 | * |
||
131 | * @throws VideoException |
||
132 | * |
||
133 | * @return mixed |
||
134 | */ |
||
135 | View Code Duplication | public function isolateYoutubeVideoCode($link) |
|
145 | |||
146 | /** |
||
147 | * @param $link |
||
148 | * |
||
149 | * @throws VideoException |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isolateDailymotionVideoCode($link) |
||
172 | |||
173 | /** |
||
174 | * String helper. |
||
175 | * |
||
176 | * @param string $str string |
||
177 | * @param string $sub substring |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | private function endsWith($str, $sub) |
||
185 | |||
186 | /** |
||
187 | * @param Media $media |
||
188 | */ |
||
189 | public function saveMedia(Media $media) |
||
192 | |||
193 | /** |
||
194 | * @param Media $media |
||
195 | */ |
||
196 | public function removeMedia(Media $media) |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function updateMedia(Media $media) |
||
206 | |||
207 | /** |
||
208 | * @param array $params |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getAddUrlFor(array $params = []) |
||
223 | |||
224 | /** |
||
225 | * @param mixed $data |
||
226 | * |
||
227 | * @return Media |
||
228 | */ |
||
229 | public function createNew($data) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function getShowTemplate(Media $media) |
||
282 | |||
283 | /** |
||
284 | * @param Media $media The media entity |
||
285 | * @param string $basepath The base path |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public function getImageUrl(Media $media, $basepath) |
||
295 | |||
296 | /** |
||
297 | * @return array |
||
298 | */ |
||
299 | public function getAddFolderActions() |
||
307 | } |
||
308 |
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.