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 Media 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 Media, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class Media extends ContentType |
||
19 | { |
||
20 | const BASE_PATH = 'uploads/'; |
||
21 | const TYPE_PATH = 'tmp/'; |
||
22 | const BASE_URI = '@web/'; |
||
23 | public $usable = false; |
||
24 | public $canPreview = true; |
||
25 | |||
26 | public $upload; |
||
27 | public $_size; |
||
28 | public $_filename; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function upload($fileInstance) |
||
59 | |||
60 | /** |
||
61 | * Custom file validation based on mediainfo description. |
||
62 | * |
||
63 | * @param string $realFilepath filesystem path |
||
64 | * |
||
65 | * @return bool is file valid |
||
66 | */ |
||
67 | public static function validateFile($realFilepath) |
||
71 | |||
72 | /** |
||
73 | * Validate an url based on PHP filter_var. |
||
74 | * |
||
75 | * @param string $url |
||
76 | * |
||
77 | * @return bool valid |
||
78 | */ |
||
79 | public static function validateUrl($url) |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function sideload($url) |
||
140 | |||
141 | /** |
||
142 | * Header parsing method used to get size & filename. |
||
143 | * |
||
144 | * @param mixed $curl curl handler |
||
145 | * @param string $header |
||
146 | * |
||
147 | * @return int header length |
||
148 | */ |
||
149 | public function readHeaderFilename($curl, $header) |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function getLoadError() |
||
172 | |||
173 | /** |
||
174 | * Get storage path from web root. |
||
175 | * |
||
176 | * @return string path |
||
177 | */ |
||
178 | public static function getPath() |
||
182 | |||
183 | /** |
||
184 | * Get Yii aliased storage path. |
||
185 | * |
||
186 | * @return string path |
||
187 | */ |
||
188 | public static function getWebPath() |
||
192 | |||
193 | /** |
||
194 | * Get filesystem storage path. |
||
195 | * |
||
196 | * @return string path |
||
197 | */ |
||
198 | public static function getRealPath() |
||
202 | |||
203 | /** |
||
204 | * Try to get media info for this media. |
||
205 | * |
||
206 | * @return \Mhor\MediaInfo\Container\MediaInfoContainer|null media info |
||
207 | */ |
||
208 | protected static function getMediaInfo($realFilepath) |
||
216 | |||
217 | /** |
||
218 | * Use mediainfo to parse media duration. |
||
219 | * |
||
220 | * @param string $realFilepath |
||
221 | * |
||
222 | * @return int|null media duration |
||
223 | */ |
||
224 | public static function getDuration($realFilepath) |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function transformDataBeforeSave($insert, $data) |
||
263 | |||
264 | /** |
||
265 | * Create unique filename by checking for existence and appending to filename. |
||
266 | * |
||
267 | * @param string $path filepath |
||
268 | * @param string $filename |
||
269 | * |
||
270 | * @return string unique filename |
||
271 | */ |
||
272 | protected static function getUniqFilename($path, $filename) |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function processData($data) |
||
302 | } |
||
303 |
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.