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 | ];  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * Load javascript plugin to load all block features. Set to false  | 
            ||
| 56 | * and add yours. This will load /assets/javascript/video-block.js file.  | 
            ||
| 57 | *  | 
            ||
| 58 | * @var bool  | 
            ||
| 59 | * @config  | 
            ||
| 60 | */  | 
            ||
| 61 | private static $load_javascript_plugin = true;  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * If the singular name is set in a private static $singular_name, it cannot be changed using the translation files  | 
            ||
| 65 | * for some reason. Fix it by defining a method that handles the translation.  | 
            ||
| 66 | * @return string  | 
            ||
| 67 | */  | 
            ||
| 68 |     public function singular_name() { | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * If the plural name is set in a private static $plural_name, it cannot be changed using the translation files  | 
            ||
| 74 | * for some reason. Fix it by defining a method that handles the translation.  | 
            ||
| 75 | * @return string  | 
            ||
| 76 | */  | 
            ||
| 77 |     public function plural_name() { | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * @return string  | 
            ||
| 83 | */  | 
            ||
| 84 |     public function getVideoType() { | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 | * @return array  | 
            ||
| 90 | */  | 
            ||
| 91 | View Code Duplication |     public function getVideoTypes() { | 
            |
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * @return FieldList  | 
            ||
| 103 | */  | 
            ||
| 104 | View Code Duplication |     public function getCMSFields() { | 
            |
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * @param bool $includeRelations  | 
            ||
| 167 | *  | 
            ||
| 168 | * @return array  | 
            ||
| 169 | */  | 
            ||
| 170 |     public function fieldLabels($includeRelations = true) { | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * This will get an id of the URL address or false  | 
            ||
| 176 | * if can't parsed, object type not one of supported  | 
            ||
| 177 | * providers or just empty url address field.  | 
            ||
| 178 | *  | 
            ||
| 179 | * @return string|false  | 
            ||
| 180 | * @throws ProviderNotFound  | 
            ||
| 181 | */  | 
            ||
| 182 | View Code Duplication |     public function getVideoId() { | 
            |
| 191 | |||
| 192 | /**  | 
            ||
| 193 | * Get embed link by the set of Type field. Method depends by  | 
            ||
| 194 | * VideoSliderItem::$embed_links property.  | 
            ||
| 195 | *  | 
            ||
| 196 | * @return bool|string  | 
            ||
| 197 | */  | 
            ||
| 198 | View Code Duplication |     public function getEmbedLink() { | 
            |
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * @return \ValidationResult  | 
            ||
| 223 | */  | 
            ||
| 224 | View Code Duplication |     protected function validate() { | 
            |
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * @return Image|false  | 
            ||
| 248 | */  | 
            ||
| 249 |     public function getCoverImage() { | 
            ||
| 256 | |||
| 257 | /**  | 
            ||
| 258 | * Creating a button to fetch videos picture if cover image not exists.  | 
            ||
| 259 | *  | 
            ||
| 260 | * @return FieldList  | 
            ||
| 261 | */  | 
            ||
| 262 | View Code Duplication |     public function getBetterButtonsActions() { | 
            |
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * @return bool|string  | 
            ||
| 274 | */  | 
            ||
| 275 | View Code Duplication |     public function getMp4VideoUrl() { | 
            |
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * @return bool|string  | 
            ||
| 287 | */  | 
            ||
| 288 | View Code Duplication |     public function getWebMVideoUrl() { | 
            |
| 297 | |||
| 298 | /**  | 
            ||
| 299 | * @return bool|string  | 
            ||
| 300 | */  | 
            ||
| 301 | View Code Duplication |     public function getOggVideoUrl() { | 
            |
| 310 | |||
| 311 | /**  | 
            ||
| 312 | * Fetching/downloading picture from the providers url address and  | 
            ||
| 313 | * saving as Image object.  | 
            ||
| 314 | *  | 
            ||
| 315 | * @return false  | 
            ||
| 316 | */  | 
            ||
| 317 |     public function fetchVideosPicture() { | 
            ||
| 368 | |||
| 369 |     public function getVideoOptions() { | 
            ||
| 392 | |||
| 393 | }  | 
            ||
| 394 | |||
| 405 | }  | 
            
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.