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