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 EditableOption extends DataObject { |
||
|
|||
11 | |||
12 | private static $default_sort = "Sort"; |
||
13 | |||
14 | private static $db = array( |
||
15 | "Name" => "Varchar(255)", |
||
16 | "Title" => "Varchar(255)", |
||
17 | "Default" => "Boolean", |
||
18 | "Sort" => "Int", |
||
19 | "Value" => "Varchar(255)", |
||
20 | ); |
||
21 | |||
22 | private static $has_one = array( |
||
23 | "Parent" => "EditableMultipleOptionField", |
||
24 | ); |
||
25 | |||
26 | private static $extensions = array( |
||
27 | "Versioned('Stage', 'Live')" |
||
28 | ); |
||
29 | |||
30 | private static $summary_fields = array( |
||
31 | 'Title', |
||
32 | 'Default' |
||
33 | ); |
||
34 | |||
35 | protected static $allow_empty_values = false; |
||
36 | |||
37 | /** |
||
38 | * Returns whether to allow empty values or not. |
||
39 | * |
||
40 | * @return boolean |
||
41 | */ |
||
42 | public static function allow_empty_values() { |
||
45 | |||
46 | /** |
||
47 | * Set whether to allow empty values. |
||
48 | * |
||
49 | * @param boolean $allow |
||
50 | */ |
||
51 | public static function set_allow_empty_values($allow) { |
||
54 | |||
55 | /** |
||
56 | * @param Member $member |
||
57 | * |
||
58 | * @return boolean |
||
59 | */ |
||
60 | public function canEdit($member = null) { |
||
61 | return $this->Parent()->canEdit($member); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param Member $member |
||
66 | * |
||
67 | * @return boolean |
||
68 | */ |
||
69 | public function canDelete($member = null) { |
||
70 | return $this->canEdit($member); |
||
71 | } |
||
72 | |||
73 | public function getEscapedTitle() { |
||
74 | return Convert::raw2att($this->Title); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param Member $member |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function canView($member = null) { |
||
84 | |||
85 | /** |
||
86 | * Return whether a user can create an object of this type |
||
87 | * |
||
88 | * @param Member $member |
||
89 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
90 | * @return bool |
||
91 | */ |
||
92 | View Code Duplication | public function canCreate($member = null) { |
|
102 | |||
103 | /** |
||
104 | * Helper method to check the parent for this object |
||
105 | * |
||
106 | * @param array $args List of arguments passed to canCreate |
||
107 | * @return DataObject Some parent dataobject to inherit permissions from |
||
108 | */ |
||
109 | View Code Duplication | protected function getCanCreateContext($args) { |
|
122 | |||
123 | /** |
||
124 | * @param Member $member |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function canPublish($member = null) { |
||
130 | |||
131 | /** |
||
132 | * @param Member $member |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function canUnpublish($member = null) { |
||
138 | |||
139 | /** |
||
140 | * Fetches a value for $this->Value. If empty values are not allowed, |
||
141 | * then this will return the title in the case of an empty value. |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getValue() |
||
153 | } |
||
154 |
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.