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 |
||
8 | class GSTTaxModifierOptions extends DataObject |
||
|
|||
9 | { |
||
10 | |||
11 | /** |
||
12 | * standard SS variable |
||
13 | * @var Array |
||
14 | */ |
||
15 | private static $db = array( |
||
16 | "CountryCode" => "Varchar(3)", |
||
17 | "Code" => "Varchar(12)", |
||
18 | "Name" => "Varchar(175)", |
||
19 | "LegalNotice" => "Varchar(255)", |
||
20 | "InclusiveOrExclusive" => "Enum('Inclusive,Exclusive', 'Inclusive')", |
||
21 | "Rate" => "Double", |
||
22 | "DoesNotApplyToAllProducts" => "Boolean", |
||
23 | "AppliesToAllCountries" => "Boolean" |
||
24 | ); |
||
25 | |||
26 | /** |
||
27 | * standard SS variable |
||
28 | * @var Array |
||
29 | */ |
||
30 | private static $defaults = array( |
||
31 | "InclusiveOrExclusive" => "Inclusive" |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * standard SS variable |
||
36 | * @var Array |
||
37 | */ |
||
38 | private static $casting = array( |
||
39 | "CountryName" => "Varchar", |
||
40 | "PercentageNice" => "Varchar" |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * standard SS variable |
||
45 | * @var Array |
||
46 | */ |
||
47 | private static $indexes = array( |
||
48 | "Code" => true |
||
49 | ); |
||
50 | |||
51 | /** |
||
52 | * standard SS variable |
||
53 | * @var Array |
||
54 | */ |
||
55 | private static $searchable_fields = array( |
||
56 | "CountryCode" => "PartialMatchFilter", |
||
57 | "Code" => "PartialMatchFilter", |
||
58 | "Name" => "PartialMatchFilter" |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * standard SS variable |
||
63 | * @var Array |
||
64 | */ |
||
65 | private static $field_labels = array( |
||
66 | "CountryName" => "Country Name", |
||
67 | "CountryCode" => "Country Code", |
||
68 | "Code" => "Code for tax", |
||
69 | "Name" => "Name for tax", |
||
70 | "InclusiveOrExclusive" => "Inclusive/Exclusive", |
||
71 | "LegalNotice" => "Here you can put your GST number or VAT registration number", |
||
72 | "Rate" => "Rate (e.g. 0.125 = 12.5%)", |
||
73 | "PercentageNice" => "Percentage", |
||
74 | "DoesNotApplyToAllProducts" => "Added to individual products only" |
||
75 | ); |
||
76 | |||
77 | |||
78 | /** |
||
79 | * standard SS variable |
||
80 | * @var Array |
||
81 | */ |
||
82 | private static $summary_fields = array( |
||
83 | "CountryName" => 'Country', |
||
84 | "Code" => 'Code', |
||
85 | "Name" => 'Title', |
||
86 | "InclusiveOrExclusive" => 'Type', |
||
87 | "PercentageNice" => 'Percentage' |
||
88 | ); |
||
89 | |||
90 | /** |
||
91 | * standard SS variable |
||
92 | * @var String |
||
93 | */ |
||
94 | private static $singular_name = "Tax Option"; |
||
95 | public function i18n_singular_name() |
||
99 | |||
100 | |||
101 | /** |
||
102 | * standard SS variable |
||
103 | * @var String |
||
104 | */ |
||
105 | private static $plural_name = "Tax Options"; |
||
106 | public function i18n_plural_name() |
||
110 | |||
111 | /** |
||
112 | * standard SS method |
||
113 | * @param Member | NULL |
||
114 | * @return Boolean |
||
115 | */ |
||
116 | View Code Duplication | public function canCreate($member = null) |
|
127 | |||
128 | /** |
||
129 | * standard SS method |
||
130 | * @param Member | NULL |
||
131 | * @return Boolean |
||
132 | */ |
||
133 | View Code Duplication | public function canView($member = null) |
|
144 | |||
145 | /** |
||
146 | * standard SS method |
||
147 | * @param Member | NULL |
||
148 | * @return Boolean |
||
149 | */ |
||
150 | View Code Duplication | public function canEdit($member = null) |
|
161 | |||
162 | /** |
||
163 | * standard SS method |
||
164 | * @param Member | NULL |
||
165 | * @return Boolean |
||
166 | */ |
||
167 | View Code Duplication | public function canDelete($member = null) |
|
178 | |||
179 | /** |
||
180 | * standard SS method |
||
181 | * @return FieldList |
||
182 | */ |
||
183 | public function getCMSFields() |
||
195 | |||
196 | public function Title() |
||
209 | |||
210 | /** |
||
211 | * standard SS method |
||
212 | */ |
||
213 | public function populateDefaults() |
||
230 | |||
231 | /** |
||
232 | * standard SS method |
||
233 | */ |
||
234 | public function onBeforeWrite() |
||
243 | |||
244 | // /** |
||
245 | // * standard SS method |
||
246 | // */ |
||
247 | // public function requireDefaultRecords() |
||
248 | // { |
||
249 | // parent::requireDefaultRecords(); |
||
250 | // DB::query(" |
||
251 | // UPDATE \"GSTTaxModifierOptions\" |
||
252 | // SET \"InclusiveOrExclusive\" = 'Inclusive' |
||
253 | // WHERE \"InclusiveOrExclusive\" <> 'Exclusive'" |
||
254 | // ); |
||
255 | // } |
||
256 | |||
257 | /** |
||
258 | * returns the instance of EcommerceDBConfig |
||
259 | * |
||
260 | * @return EcommerceDBConfig |
||
261 | **/ |
||
262 | public function EcomConfig() |
||
266 | |||
267 | public function CountryName() |
||
275 | |||
276 | public function PercentageNice() |
||
284 | } |
||
285 |
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.