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 |
||
23 | class VideoSliderItem extends BaseSliderItem { |
||
|
|||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | * @config |
||
28 | */ |
||
29 | private static $db = [ |
||
30 | 'Type' => 'Enum(array("Youtube", "Vimeo", "File"), "File")', |
||
31 | 'URL' => 'Varchar(128)', |
||
32 | 'AutoPlay' => 'Boolean(true)', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | * @config |
||
38 | */ |
||
39 | private static $has_one = [ |
||
40 | 'Mp4' => 'File', |
||
41 | 'WebM' => 'File', |
||
42 | 'Ogg' => 'File', |
||
43 | 'Cover' => 'Image', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Allow to call those functions. |
||
48 | * |
||
49 | * @var array |
||
50 | * @config |
||
51 | */ |
||
52 | private static $better_buttons_actions = [ |
||
53 | 'fetchVideosPicture', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Set providers default embed link. The key value should |
||
58 | * be equal within Type field value. |
||
59 | * {VideoId} - will be replaced with actual video id |
||
60 | * {AutoPlay} - will be replaced within key of AutoPlay. |
||
61 | * |
||
62 | * @var array |
||
63 | * @config |
||
64 | */ |
||
65 | private static $embed_links = [ |
||
66 | "Youtube" => [ |
||
67 | "Link" => "https://www.youtube.com/embed/{VideoId}{AutoPlay}", |
||
68 | "AutoPlay" => "?autoplay=1", |
||
69 | ], |
||
70 | "Vimeo" => [ |
||
71 | "Link" => "https://player.vimeo.com/video/{VideoId}{AutoPlay}", |
||
72 | "AutoPlay" => "?autoplay=1", |
||
73 | ], |
||
74 | // Set your own providers options |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | * @config |
||
80 | */ |
||
81 | private static $thumbnail_links = [ |
||
82 | "youtube" => "https://img.youtube.com/vi/{VideoId}/maxresdefault.jpg", |
||
83 | "vimeo" => "http://vimeo.com/api/v2/video/{VideoId}.php", |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getVideoType() { |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function singular_name() { |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function plural_name() { |
||
106 | |||
107 | /** |
||
108 | * @return array |
||
109 | */ |
||
110 | View Code Duplication | public function getSliderTypes() { |
|
119 | |||
120 | /** |
||
121 | * @return \FieldList |
||
122 | */ |
||
123 | View Code Duplication | public function getCMSFields() { |
|
195 | |||
196 | /** |
||
197 | * @param bool $includeRelations |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function fieldLabels($includeRelations = true) { |
||
204 | |||
205 | /** |
||
206 | * @return array |
||
207 | */ |
||
208 | public static function labels() { |
||
222 | |||
223 | /** |
||
224 | * This will get an id of the URL address or false |
||
225 | * if can't parsed, object type not one of supported |
||
226 | * providers or just empty url address field. |
||
227 | * |
||
228 | * @return string|false |
||
229 | * @throws ProviderNotFound |
||
230 | */ |
||
231 | View Code Duplication | public function getVideoId() { |
|
240 | |||
241 | /** |
||
242 | * Get embed link by the set of Type field. Method depends by |
||
243 | * static::$embed_links property. |
||
244 | * |
||
245 | * @return bool|string |
||
246 | */ |
||
247 | View Code Duplication | public function getEmbedLink() { |
|
269 | |||
270 | /** |
||
271 | * @return \ValidationResult |
||
272 | */ |
||
273 | View Code Duplication | protected function validate() { |
|
294 | |||
295 | /** |
||
296 | * @return Image|false |
||
297 | */ |
||
298 | public function getSliderImage() { |
||
305 | |||
306 | /** |
||
307 | * Creating a button to fetch videos picture if cover image not exists. |
||
308 | * |
||
309 | * @return FieldList |
||
310 | */ |
||
311 | View Code Duplication | public function getBetterButtonsActions() { |
|
312 | $fields = parent::getBetterButtonsActions(); |
||
313 | |||
314 | if ($this->Type != 'File' && ! $this->Cover()->exists() && ! empty($this->URL)) { |
||
315 | $fields->push(BetterButtonCustomAction::create('fetchVideosPicture', _t('VideoSliderItem.FETCH_VIDEOS_PICTURE', 'Fetch videos picture'))); |
||
316 | } |
||
317 | |||
318 | return $fields; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Fetching/downloading picture from the providers url address and |
||
323 | * saving as Image object. |
||
324 | * |
||
325 | * @return false |
||
326 | */ |
||
327 | public function fetchVideosPicture() { |
||
368 | |||
369 | } |
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.