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 | 1 | ); |
|
| 43 | |||
| 44 | 1 | /** |
|
| 45 | 1 | * |
|
| 46 | 1 | * Determine if this rule matches the given condition |
|
| 47 | 1 | * |
|
| 48 | 1 | * @param $data |
|
| 49 | 1 | * |
|
| 50 | 1 | * @return bool|null |
|
| 51 | 1 | * @throws LogicException |
|
| 52 | 1 | */ |
|
| 53 | 1 | public function matches($data) |
|
| 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.