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 | View Code Duplication | class BlogCategory extends DataObject implements CategorisationObject |
|
15 | { |
||
16 | |||
17 | /** |
||
18 | * Use an exception code so that attempted writes can continue on |
||
19 | * duplicate errors. 600 is completely arbitrary. |
||
20 | * |
||
21 | * @const string |
||
22 | * This must be a string because ValidationException has decided we can't use int |
||
23 | */ |
||
24 | const DUPLICATE_EXCEPTION = "DUPLICATE"; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $db = array( |
||
30 | 'Title' => 'Varchar(255)', |
||
31 | ); |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private static $has_one = array( |
||
37 | 'Blog' => 'Blog', |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private static $belongs_many_many = array( |
||
44 | 'BlogPosts' => 'BlogPost', |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private static $extensions = array( |
||
51 | 'URLSegmentExtension', |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * @return DataList |
||
56 | */ |
||
57 | public function BlogPosts() |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function getCMSFields() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | protected function validate() |
||
104 | |||
105 | /** |
||
106 | * Returns a relative link to this category. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getLink() |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Inherits from the parent blog or can be overwritten using a DataExtension. |
||
118 | * |
||
119 | * @param null|Member $member |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function canView($member = null) |
||
133 | |||
134 | /** |
||
135 | * Inherits from the parent blog or can be overwritten using a DataExtension. |
||
136 | * |
||
137 | * @param null|Member $member |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function canCreate($member = null) |
||
153 | |||
154 | /** |
||
155 | * Inherits from the parent blog or can be overwritten using a DataExtension. |
||
156 | * |
||
157 | * @param null|Member $member |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function canDelete($member = null) |
||
171 | |||
172 | /** |
||
173 | * Inherits from the parent blog or can be overwritten using a DataExtension. |
||
174 | * |
||
175 | * @param null|Member $member |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function canEdit($member = null) |
||
189 | } |
||
190 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.