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 | private static $db = array( |
||
|
|
|||
| 13 | "Content" => "Text", |
||
| 14 | "Status" => "Enum('Awaiting, Moderated, Rejected, Archived', 'Moderated')", |
||
| 15 | ); |
||
| 16 | |||
| 17 | private static $casting = array( |
||
| 18 | "Updated" => "SS_Datetime", |
||
| 19 | "RSSContent" => "HTMLText", |
||
| 20 | "RSSAuthor" => "Varchar", |
||
| 21 | "Content" => "HTMLText" |
||
| 22 | ); |
||
| 23 | |||
| 24 | private static $has_one = array( |
||
| 25 | "Author" => "Member", |
||
| 26 | "Thread" => "ForumThread", |
||
| 27 | "Forum" => "Forum" // denormalized data but used for read speed |
||
| 28 | ); |
||
| 29 | |||
| 30 | private static $has_many = array( |
||
| 31 | "Attachments" => "Post_Attachment" |
||
| 32 | ); |
||
| 33 | |||
| 34 | private static $summary_fields = array( |
||
| 35 | "Content.LimitWordCount" => "Summary", |
||
| 36 | "Created" => "Created", |
||
| 37 | "Status" => "Status", |
||
| 38 | "Thread.Title" => "Thread", |
||
| 39 | "Forum.Title" => "Forum" |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Update all the posts to have a forum ID of their thread ID. |
||
| 44 | */ |
||
| 45 | function requireDefaultRecords() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Before deleting a post make sure all attachments are also deleted |
||
| 62 | */ |
||
| 63 | function onBeforeDelete() { |
||
| 64 | parent::onBeforeDelete(); |
||
| 65 | |||
| 66 | if($attachments = $this->Attachments()) { |
||
| 67 | foreach($attachments as $file) { |
||
| 68 | $file->delete(); |
||
| 69 | $file->destroy(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Check if user can see the post |
||
| 76 | */ |
||
| 77 | function canView($member = null) { |
||
| 78 | if(!$member) $member = Member::currentUser(); |
||
| 79 | |||
| 80 | if($this->Author()->ForumStatus != 'Normal') { |
||
| 81 | if($this->AuthorID != $member->ID || $member->ForumStatus != 'Ghost') { |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this->Thread()->canView($member); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Check if user can edit the post (only if it's his own, or he's an admin user) |
||
| 91 | */ |
||
| 92 | function canEdit($member = null) { |
||
| 93 | if(!$member) $member = Member::currentUser(); |
||
| 94 | |||
| 95 | if($member) { |
||
| 96 | // Admins can always edit, regardless of thread/post ownership |
||
| 97 | if(Permission::checkMember($member, 'ADMIN')) return true; |
||
| 98 | |||
| 99 | // Otherwise check for thread permissions and ownership |
||
| 100 | if($this->Thread()->canPost($member) && $member->ID == $this->AuthorID) return true; |
||
| 101 | } |
||
| 102 | |||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Follow edit permissions for this, but additionally allow moderation even |
||
| 108 | * if the thread is marked as readonly. |
||
| 109 | */ |
||
| 110 | function canDelete($member = null) { |
||
| 111 | if(!$member) $member = Member::currentUser(); |
||
| 112 | if($this->canEdit($member)) { |
||
| 113 | return true; |
||
| 114 | } else { |
||
| 115 | return $this->Thread()->canModerate($member); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Check if user can add new posts - hook up into canPost. |
||
| 121 | */ |
||
| 122 | function canCreate($member = null) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns the absolute url rather then relative. Used in Post RSS Feed |
||
| 129 | * |
||
| 130 | * @return String |
||
| 131 | */ |
||
| 132 | function AbsoluteLink() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return the title of the post. Because we don't have to have the title |
||
| 138 | * on individual posts check with the topic |
||
| 139 | * |
||
| 140 | * @return String |
||
| 141 | */ |
||
| 142 | function getTitle() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return the last edited date, if it's different from created |
||
| 148 | */ |
||
| 149 | function getUpdated() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Is this post the first post in the thread. Check if their is a post with an ID less |
||
| 155 | * than the one of this post in the same thread |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function isFirstPost() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return a link to edit this post. |
||
| 171 | * |
||
| 172 | * @return String |
||
| 173 | */ |
||
| 174 | function EditLink() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Return a link to delete this post. |
||
| 184 | * |
||
| 185 | * If the member is an admin of this forum, (ADMIN permissions |
||
| 186 | * or a moderator) then they can delete the post. |
||
| 187 | * |
||
| 188 | * @return String |
||
| 189 | */ |
||
| 190 | function DeleteLink() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return a link to the reply form. Permission checking is handled on the actual URL |
||
| 206 | * and not on this function |
||
| 207 | * |
||
| 208 | * @return String |
||
| 209 | */ |
||
| 210 | function ReplyLink() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Return a link to the post view. |
||
| 218 | * |
||
| 219 | * @return String |
||
| 220 | */ |
||
| 221 | function ShowLink() { |
||
| 226 | |||
| 227 | View Code Duplication | public function BanLink() { |
|
| 235 | |||
| 236 | View Code Duplication | public function GhostLink() { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Return the parsed content and the information for the |
||
| 247 | * RSS feed |
||
| 248 | */ |
||
| 249 | function getRSSContent() { |
||
| 252 | |||
| 253 | |||
| 254 | function getRSSAuthor() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Return a link to show this post |
||
| 262 | * |
||
| 263 | * @return String |
||
| 264 | */ |
||
| 265 | function Link($action = "show") { |
||
| 284 | } |
||
| 285 | |||
| 338 |