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() |
|
| 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.