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 Comment 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 Comment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Comment extends DataObject { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private static $db = array( |
||
|
|
|||
| 29 | 'Name' => 'Varchar(200)', |
||
| 30 | 'Comment' => 'Text', |
||
| 31 | 'Email' => 'Varchar(200)', |
||
| 32 | 'URL' => 'Varchar(255)', |
||
| 33 | 'BaseClass' => 'Varchar(200)', |
||
| 34 | 'Moderated' => 'Boolean(0)', |
||
| 35 | 'IsSpam' => 'Boolean(0)', |
||
| 36 | 'ParentID' => 'Int', |
||
| 37 | 'AllowHtml' => 'Boolean', |
||
| 38 | 'SecretToken' => 'Varchar(255)', |
||
| 39 | 'Depth' => 'Int', |
||
| 40 | ); |
||
| 41 | |||
| 42 | private static $has_one = array( |
||
| 43 | "Author" => "Member", |
||
| 44 | "ParentComment" => "Comment", |
||
| 45 | ); |
||
| 46 | |||
| 47 | private static $has_many = array( |
||
| 48 | "ChildComments" => "Comment" |
||
| 49 | ); |
||
| 50 | |||
| 51 | private static $default_sort = '"Created" DESC'; |
||
| 52 | |||
| 53 | private static $defaults = array( |
||
| 54 | 'Moderated' => 0, |
||
| 55 | 'IsSpam' => 0, |
||
| 56 | ); |
||
| 57 | |||
| 58 | private static $casting = array( |
||
| 59 | 'Title' => 'Varchar', |
||
| 60 | 'ParentTitle' => 'Varchar', |
||
| 61 | 'ParentClassName' => 'Varchar', |
||
| 62 | 'AuthorName' => 'Varchar', |
||
| 63 | 'RSSName' => 'Varchar', |
||
| 64 | 'DeleteLink' => 'Varchar', |
||
| 65 | 'SpamLink' => 'Varchar', |
||
| 66 | 'HamLink' => 'Varchar', |
||
| 67 | 'ApproveLink' => 'Varchar', |
||
| 68 | 'Permalink' => 'Varchar', |
||
| 69 | ); |
||
| 70 | |||
| 71 | private static $searchable_fields = array( |
||
| 72 | 'Name', |
||
| 73 | 'Email', |
||
| 74 | 'Comment', |
||
| 75 | 'Created', |
||
| 76 | 'BaseClass', |
||
| 77 | ); |
||
| 78 | |||
| 79 | private static $summary_fields = array( |
||
| 80 | 'Name' => 'Submitted By', |
||
| 81 | 'Email' => 'Email', |
||
| 82 | 'Comment.LimitWordCount' => 'Comment', |
||
| 83 | 'Created' => 'Date Posted', |
||
| 84 | 'ParentTitle' => 'Post', |
||
| 85 | 'IsSpam' => 'Is Spam', |
||
| 86 | ); |
||
| 87 | |||
| 88 | private static $field_labels = array( |
||
| 89 | 'Author' => 'Author Member', |
||
| 90 | ); |
||
| 91 | |||
| 92 | public function onBeforeWrite() { |
||
| 93 | parent::onBeforeWrite(); |
||
| 94 | |||
| 95 | // Sanitize HTML, because its expected to be passed to the template unescaped later |
||
| 96 | if($this->AllowHtml) { |
||
| 97 | $this->Comment = $this->purifyHtml($this->Comment); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Check comment depth |
||
| 101 | $this->updateDepth(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function onBeforeDelete() { |
||
| 105 | parent::onBeforeDelete(); |
||
| 106 | |||
| 107 | // Delete all children |
||
| 108 | foreach($this->ChildComments() as $comment) { |
||
| 109 | $comment->delete(); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return Comment_SecurityToken |
||
| 115 | */ |
||
| 116 | public function getSecurityToken() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Migrates the old {@link PageComment} objects to {@link Comment} |
||
| 122 | */ |
||
| 123 | public function requireDefaultRecords() { |
||
| 124 | parent::requireDefaultRecords(); |
||
| 125 | |||
| 126 | if(DB::getConn()->hasTable('PageComment')) { |
||
| 127 | $comments = DB::query('SELECT * FROM "PageComment"'); |
||
| 128 | |||
| 129 | if($comments) { |
||
| 130 | while($pageComment = $comments->nextRecord()) { |
||
| 131 | // create a new comment from the older page comment |
||
| 132 | $comment = new Comment(); |
||
| 133 | $comment->update($pageComment); |
||
| 134 | |||
| 135 | // set the variables which have changed |
||
| 136 | $comment->BaseClass = 'SiteTree'; |
||
| 137 | $comment->URL = (isset($pageComment['CommenterURL'])) ? $pageComment['CommenterURL'] : ''; |
||
| 138 | if((int) $pageComment['NeedsModeration'] == 0) $comment->Moderated = true; |
||
| 139 | |||
| 140 | $comment->write(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | DB::alteration_message('Migrated PageComment to Comment', 'changed'); |
||
| 145 | DB::getConn()->dontRequireTable('PageComment'); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return a link to this comment |
||
| 151 | * |
||
| 152 | * @param string $action |
||
| 153 | * |
||
| 154 | * @return string link to this comment. |
||
| 155 | */ |
||
| 156 | public function Link($action = '') { |
||
| 157 | if($parent = $this->getParent()) { |
||
| 158 | return $parent->Link($action) . '#' . $this->Permalink(); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the permalink for this {@link Comment}. Inserted into |
||
| 164 | * the ID tag of the comment |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function Permalink() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Translate the form field labels for the CMS administration |
||
| 175 | * |
||
| 176 | * @param boolean $includerelations |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | public function fieldLabels($includerelations = true) { |
||
| 181 | $labels = parent::fieldLabels($includerelations); |
||
| 182 | |||
| 183 | $labels['Name'] = _t('Comment.NAME', 'Author Name'); |
||
| 184 | $labels['Comment'] = _t('Comment.COMMENT', 'Comment'); |
||
| 185 | $labels['Email'] = _t('Comment.EMAIL', 'Email'); |
||
| 186 | $labels['URL'] = _t('Comment.URL', 'URL'); |
||
| 187 | $labels['IsSpam'] = _t('Comment.ISSPAM', 'Spam?'); |
||
| 188 | $labels['Moderated'] = _t('Comment.MODERATED', 'Moderated?'); |
||
| 189 | $labels['ParentTitle'] = _t('Comment.PARENTTITLE', 'Parent'); |
||
| 190 | $labels['Created'] = _t('Comment.CREATED', 'Date posted'); |
||
| 191 | |||
| 192 | return $labels; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the commenting option |
||
| 197 | * |
||
| 198 | * @param string $key |
||
| 199 | * |
||
| 200 | * @return mixed Result if the setting is available, or null otherwise |
||
| 201 | */ |
||
| 202 | public function getOption($key) { |
||
| 203 | // If possible use the current record |
||
| 204 | $record = $this->getParent(); |
||
| 205 | |||
| 206 | if(!$record && $this->BaseClass) { |
||
| 207 | // Otherwise a singleton of that record |
||
| 208 | $record = singleton($this->BaseClass); |
||
| 209 | } |
||
| 210 | else if(!$record) { |
||
| 211 | // Otherwise just use the default options |
||
| 212 | $record = singleton('CommentsExtension'); |
||
| 213 | } |
||
| 214 | |||
| 215 | return ($record->hasMethod('getCommentsOption')) ? $record->getCommentsOption($key) : null; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns the parent {@link DataObject} this comment is attached too |
||
| 220 | * |
||
| 221 | * @return DataObject |
||
| 222 | */ |
||
| 223 | public function getParent() { |
||
| 224 | return $this->BaseClass && $this->ParentID |
||
| 225 | ? DataObject::get_by_id($this->BaseClass, $this->ParentID, true) |
||
| 226 | : null; |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns a string to help identify the parent of the comment |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getParentTitle() { |
||
| 236 | if($parent = $this->getParent()) { |
||
| 237 | return $parent->Title ?: ($parent->ClassName . ' #' . $parent->ID); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Comment-parent classnames obviously vary, return the parent classname |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getParentClassName() { |
||
| 249 | |||
| 250 | public function castingHelper($field) { |
||
| 251 | // Safely escape the comment |
||
| 252 | if($field === 'EscapedComment') { |
||
| 253 | return $this->AllowHtml ? 'HTMLText' : 'Text'; |
||
| 254 | } |
||
| 255 | return parent::castingHelper($field); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Content to be safely escaped on the frontend |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getEscapedComment() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Return whether this comment is a preview (has not been written to the db) |
||
| 269 | * |
||
| 270 | * @return boolean |
||
| 271 | */ |
||
| 272 | public function isPreview() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @todo needs to compare to the new {@link Commenting} configuration API |
||
| 278 | * |
||
| 279 | * @param Member $member |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function canCreate($member = null) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Checks for association with a page, and {@link SiteTree->ProvidePermission} |
||
| 289 | * flag being set to true. |
||
| 290 | * |
||
| 291 | * @param Member $member |
||
| 292 | * |
||
| 293 | * @return Boolean |
||
| 294 | */ |
||
| 295 | public function canView($member = null) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Checks if the comment can be edited. |
||
| 318 | * |
||
| 319 | * @param null|int|Member $member |
||
| 320 | * |
||
| 321 | * @return Boolean |
||
| 322 | */ |
||
| 323 | public function canEdit($member = null) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Checks if the comment can be deleted. |
||
| 348 | * |
||
| 349 | * @param null|int|Member $member |
||
| 350 | * |
||
| 351 | * @return Boolean |
||
| 352 | */ |
||
| 353 | public function canDelete($member = null) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Resolves Member object. |
||
| 370 | * |
||
| 371 | * @param Member|int|null $member |
||
| 372 | * @return Member|null |
||
| 373 | */ |
||
| 374 | protected function getMember($member = null) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Return the authors name for the comment |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function getAuthorName() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Generate a secure admin-action link authorised for the specified member |
||
| 401 | * |
||
| 402 | * @param string $action An action on CommentingController to link to |
||
| 403 | * @param Member $member The member authorised to invoke this action |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | protected function actionLink($action, $member = null) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Link to delete this comment |
||
| 425 | * |
||
| 426 | * @param Member $member |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function DeleteLink($member = null) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Link to mark as spam |
||
| 438 | * |
||
| 439 | * @param Member $member |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function SpamLink($member = null) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Link to mark as not-spam (ham) |
||
| 451 | * |
||
| 452 | * @param Member $member |
||
| 453 | * |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function HamLink($member = null) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Link to approve this comment |
||
| 464 | * |
||
| 465 | * @param Member $member |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function ApproveLink($member = null) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Mark this comment as spam |
||
| 477 | */ |
||
| 478 | public function markSpam() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Mark this comment as approved |
||
| 487 | */ |
||
| 488 | public function markApproved() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Mark this comment as unapproved |
||
| 497 | */ |
||
| 498 | public function markUnapproved() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function SpamClass() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public function getTitle() { |
||
| 531 | |||
| 532 | /* |
||
| 533 | * Modify the default fields shown to the user |
||
| 534 | */ |
||
| 535 | public function getCMSFields() { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param String $dirtyHtml |
||
| 610 | * |
||
| 611 | * @return String |
||
| 612 | */ |
||
| 613 | public function purifyHtml($dirtyHtml) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
| 620 | */ |
||
| 621 | public function getHtmlPurifierService() { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Calculate the Gravatar link from the email address |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public function Gravatar() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Determine if replies are enabled for this instance |
||
| 658 | * |
||
| 659 | * @return boolean |
||
| 660 | */ |
||
| 661 | public function getRepliesEnabled() { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Returns the list of all replies |
||
| 675 | * |
||
| 676 | * @return SS_List |
||
| 677 | */ |
||
| 678 | public function AllReplies() { |
||
| 694 | |||
| 695 | public function ShowReplyToForm() { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
| 705 | * |
||
| 706 | * @return SS_List |
||
| 707 | */ |
||
| 708 | public function Replies() { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
| 737 | * |
||
| 738 | * @return PaginatedList |
||
| 739 | */ |
||
| 740 | View Code Duplication | public function PagedReplies() { |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Generate a reply form for this comment |
||
| 754 | * |
||
| 755 | * @return Form |
||
| 756 | */ |
||
| 757 | public function ReplyForm() { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Refresh of this comment in the hierarchy |
||
| 780 | */ |
||
| 781 | public function updateDepth() { |
||
| 790 | } |
||
| 791 | |||
| 892 |