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 |
||
| 22 | class EmailRecipientCondition extends DataObject |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * List of options |
||
| 26 | * |
||
| 27 | * @config |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $condition_options = [ |
||
|
|
|||
| 31 | 'IsBlank' => 'Is blank', |
||
| 32 | 'IsNotBlank' => 'Is not blank', |
||
| 33 | 'Equals' => 'Equals', |
||
| 34 | 'NotEquals' => "Doesn't equal", |
||
| 35 | 'ValueLessThan' => 'Less than', |
||
| 36 | 'ValueLessThanEqual' => 'Less than or equal', |
||
| 37 | 'ValueGreaterThan' => 'Greater than', |
||
| 38 | 'ValueGreaterThanEqual' => 'Greater than or equal' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private static $db = [ |
||
| 42 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
||
| 43 | 'ConditionValue' => 'Varchar' |
||
| 44 | ]; |
||
| 45 | |||
| 46 | private static $has_one = [ |
||
| 47 | 'Parent' => EmailRecipient::class, |
||
| 48 | 'ConditionField' => EditableFormField::class |
||
| 49 | ]; |
||
| 50 | |||
| 51 | private static $table_name = 'UserDefinedForm_EmailRecipientCondition'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * Determine if this rule matches the given condition |
||
| 56 | * |
||
| 57 | * @param $data |
||
| 58 | * |
||
| 59 | * @return bool|null |
||
| 60 | * @throws LogicException |
||
| 61 | */ |
||
| 62 | public function matches($data) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Return whether a user can create an object of this type |
||
| 107 | * |
||
| 108 | * @param Member $member |
||
| 109 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Helper method to check the parent for this object |
||
| 126 | * |
||
| 127 | * @param array $args List of arguments passed to canCreate |
||
| 128 | * @return SiteTree Parent page instance |
||
| 129 | */ |
||
| 130 | View Code Duplication | protected function getCanCreateContext($args) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param Member |
||
| 147 | * |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | public function canView($member = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Member |
||
| 157 | * |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | public function canEdit($member = null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param Member |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public function canDelete($member = null) |
||
| 174 | } |
||
| 175 |
This check marks private properties in classes that are never used. Those properties can be removed.