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 |
||
14 | class EditableCustomRule extends DataObject |
||
|
|||
15 | { |
||
16 | |||
17 | private static $condition_options = array( |
||
18 | "IsBlank" => "Is blank", |
||
19 | "IsNotBlank" => "Is not blank", |
||
20 | "HasValue" => "Equals", |
||
21 | "ValueNot" => "Doesn't equal", |
||
22 | "ValueLessThan" => "Less than", |
||
23 | "ValueLessThanEqual" => "Less than or equal", |
||
24 | "ValueGreaterThan" => "Greater than", |
||
25 | "ValueGreaterThanEqual" => "Greater than or equal" |
||
26 | ); |
||
27 | |||
28 | private static $db = array( |
||
29 | 'Display' => 'Enum("Show,Hide")', |
||
30 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
||
31 | 'FieldValue' => 'Varchar(255)' |
||
32 | ); |
||
33 | |||
34 | private static $has_one = array( |
||
35 | 'Parent' => 'EditableFormField', |
||
36 | 'ConditionField' => 'EditableFormField' |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Built in extensions required |
||
41 | * |
||
42 | * @config |
||
43 | * @var array |
||
44 | */ |
||
45 | private static $extensions = array( |
||
46 | "Versioned('Stage', 'Live')" |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | 1 | * Publish this custom rule to the live site |
|
51 | * |
||
52 | 1 | * Wrapper for the {@link Versioned} publish function |
|
53 | 1 | */ |
|
54 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
||
58 | |||
59 | /** |
||
60 | * Delete this custom rule from a given stage |
||
61 | * |
||
62 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
63 | */ |
||
64 | public function doDeleteFromStage($stage) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @param Member $member |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function canDelete($member = null) |
||
78 | |||
79 | /** |
||
80 | * @param Member $member |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function canEdit($member = null) |
||
87 | |||
88 | /** |
||
89 | * @param Member $member |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function canView($member = null) |
||
96 | |||
97 | /** |
||
98 | * Return whether a user can create an object of this type |
||
99 | * |
||
100 | * @param Member $member |
||
101 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
102 | * @return bool |
||
103 | */ |
||
104 | View Code Duplication | public function canCreate($member = null) |
|
105 | { |
||
106 | // Check parent page |
||
107 | $parent = $this->getCanCreateContext(func_get_args()); |
||
108 | if ($parent) { |
||
109 | return $parent->canEdit($member); |
||
110 | } |
||
111 | |||
112 | // Fall back to secure admin permissions |
||
113 | return parent::canCreate($member); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Helper method to check the parent for this object |
||
118 | * |
||
119 | * @param array $args List of arguments passed to canCreate |
||
120 | * @return DataObject Some parent dataobject to inherit permissions from |
||
121 | */ |
||
122 | View Code Duplication | protected function getCanCreateContext($args) |
|
123 | { |
||
124 | // Inspect second parameter to canCreate for a 'Parent' context |
||
125 | if (isset($args[1]['Parent'])) { |
||
126 | return $args[1]['Parent']; |
||
127 | } |
||
128 | // Hack in currently edited page if context is missing |
||
129 | if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
||
130 | return Controller::curr()->currentPage(); |
||
131 | } |
||
132 | |||
133 | // No page being edited |
||
134 | return null; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param Member $member |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function canPublish($member = null) |
||
145 | |||
146 | /** |
||
147 | * @param Member $member |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function canUnpublish($member = null) |
||
154 | |||
155 | /** |
||
156 | * Substitutes configured rule logic with it's JS equivalents and returns them as array elements |
||
157 | * @return array |
||
158 | */ |
||
159 | public function buildExpression() |
||
229 | |||
230 | /** |
||
231 | * Returns the opposite of the show/hide pairs of strings |
||
232 | * |
||
233 | * @param string $text |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function toggleDisplayText($text) |
||
241 | } |
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.