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 UserDefinedForm_EmailRecipientCondition extends DataObject |
||
|
|||
15 | { |
||
16 | |||
17 | /** |
||
18 | * List of options |
||
19 | * |
||
20 | * @config |
||
21 | * @var array |
||
22 | */ |
||
23 | private static $condition_options = array( |
||
24 | "IsBlank" => "Is blank", |
||
25 | "IsNotBlank" => "Is not blank", |
||
26 | "Equals" => "Equals", |
||
27 | "NotEquals" => "Doesn't equal", |
||
28 | "ValueLessThan" => "Less than", |
||
29 | "ValueLessThanEqual" => "Less than or equal", |
||
30 | "ValueGreaterThan" => "Greater than", |
||
31 | "ValueGreaterThanEqual" => "Greater than or equal" |
||
32 | ); |
||
33 | |||
34 | private static $db = array( |
||
35 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
||
36 | 'ConditionValue' => 'Varchar' |
||
37 | ); |
||
38 | |||
39 | private static $has_one = array( |
||
40 | 'Parent' => 'UserDefinedForm_EmailRecipient', |
||
41 | 'ConditionField' => 'EditableFormField' |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | * Determine if this rule matches the given condition |
||
47 | * |
||
48 | * @param $data |
||
49 | * |
||
50 | * @return bool|null |
||
51 | * @throws LogicException |
||
52 | */ |
||
53 | 2 | public function matches($data) |
|
54 | { |
||
55 | 2 | $fieldName = $this->ConditionField()->Name; |
|
56 | 2 | $fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null; |
|
57 | 2 | $conditionValue = $this->ConditionValue; |
|
58 | 2 | $result = null; |
|
59 | 2 | switch ($this->ConditionOption) { |
|
60 | 2 | case 'IsBlank': |
|
61 | 2 | $result = empty($fieldValue); |
|
62 | 2 | break; |
|
63 | 2 | case 'IsNotBlank': |
|
64 | 2 | $result = !empty($fieldValue); |
|
65 | 2 | break; |
|
66 | 2 | case 'ValueLessThan': |
|
67 | 1 | $result = ($fieldValue < $conditionValue); |
|
68 | 1 | break; |
|
69 | 2 | case 'ValueLessThanEqual': |
|
70 | 1 | $result = ($fieldValue <= $conditionValue); |
|
71 | 1 | break; |
|
72 | 2 | case 'ValueGreaterThan': |
|
73 | 1 | $result = ($fieldValue > $conditionValue); |
|
74 | 1 | break; |
|
75 | 2 | case 'ValueGreaterThanEqual': |
|
76 | 1 | $result = ($fieldValue >= $conditionValue); |
|
77 | 1 | break; |
|
78 | 2 | case 'NotEquals': |
|
79 | 2 | case 'Equals': |
|
80 | 2 | $result = is_array($fieldValue) |
|
81 | 1 | ? in_array($conditionValue, $fieldValue) |
|
82 | 2 | : $fieldValue == $conditionValue; |
|
83 | |||
84 | 2 | if ($this->ConditionOption == 'NotEquals') { |
|
85 | 2 | $result = !($result); |
|
86 | } |
||
87 | 2 | break; |
|
88 | default: |
||
89 | throw new LogicException("Unhandled rule {$this->ConditionOption}"); |
||
90 | break; |
||
91 | } |
||
92 | |||
93 | 2 | return $result; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Return whether a user can create an object of this type |
||
98 | * |
||
99 | * @param Member $member |
||
100 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
101 | * @return bool |
||
102 | */ |
||
103 | View Code Duplication | public function canCreate($member = null) |
|
114 | |||
115 | /** |
||
116 | * Helper method to check the parent for this object |
||
117 | * |
||
118 | * @param array $args List of arguments passed to canCreate |
||
119 | * @return SiteTree Parent page instance |
||
120 | */ |
||
121 | View Code Duplication | protected function getCanCreateContext($args) |
|
135 | |||
136 | /** |
||
137 | * @param Member |
||
138 | * |
||
139 | * @return boolean |
||
140 | */ |
||
141 | public function canView($member = null) |
||
145 | |||
146 | /** |
||
147 | * @param Member |
||
148 | * |
||
149 | * @return boolean |
||
150 | */ |
||
151 | public function canEdit($member = null) |
||
155 | |||
156 | /** |
||
157 | * @param Member |
||
158 | * |
||
159 | * @return boolean |
||
160 | */ |
||
161 | public function canDelete($member = null) |
||
165 | } |
||
166 |
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.