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 |
||
10 | class EditableCustomRule extends DataObject { |
||
|
|||
11 | |||
12 | private static $condition_options = array( |
||
13 | "IsBlank" => "Is blank", |
||
14 | "IsNotBlank" => "Is not blank", |
||
15 | "HasValue" => "Equals", |
||
16 | "ValueNot" => "Doesn't equal", |
||
17 | "ValueLessThan" => "Less than", |
||
18 | "ValueLessThanEqual" => "Less than or equal", |
||
19 | "ValueGreaterThan" => "Greater than", |
||
20 | "ValueGreaterThanEqual" => "Greater than or equal" |
||
21 | ); |
||
22 | |||
23 | private static $db = array( |
||
24 | 'Display' => 'Enum("Show,Hide")', |
||
25 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
||
26 | 'FieldValue' => 'Varchar' |
||
27 | ); |
||
28 | |||
29 | private static $has_one = array( |
||
30 | 'Parent' => 'EditableFormField', |
||
31 | 'ConditionField' => 'EditableFormField' |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * Built in extensions required |
||
36 | * |
||
37 | * @config |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $extensions = array( |
||
41 | "Versioned('Stage', 'Live')" |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * Publish this custom rule to the live site |
||
46 | * |
||
47 | * Wrapper for the {@link Versioned} publish function |
||
48 | */ |
||
49 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
52 | |||
53 | /** |
||
54 | * Delete this custom rule from a given stage |
||
55 | * |
||
56 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
57 | */ |
||
58 | public function doDeleteFromStage($stage) { |
||
61 | |||
62 | |||
63 | /** |
||
64 | * @param Member $member |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function canDelete($member = null) { |
||
70 | |||
71 | /** |
||
72 | * @param Member $member |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function canEdit($member = null) { |
||
78 | |||
79 | /** |
||
80 | * @param Member $member |
||
81 | * @return bool |
||
82 | */ |
||
83 | 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) { |
|
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) { |
|
124 | |||
125 | /** |
||
126 | * @param Member $member |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function canPublish($member = null) { |
||
132 | |||
133 | /** |
||
134 | * @param Member $member |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function canUnpublish($member = null) { |
||
140 | } |
||
141 |
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.