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 ShareThisDataObject extends DataObject |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private static $table_name = 'ShareThisDataObject'; |
||
|
|||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private static $permission_framework = [ |
||
34 | "SOCIAL_MEDIA" => [ |
||
35 | 'name' => "Social Media Management", |
||
36 | 'category' => "Social Media", |
||
37 | 'help' => 'Edit relationships, links and data of various social media platforms.', |
||
38 | 'sort' => 0 |
||
39 | ] |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private static $db = [ |
||
46 | 'Title' => 'Varchar(20)', |
||
47 | 'IncludeThisIcon' => 'Boolean', |
||
48 | 'IncludeThisIconInExtendedList' => 'Boolean', |
||
49 | 'Sort' => 'Int' |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private static $has_one = [ |
||
56 | 'AlternativeIcon' => Image::class |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private static $casting = [ |
||
63 | 'Icon' => 'HTMLText', |
||
64 | 'IncludeThisIconNice' => 'Varchar', |
||
65 | 'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList' |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | private static $field_labels = [ |
||
72 | 'Title' => 'Name', |
||
73 | 'IncludeThisIcon' => 'Include in main list', |
||
74 | 'IncludeThisIconNice' => 'Include in primary list', |
||
75 | 'IncludeThisIconInExtendedList' => 'Include in secondary list', |
||
76 | 'IncludeThisIconInExtendedListNice' => 'Include in secondary list', |
||
77 | 'Sort' => 'Sort Index (lower numbers shown first)', |
||
78 | 'AlternativeIcon' => 'Optional Alternative Icon (can be any size, a 32px by 32px square is recommended)' |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | private static $summary_fields = [ |
||
85 | 'Title' => 'Name', |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | private static $singular_name = 'Icon to share this page'; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | private static $plural_name = 'Icons to share this page'; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | private static $default_sort = 'IncludeThisIcon DESC, IncludeThisIconInExtendedList ASC, Sort ASC, Title ASC'; |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | public function providePermissions() |
||
110 | |||
111 | /** |
||
112 | * @return boolean |
||
113 | */ |
||
114 | public function canView($member = null) |
||
118 | |||
119 | /** |
||
120 | * @return boolean |
||
121 | */ |
||
122 | public function canCreate($member = null, $context = []) |
||
126 | |||
127 | /** |
||
128 | * @return boolean |
||
129 | */ |
||
130 | public function canEdit($member = null) |
||
134 | |||
135 | /** |
||
136 | * @return boolean |
||
137 | */ |
||
138 | public function canDelete($member = null) |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function IncludeThisIconNice() |
||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getIncludeThisIconNice() |
||
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | public function IncludeThisIconInExtendedListNice() |
||
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getIncludeThisIconInExtendedListNice() |
||
174 | |||
175 | /** |
||
176 | * Icon |
||
177 | */ |
||
178 | public function Icon() |
||
182 | |||
183 | /** |
||
184 | * Get the icon |
||
185 | * |
||
186 | * @return DBField [<description>] |
||
187 | */ |
||
188 | public function getIcon() |
||
199 | |||
200 | /** |
||
201 | * @return FieldList $fields |
||
202 | */ |
||
203 | public function getCMSFields() |
||
212 | |||
213 | /** |
||
214 | * @return void |
||
215 | */ |
||
216 | public function onAfterWrite() |
||
221 | |||
222 | /** |
||
223 | * @return ValidationResult $result |
||
224 | */ |
||
225 | public function validate() |
||
241 | |||
242 | /** |
||
243 | * Setting default records |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | public function requireDefaultRecords() |
||
301 | } |
||
302 |