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 |
||
35 | class ForumThread extends DataObject |
||
36 | { |
||
37 | /** @var array */ |
||
38 | private static $db = array( |
||
39 | "Title" => "Varchar(255)", |
||
40 | "NumViews" => "Int", |
||
41 | "IsSticky" => "Boolean", |
||
42 | "IsReadOnly" => "Boolean", |
||
43 | "IsGlobalSticky" => "Boolean" |
||
44 | ); |
||
45 | |||
46 | /** @var array */ |
||
47 | private static $has_one = array( |
||
48 | 'Forum' => 'Forum' |
||
49 | ); |
||
50 | |||
51 | /** @var array */ |
||
52 | private static $has_many = array( |
||
53 | 'Posts' => 'Post' |
||
54 | ); |
||
55 | |||
56 | /** @var array */ |
||
57 | private static $defaults = array( |
||
58 | 'NumViews' => 0, |
||
59 | 'IsSticky' => false, |
||
60 | 'IsReadOnly' => false, |
||
61 | 'IsGlobalSticky' => false |
||
62 | ); |
||
63 | |||
64 | /** @var array */ |
||
65 | private static $indexes = array( |
||
66 | 'IsSticky' => true, |
||
67 | 'IsGlobalSticky' => true |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * @var null|boolean Per-request cache, whether we should display signatures on a post. |
||
72 | */ |
||
73 | private static $cacheDisplaySignatures = null; |
||
74 | |||
75 | /** |
||
76 | * Check if the user can create new threads and add responses |
||
77 | * |
||
78 | * @param null|Member $member |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function canPost($member = null) |
||
90 | |||
91 | /** |
||
92 | * Check if user can moderate this thread |
||
93 | * |
||
94 | * @param null|Member $member |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function canModerate($member = null) |
||
106 | |||
107 | /** |
||
108 | * Check if user can view the thread |
||
109 | * |
||
110 | * @param null|Member $member |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function canView($member = null) |
||
122 | |||
123 | /** |
||
124 | * Hook up into moderation. |
||
125 | * |
||
126 | * @param null|Member $member |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function canEdit($member = null) |
||
138 | |||
139 | /** |
||
140 | * Hook up into moderation - users cannot delete their own posts/threads because |
||
141 | * we will loose history this way. |
||
142 | * |
||
143 | * @param null|Member $member |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function canDelete($member = null) |
||
155 | |||
156 | /** |
||
157 | * Hook up into canPost check |
||
158 | * |
||
159 | * @param null|Member $member |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function canCreate($member = null) |
||
171 | |||
172 | /** |
||
173 | * Are Forum Signatures on Member profiles allowed. |
||
174 | * This only needs to be checked once, so we cache the initial value once per-request. |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function getDisplaySignatures() |
||
189 | |||
190 | /** |
||
191 | * Get the latest post from this thread. Nicer way then using an control |
||
192 | * from the template |
||
193 | * |
||
194 | * @return Post |
||
195 | */ |
||
196 | public function getLatestPost() |
||
202 | |||
203 | /** |
||
204 | * Return the first post from the thread. Useful to working out the original author |
||
205 | * |
||
206 | * @return Post |
||
207 | */ |
||
208 | public function getFirstPost() |
||
214 | |||
215 | /** |
||
216 | * Return the number of posts in this thread. We could use count on |
||
217 | * the dataobject set but that is slower and causes a performance overhead |
||
218 | * |
||
219 | * @todo SS4 compat |
||
220 | * @return int |
||
221 | */ |
||
222 | View Code Duplication | public function getNumPosts() |
|
233 | |||
234 | /** |
||
235 | * Check if they have visited this thread before. If they haven't increment |
||
236 | * the NumViews value by 1 and set visited to true. |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function incNumViews() |
||
251 | |||
252 | /** |
||
253 | * Link to this forum thread |
||
254 | * |
||
255 | * @param string $action |
||
256 | * @param bool $showID |
||
257 | * |
||
258 | * @return String |
||
259 | */ |
||
260 | public function Link($action = "show", $showID = true) |
||
272 | |||
273 | /** |
||
274 | * Check to see if the user has subscribed to this thread |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function getHasSubscribed() |
||
284 | |||
285 | /** |
||
286 | * Before deleting the thread remove all the posts |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | public function onBeforeDelete() |
||
301 | |||
302 | /** |
||
303 | * Ensure the correct ForumID is applied to the record |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | public function onAfterWrite() |
||
320 | |||
321 | /** |
||
322 | * @return DBText |
||
323 | */ |
||
324 | public function getEscapedTitle() |
||
328 | } |
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.