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 | * Publish this custom rule to the live site |
||
51 | * |
||
52 | * Wrapper for the {@link Versioned} publish function |
||
53 | */ |
||
54 | 1 | 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) |
|
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) |
|
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 | 2 | public function buildExpression() |
|
160 | { |
||
161 | /** @var EditableFormField $formFieldWatch */ |
||
162 | 2 | $formFieldWatch = $this->ConditionField(); |
|
163 | //Encapsulated the action to the object |
||
164 | 2 | $action = $formFieldWatch->getJsEventHandler(); |
|
165 | |||
166 | // is this field a special option field |
||
167 | 2 | $checkboxField = $formFieldWatch->isCheckBoxField(); |
|
168 | 2 | $radioField = $formFieldWatch->isRadioField(); |
|
169 | 2 | $target = sprintf('$("%s")', $formFieldWatch->getSelectorFieldOnly()); |
|
170 | 2 | $fieldValue = Convert::raw2js($this->FieldValue); |
|
171 | |||
172 | $conditionOptions = array( |
||
173 | 2 | 'ValueLessThan' => '<', |
|
174 | 2 | 'ValueLessThanEqual' => '<=', |
|
175 | 2 | 'ValueGreaterThan' => '>', |
|
176 | 'ValueGreaterThanEqual' => '>=' |
||
177 | 2 | ); |
|
178 | // and what should we evaluate |
||
179 | 2 | switch ($this->ConditionOption) { |
|
180 | 2 | case 'IsNotBlank': |
|
181 | 2 | case 'IsBlank': |
|
182 | $expression = ($checkboxField || $radioField) ? "!{$target}.is(\":checked\")" : "{$target}.val() == ''"; |
||
183 | if ($this->ConditionOption == 'IsNotBlank') { |
||
184 | //Negate |
||
185 | $expression = "!({$expression})"; |
||
186 | } |
||
187 | break; |
||
188 | 2 | case 'HasValue': |
|
189 | 2 | case 'ValueNot': |
|
190 | 2 | if ($checkboxField) { |
|
191 | if ($formFieldWatch->isCheckBoxGroupField()) { |
||
192 | $expression = sprintf( |
||
193 | "$.inArray('%s', %s.filter(':checked').map(function(){ return $(this).val();}).get()) > -1", |
||
194 | $fieldValue, |
||
195 | $target |
||
196 | ); |
||
197 | } else { |
||
198 | $expression = "{$target}.prop('checked')"; |
||
199 | } |
||
200 | 2 | } elseif ($radioField) { |
|
201 | // We cannot simply get the value of the radio group, we need to find the checked option first. |
||
202 | $expression = sprintf( |
||
203 | '%s.closest(".field, .control-group").find("input:checked").val() == "%s"', |
||
204 | $target, |
||
205 | $fieldValue |
||
206 | ); |
||
207 | } else { |
||
208 | 2 | $expression = sprintf('%s.val() == "%s"', $target, $fieldValue); |
|
209 | } |
||
210 | |||
211 | 2 | if ($this->ConditionOption == 'ValueNot') { |
|
212 | //Negate |
||
213 | $expression = "!({$expression})"; |
||
214 | } |
||
215 | 2 | break; |
|
216 | 1 | case 'ValueLessThan': |
|
217 | 1 | case 'ValueLessThanEqual': |
|
218 | 1 | case 'ValueGreaterThan': |
|
219 | 1 | case 'ValueGreaterThanEqual': |
|
220 | 1 | $expression = sprintf( |
|
221 | 1 | '%s.val() %s parseFloat("%s")', |
|
222 | 1 | $target, |
|
223 | 1 | $conditionOptions[$this->ConditionOption], |
|
224 | $fieldValue |
||
225 | 1 | ); |
|
226 | 1 | break; |
|
227 | default: |
||
228 | throw new LogicException("Unhandled rule {$this->ConditionOption}"); |
||
229 | break; |
||
230 | 2 | } |
|
231 | |||
232 | $result = array( |
||
233 | 2 | 'operation' => $expression, |
|
234 | 2 | 'event' => $action, |
|
235 | 2 | ); |
|
236 | |||
237 | 2 | return $result; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Returns the opposite visibility function for the value of the initial visibility field, e.g. show/hide. This |
||
242 | * will toggle the "hide" class either way, which is handled by CSS. |
||
243 | * |
||
244 | * @param string $text |
||
245 | * @return string |
||
246 | */ |
||
247 | 2 | public function toggleDisplayText($text) |
|
251 | } |
||
252 |
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.