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 |
||
| 10 | class Post extends DataObject |
||
| 11 | { |
||
| 12 | |||
| 13 | private static $db = array( |
||
|
|
|||
| 14 | "Content" => "Text", |
||
| 15 | "Status" => "Enum('Awaiting, Moderated, Rejected, Archived', 'Moderated')", |
||
| 16 | ); |
||
| 17 | |||
| 18 | private static $casting = array( |
||
| 19 | "Updated" => "SS_Datetime", |
||
| 20 | "RSSContent" => "HTMLText", |
||
| 21 | "RSSAuthor" => "Varchar", |
||
| 22 | "Content" => "HTMLText" |
||
| 23 | ); |
||
| 24 | |||
| 25 | private static $has_one = array( |
||
| 26 | "Author" => "Member", |
||
| 27 | "Thread" => "ForumThread", |
||
| 28 | "Forum" => "Forum" // denormalized data but used for read speed |
||
| 29 | ); |
||
| 30 | |||
| 31 | private static $has_many = array( |
||
| 32 | "Attachments" => "Post_Attachment" |
||
| 33 | ); |
||
| 34 | |||
| 35 | private static $summary_fields = array( |
||
| 36 | "Content.LimitWordCount" => "Summary", |
||
| 37 | "Created" => "Created", |
||
| 38 | "Status" => "Status", |
||
| 39 | "Thread.Title" => "Thread", |
||
| 40 | "Forum.Title" => "Forum" |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Update all the posts to have a forum ID of their thread ID. |
||
| 45 | */ |
||
| 46 | public function requireDefaultRecords() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Before deleting a post make sure all attachments are also deleted |
||
| 64 | */ |
||
| 65 | public function onBeforeDelete() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Check if user can see the post |
||
| 79 | */ |
||
| 80 | public function canView($member = null) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check if user can edit the post (only if it's his own, or he's an admin user) |
||
| 97 | */ |
||
| 98 | public function canEdit($member = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Follow edit permissions for this, but additionally allow moderation even |
||
| 121 | * if the thread is marked as readonly. |
||
| 122 | */ |
||
| 123 | public function canDelete($member = null) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Check if user can add new posts - hook up into canPost. |
||
| 137 | */ |
||
| 138 | public function canCreate($member = null) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the absolute url rather then relative. Used in Post RSS Feed |
||
| 148 | * |
||
| 149 | * @return String |
||
| 150 | */ |
||
| 151 | public function AbsoluteLink() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return the title of the post. Because we don't have to have the title |
||
| 158 | * on individual posts check with the topic |
||
| 159 | * |
||
| 160 | * @return String |
||
| 161 | */ |
||
| 162 | public function getTitle() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the last edited date, if it's different from created |
||
| 169 | */ |
||
| 170 | public function getUpdated() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Is this post the first post in the thread. Check if their is a post with an ID less |
||
| 179 | * than the one of this post in the same thread |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function isFirstPost() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return a link to edit this post. |
||
| 198 | * |
||
| 199 | * @return String |
||
| 200 | */ |
||
| 201 | public function EditLink() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return a link to delete this post. |
||
| 212 | * |
||
| 213 | * If the member is an admin of this forum, (ADMIN permissions |
||
| 214 | * or a moderator) then they can delete the post. |
||
| 215 | * |
||
| 216 | * @return String |
||
| 217 | */ |
||
| 218 | public function DeleteLink() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return a link to the reply form. Permission checking is handled on the actual URL |
||
| 235 | * and not on this function |
||
| 236 | * |
||
| 237 | * @return String |
||
| 238 | */ |
||
| 239 | public function ReplyLink() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Return a link to the post view. |
||
| 248 | * |
||
| 249 | * @return String |
||
| 250 | */ |
||
| 251 | public function ShowLink() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return a link to mark this post as spam. |
||
| 260 | * used for the spamprotection module |
||
| 261 | * |
||
| 262 | * @return String |
||
| 263 | */ |
||
| 264 | public function MarkAsSpamLink() |
||
| 280 | |||
| 281 | View Code Duplication | public function BanLink() |
|
| 290 | |||
| 291 | View Code Duplication | public function GhostLink() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Return the parsed content and the information for the |
||
| 303 | * RSS feed |
||
| 304 | */ |
||
| 305 | public function getRSSContent() |
||
| 309 | |||
| 310 | |||
| 311 | public function getRSSAuthor() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Return a link to show this post |
||
| 320 | * |
||
| 321 | * @return String |
||
| 322 | */ |
||
| 323 | public function Link($action = "show") |
||
| 343 | } |
||
| 344 | |||
| 405 |