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 |
||
9 | class UserDefinedForm_EmailRecipientCondition extends DataObject { |
||
|
|||
10 | |||
11 | /** |
||
12 | * List of options |
||
13 | * |
||
14 | * @config |
||
15 | * @var array |
||
16 | */ |
||
17 | private static $condition_options = array( |
||
18 | "IsBlank" => "Is blank", |
||
19 | "IsNotBlank" => "Is not blank", |
||
20 | "Equals" => "Equals", |
||
21 | "NotEquals" => "Doesn't equal" |
||
22 | ); |
||
23 | |||
24 | private static $db = array( |
||
25 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals")', |
||
26 | 'ConditionValue' => 'Varchar' |
||
27 | ); |
||
28 | |||
29 | private static $has_one = array( |
||
30 | 'Parent' => 'UserDefinedForm_EmailRecipient', |
||
31 | 'ConditionField' => 'EditableFormField' |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * Determine if this rule matches the given condition |
||
36 | * |
||
37 | * @param array $data |
||
38 | * @param Form $form |
||
39 | * @return bool |
||
40 | */ |
||
41 | public function matches($data, $form) { |
||
56 | |||
57 | /** |
||
58 | * Return whether a user can create an object of this type |
||
59 | * |
||
60 | * @param Member $member |
||
61 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
62 | * @return bool |
||
63 | */ |
||
64 | View Code Duplication | public function canCreate($member = null) { |
|
74 | |||
75 | /** |
||
76 | * Helper method to check the parent for this object |
||
77 | * |
||
78 | * @param array $args List of arguments passed to canCreate |
||
79 | * @return SiteTree Parent page instance |
||
80 | */ |
||
81 | View Code Duplication | protected function getCanCreateContext($args) { |
|
94 | |||
95 | /** |
||
96 | * @param Member |
||
97 | * |
||
98 | * @return boolean |
||
99 | */ |
||
100 | public function canView($member = null) { |
||
103 | |||
104 | /** |
||
105 | * @param Member |
||
106 | * |
||
107 | * @return boolean |
||
108 | */ |
||
109 | public function canEdit($member = null) { |
||
112 | |||
113 | /** |
||
114 | * @param Member |
||
115 | * |
||
116 | * @return boolean |
||
117 | */ |
||
118 | public function canDelete($member = null) { |
||
121 | } |
||
122 |
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.