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 |
||
18 | class BaseSliderItem extends \DataObject { |
||
|
|||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | * @config |
||
23 | */ |
||
24 | private static $db = [ |
||
25 | 'Title' => 'Varchar(320)', |
||
26 | 'Content' => 'HTMLText', |
||
27 | 'AlignX' => 'Enum(array("Left", "Center", "Right"), "Left")', |
||
28 | 'AlignY' => 'Enum(array("Top", "Middle", "Bottom"), "Middle")', |
||
29 | 'Style' => 'Enum(array("Dark", "Light"), "Light")', |
||
30 | 'SortOrder' => 'Int', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | * @config |
||
36 | */ |
||
37 | private static $has_one = [ |
||
38 | 'Block' => 'SliderBlock', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Get localized types |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | View Code Duplication | public function getHorizontallyTypes() { |
|
47 | $types = []; |
||
48 | |||
49 | foreach ($this->dbObject('AlignX')->enumValues() as $alignType) { |
||
50 | $types[$alignType] = $this->fieldLabel($alignType); |
||
51 | } |
||
52 | |||
53 | return $types; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get localized types |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | View Code Duplication | public function getVerticallyTypes() { |
|
62 | $types = []; |
||
63 | |||
64 | foreach ($this->dbObject('AlignY')->enumValues() as $alignType) { |
||
65 | $types[$alignType] = $this->fieldLabel($alignType); |
||
66 | } |
||
67 | |||
68 | return $types; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get localized types |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | View Code Duplication | public function getStyles() { |
|
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getHorizontalType() { |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getLowerStyle() { |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getVerticalType() { |
||
106 | |||
107 | /** |
||
108 | * The default sort expression. This will be inserted in the ORDER BY |
||
109 | * clause of a SQL query if no other sort expression is provided. |
||
110 | * |
||
111 | * @var string |
||
112 | * @config |
||
113 | */ |
||
114 | private static $default_sort = 'SortOrder ASC'; |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function singular_name() { |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function plural_name() { |
||
129 | |||
130 | /** |
||
131 | * Default summary fields within localized label title's. |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | public function summaryFields() { |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getSliderType() { |
||
148 | |||
149 | /** |
||
150 | * @return \FieldList |
||
151 | */ |
||
152 | public function getCMSFields() { |
||
171 | |||
172 | /** |
||
173 | * @param bool $includeRelations |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function fieldLabels($includeRelations = true) { |
||
197 | |||
198 | /** |
||
199 | * @return \HTMLText |
||
200 | */ |
||
201 | public function forTemplate() { |
||
206 | |||
207 | /** |
||
208 | * @return false |
||
209 | */ |
||
210 | public function getSliderImage() { |
||
213 | |||
214 | public function getHeading() { |
||
215 | $title = $this->Title; |
||
221 | } |
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.