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:
Complex classes like Post often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Post extends DataObject |
||
35 | { |
||
36 | /** @var array */ |
||
37 | private static $db = array( |
||
38 | "Content" => "Text", |
||
39 | "Status" => "Enum('Awaiting, Moderated, Rejected, Archived', 'Moderated')", |
||
40 | ); |
||
41 | |||
42 | /** @var array */ |
||
43 | private static $has_one = array( |
||
44 | "Author" => Member::class, |
||
45 | "Thread" => ForumThread::class, |
||
46 | "Forum" => Forum::class |
||
47 | ); |
||
48 | |||
49 | /** @var array */ |
||
50 | private static $has_many = array( |
||
51 | "Attachments" => PostAttachment::class |
||
52 | ); |
||
53 | |||
54 | /** @var array */ |
||
55 | private static $casting = array( |
||
56 | "Updated" => "Datetime", |
||
57 | "RSSContent" => "HTMLText", |
||
58 | "RSSAuthor" => "Varchar", |
||
59 | "Content" => "HTMLText" |
||
60 | ); |
||
61 | |||
62 | /** @var array */ |
||
63 | private static $summary_fields = array( |
||
64 | "Content.LimitWordCount" => "Summary", |
||
65 | "Created" => "Created", |
||
66 | "Status" => "Status", |
||
67 | "Thread.Title" => "Thread", |
||
68 | "Forum.Title" => "Forum" |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * Update all the posts to have a forum ID of their thread ID. |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | public function requireDefaultRecords() |
||
93 | |||
94 | /** |
||
95 | * Before deleting a post make sure all attachments are also deleted |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function onBeforeDelete() |
||
110 | |||
111 | /** |
||
112 | * Check if user can see the post |
||
113 | * |
||
114 | * @param null|Member $member |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function canView($member = null) |
||
132 | |||
133 | /** |
||
134 | * Check if user can edit the post (only if it's his own, or he's an admin user) |
||
135 | * |
||
136 | * @param null|Member $member |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function canEdit($member = null) |
||
160 | |||
161 | /** |
||
162 | * Follow edit permissions for this, but additionally allow moderation even |
||
163 | * if the thread is marked as readonly. |
||
164 | * |
||
165 | * @param null|Member $member |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function canDelete($member = null) |
||
181 | |||
182 | /** |
||
183 | * Check if user can add new posts - hook up into canPost. |
||
184 | * |
||
185 | * @param null|Member $member |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function canCreate($member = null) |
||
197 | |||
198 | /** |
||
199 | * Returns the absolute url rather then relative. Used in Post RSS Feed |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function AbsoluteLink() |
||
207 | |||
208 | /** |
||
209 | * Return the title of the post. Because we don't have to have the title |
||
210 | * on individual posts check with the topic |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getTitle() |
||
229 | |||
230 | /** |
||
231 | * Return the last edited date, if it's different from created |
||
232 | * |
||
233 | * @return DBDatetime|bool |
||
234 | */ |
||
235 | public function getUpdated() |
||
243 | |||
244 | /** |
||
245 | * Is this post the first post in the thread. Check if their is a post with an ID less |
||
246 | * than the one of this post in the same thread |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function isFirstPost() |
||
265 | |||
266 | /** |
||
267 | * Return a link to edit this post. |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | public function EditLink() |
||
281 | |||
282 | /** |
||
283 | * Return a link to delete this post. |
||
284 | * |
||
285 | * If the member is an admin of this forum, (ADMIN permissions |
||
286 | * or a moderator) then they can delete the post. |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function DeleteLink() |
||
305 | |||
306 | /** |
||
307 | * Return a link to the reply form. Permission checking is handled on the actual URL |
||
308 | * and not on this function |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function ReplyLink() |
||
318 | |||
319 | /** |
||
320 | * Return a link to the post view. |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | public function ShowLink() |
||
330 | |||
331 | /** |
||
332 | * Return a link to mark this post as spam. Used for the SpamProtection module |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function MarkAsSpamLink() |
||
354 | |||
355 | /** |
||
356 | * Returns a ban link |
||
357 | * |
||
358 | * @return bool|string |
||
359 | */ |
||
360 | public function BanLink() |
||
371 | |||
372 | /** |
||
373 | * Returns a ghost link |
||
374 | * |
||
375 | * @return bool|string |
||
376 | */ |
||
377 | public function GhostLink() |
||
389 | |||
390 | /** |
||
391 | * Return the parsed content and the information for the RSS feed |
||
392 | * |
||
393 | * @return DBHTMLText |
||
394 | */ |
||
395 | public function getRSSContent() |
||
399 | |||
400 | /** |
||
401 | * Get RSS Author |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | public function getRSSAuthor() |
||
411 | |||
412 | /** |
||
413 | * Return a link to show this post |
||
414 | * |
||
415 | * @param string $action |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public function Link($action = "show") |
||
441 | } |
||
442 | |||
443 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: