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 VideoBlock 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 VideoBlock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class VideoBlock extends BaseBlock { |
||
|
|
|||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | * @config |
||
| 26 | */ |
||
| 27 | private static $db = [ |
||
| 28 | 'Type' => 'Enum(array("Youtube", "Vimeo", "File"), "File")', |
||
| 29 | 'URL' => 'Varchar(1024)', |
||
| 30 | 'AutoPlay' => 'Boolean(true)', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | * @config |
||
| 36 | */ |
||
| 37 | private static $has_one = [ |
||
| 38 | 'Mp4' => 'File', |
||
| 39 | 'WebM' => 'File', |
||
| 40 | 'Ogg' => 'File', |
||
| 41 | 'Cover' => 'Image', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Allow to call those functions. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | * @config |
||
| 49 | */ |
||
| 50 | private static $better_buttons_actions = [ |
||
| 51 | 'fetchVideosPicture', |
||
| 52 | 'renderVideo' |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Load javascript plugin to load all block features. Set to false |
||
| 57 | * and add yours. This will load /assets/javascript/video-block.js file. |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | * @config |
||
| 61 | */ |
||
| 62 | private static $load_javascript_plugin = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * If the singular name is set in a private static $singular_name, it cannot be changed using the translation files |
||
| 66 | * for some reason. Fix it by defining a method that handles the translation. |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function singular_name() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * If the plural name is set in a private static $plural_name, it cannot be changed using the translation files |
||
| 75 | * for some reason. Fix it by defining a method that handles the translation. |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function plural_name() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | public function getVideoType() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function getVideoTypes() { |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @return FieldList |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function getCMSFields() { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param bool $includeRelations |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public function fieldLabels($includeRelations = true) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * This will get an id of the URL address or false |
||
| 177 | * if can't parsed, object type not one of supported |
||
| 178 | * providers or just empty url address field. |
||
| 179 | * |
||
| 180 | * @return string|false |
||
| 181 | * @throws ProviderNotFound |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function getVideoId() { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get embed link by the set of Type field. Method depends by |
||
| 195 | * VideoSliderItem::$embed_links property. |
||
| 196 | * |
||
| 197 | * @return bool|string |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function getEmbedLink() { |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return \ValidationResult |
||
| 224 | */ |
||
| 225 | View Code Duplication | protected function validate() { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return Image|false |
||
| 249 | */ |
||
| 250 | public function getCoverImage() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Creating a button to fetch videos picture if cover image not exists. |
||
| 260 | * |
||
| 261 | * @return FieldList |
||
| 262 | */ |
||
| 263 | public function getBetterButtonsActions() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return bool|string |
||
| 281 | */ |
||
| 282 | View Code Duplication | public function getMp4VideoUrl() { |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool|string |
||
| 294 | */ |
||
| 295 | View Code Duplication | public function getWebMVideoUrl() { |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @return bool|string |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function getOggVideoUrl() { |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Fetching/downloading picture from the providers url address and |
||
| 320 | * saving as Image object. |
||
| 321 | * |
||
| 322 | * @return false |
||
| 323 | */ |
||
| 324 | public function fetchVideosPicture() { |
||
| 375 | |||
| 376 | public function renderVideo() { |
||
| 397 | |||
| 398 | public function getVideoOptions() { |
||
| 421 | |||
| 422 | } |
||
| 423 | |||
| 434 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.