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 | public function getCMSFields() { |
||
195 | |||
196 | /** |
||
197 | * @param bool $includeRelations |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function fieldLabels($includeRelations = true) { |
||
215 | |||
216 | /** |
||
217 | * This will get an id of the URL address or false |
||
218 | * if can't parsed, object type not one of supported |
||
219 | * providers or just empty url address field. |
||
220 | * |
||
221 | * @return string|false |
||
222 | * @throws ProviderNotFound |
||
223 | */ |
||
224 | public function getVideoId() { |
||
233 | |||
234 | /** |
||
235 | * Get embed link by the set of Type field. Method depends by |
||
236 | * static::$embed_links property. |
||
237 | * |
||
238 | * @return bool|string |
||
239 | */ |
||
240 | public function getEmbedLink() { |
||
262 | |||
263 | /** |
||
264 | * @return \ValidationResult |
||
265 | */ |
||
266 | protected function validate() { |
||
287 | |||
288 | /** |
||
289 | * @return Image|false |
||
290 | */ |
||
291 | public function getSliderImage() { |
||
298 | |||
299 | /** |
||
300 | * Creating a button to fetch videos picture if cover image not exists. |
||
301 | * |
||
302 | * @return FieldList |
||
303 | */ |
||
304 | public function getBetterButtonsActions() { |
||
313 | |||
314 | /** |
||
315 | * Fetching/downloading picture from the providers url address and |
||
316 | * saving as Image object. |
||
317 | * |
||
318 | * @return false |
||
319 | */ |
||
320 | public function fetchVideosPicture() { |
||
361 | |||
362 | } |
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.