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 |
||
| 24 | class EditableCustomRule extends DataObject |
||
| 25 | { |
||
| 26 | private static $condition_options = [ |
||
|
|
|||
| 27 | 'IsBlank' => 'Is blank', |
||
| 28 | 'IsNotBlank' => 'Is not blank', |
||
| 29 | 'HasValue' => 'Equals', |
||
| 30 | 'ValueNot' => 'Doesn\'t equal', |
||
| 31 | 'ValueLessThan' => 'Less than', |
||
| 32 | 'ValueLessThanEqual' => 'Less than or equal', |
||
| 33 | 'ValueGreaterThan' => 'Greater than', |
||
| 34 | 'ValueGreaterThanEqual' => 'Greater than or equal' |
||
| 35 | ]; |
||
| 36 | |||
| 37 | private static $db = [ |
||
| 38 | 'Display' => 'Enum("Show,Hide")', |
||
| 39 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
||
| 40 | 'FieldValue' => 'Varchar(255)' |
||
| 41 | ]; |
||
| 42 | |||
| 43 | private static $has_one = [ |
||
| 44 | 'Parent' => EditableFormField::class, |
||
| 45 | 'ConditionField' => EditableFormField::class |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Built in extensions required |
||
| 50 | * |
||
| 51 | * @config |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | 1 | private static $extensions = [ |
|
| 55 | Versioned::class . "('Stage', 'Live')" |
||
| 56 | 1 | ]; |
|
| 57 | 1 | ||
| 58 | private static $table_name = 'EditableCustomRule'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param Member $member |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function canDelete($member = null) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param Member $member |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | public function canEdit($member = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param Member $member |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public function canView($member = null) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Return whether a user can create an object of this type |
||
| 89 | * |
||
| 90 | * @param Member $member |
||
| 91 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Helper method to check the parent for this object |
||
| 108 | * |
||
| 109 | * @param array $args List of arguments passed to canCreate |
||
| 110 | * @return DataObject Some parent dataobject to inherit permissions from |
||
| 111 | */ |
||
| 112 | View Code Duplication | protected function getCanCreateContext($args) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param Member $member |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function canPublish($member = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param Member $member |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function canUnpublish($member = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Substitutes configured rule logic with it's JS equivalents and returns them as array elements |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | * @throws LogicException If the provided condition option was not able to be handled |
||
| 150 | */ |
||
| 151 | public function buildExpression() |
||
| 232 | |||
| 233 | 2 | /** |
|
| 234 | 2 | * Returns the opposite visibility function for the value of the initial visibility field, e.g. show/hide. This |
|
| 235 | 2 | * will toggle the "hide" class either way, which is handled by CSS. |
|
| 236 | * |
||
| 237 | 2 | * @param string $text |
|
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function toggleDisplayText($text) |
||
| 244 | } |
||
| 245 |
This check marks private properties in classes that are never used. Those properties can be removed.