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 |
||
10 | class BuyableStockOrderEntry extends DataObject |
||
|
|||
11 | { |
||
12 | private static $db = array( |
||
13 | "Quantity" => "Int", |
||
14 | "IncludeInCurrentCalculation" => "Boolean" |
||
15 | ); |
||
16 | |||
17 | private static $has_one = array( |
||
18 | "Parent" => "BuyableStockCalculatedQuantity", |
||
19 | "Order" => "Order", |
||
20 | ); |
||
21 | |||
22 | private static $defaults = array( |
||
23 | "IncludeInCurrentCalculation" => 1 |
||
24 | ); |
||
25 | |||
26 | |||
27 | //MODEL ADMIN STUFF |
||
28 | private static $searchable_fields = array( |
||
29 | "Quantity", |
||
30 | "IncludeInCurrentCalculation", |
||
31 | "ParentID", |
||
32 | "OrderID", |
||
33 | ); |
||
34 | |||
35 | private static $field_labels = array( |
||
36 | "Quantity" => "Calculated Quantity On Hand", |
||
37 | "IncludeInCurrentCalculation" => "Include in Calculation", |
||
38 | "ParentID" => "Buyable Calculation", |
||
39 | "OrderID" => "Order" |
||
40 | ); |
||
41 | |||
42 | private static $summary_fields = array( |
||
43 | "OrderID", |
||
44 | "ParentID", |
||
45 | "Quantity" |
||
46 | ); |
||
47 | |||
48 | |||
49 | private static $default_sort = [ |
||
50 | 'LastEdited' => 'DESC', |
||
51 | 'ParentID' => 'ASC', |
||
52 | 'ID' => 'DESC' |
||
53 | ]; |
||
54 | |||
55 | private static $indexes = [ |
||
56 | 'LastEdited' => true |
||
57 | ]; |
||
58 | |||
59 | private static $singular_name = "Stock Sale Entry"; |
||
60 | public function i18n_singular_name() |
||
64 | |||
65 | private static $plural_name = "Stock Sale Entries"; |
||
66 | public function i18n_plural_name() |
||
70 | |||
71 | public function canCreate($member = null) |
||
75 | |||
76 | public function canEdit($member = null) |
||
80 | |||
81 | public function canDelete($member = null) |
||
85 | |||
86 | public function canView($member = null) |
||
90 | |||
91 | View Code Duplication | protected function canDoAnything() |
|
99 | |||
100 | public function onAfterWrite() |
||
125 | } |
||
126 |
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.