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 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $db = array( |
||
| 30 | 'Name' => 'Varchar(200)', |
||
| 31 | 'Comment' => 'Text', |
||
| 32 | 'Email' => 'Varchar(200)', |
||
| 33 | 'URL' => 'Varchar(255)', |
||
| 34 | 'BaseClass' => 'Varchar(200)', |
||
| 35 | 'Moderated' => 'Boolean(0)', |
||
| 36 | 'IsSpam' => 'Boolean(0)', |
||
| 37 | 'ParentID' => 'Int', |
||
| 38 | 'AllowHtml' => 'Boolean', |
||
| 39 | 'SecretToken' => 'Varchar(255)', |
||
| 40 | 'Depth' => 'Int', |
||
| 41 | ); |
||
| 42 | |||
| 43 | private static $has_one = array( |
||
| 44 | "Author" => "Member", |
||
| 45 | "ParentComment" => "Comment", |
||
| 46 | ); |
||
| 47 | |||
| 48 | private static $has_many = array( |
||
| 49 | "ChildComments" => "Comment" |
||
| 50 | ); |
||
| 51 | |||
| 52 | private static $default_sort = '"Created" DESC'; |
||
| 53 | |||
| 54 | private static $defaults = array( |
||
| 55 | 'Moderated' => 0, |
||
| 56 | 'IsSpam' => 0, |
||
| 57 | ); |
||
| 58 | |||
| 59 | private static $casting = array( |
||
| 60 | 'Title' => 'Varchar', |
||
| 61 | 'ParentTitle' => 'Varchar', |
||
| 62 | 'ParentClassName' => 'Varchar', |
||
| 63 | 'AuthorName' => 'Varchar', |
||
| 64 | 'RSSName' => 'Varchar', |
||
| 65 | 'DeleteLink' => 'Varchar', |
||
| 66 | 'SpamLink' => 'Varchar', |
||
| 67 | 'HamLink' => 'Varchar', |
||
| 68 | 'ApproveLink' => 'Varchar', |
||
| 69 | 'Permalink' => 'Varchar', |
||
| 70 | ); |
||
| 71 | |||
| 72 | private static $searchable_fields = array( |
||
| 73 | 'Name', |
||
| 74 | 'Email', |
||
| 75 | 'Comment', |
||
| 76 | 'Created', |
||
| 77 | 'BaseClass', |
||
| 78 | ); |
||
| 79 | |||
| 80 | private static $summary_fields = array( |
||
| 81 | 'Name' => 'Submitted By', |
||
| 82 | 'Email' => 'Email', |
||
| 83 | 'Comment.LimitWordCount' => 'Comment', |
||
| 84 | 'Created' => 'Date Posted', |
||
| 85 | 'ParentTitle' => 'Post', |
||
| 86 | 'IsSpam' => 'Is Spam', |
||
| 87 | ); |
||
| 88 | |||
| 89 | private static $field_labels = array( |
||
| 90 | 'Author' => 'Author Member', |
||
| 91 | ); |
||
| 92 | |||
| 93 | public function onBeforeWrite() |
||
| 94 | { |
||
| 95 | parent::onBeforeWrite(); |
||
| 96 | |||
| 97 | // Sanitize HTML, because its expected to be passed to the template unescaped later |
||
| 98 | if ($this->AllowHtml) { |
||
| 99 | $this->Comment = $this->purifyHtml($this->Comment); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Check comment depth |
||
| 103 | $this->updateDepth(); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function onBeforeDelete() |
||
| 107 | { |
||
| 108 | parent::onBeforeDelete(); |
||
| 109 | |||
| 110 | // Delete all children |
||
| 111 | foreach ($this->ChildComments() as $comment) { |
||
| 112 | $comment->delete(); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return Comment_SecurityToken |
||
| 118 | */ |
||
| 119 | public function getSecurityToken() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Migrates the old {@link PageComment} objects to {@link Comment} |
||
| 126 | */ |
||
| 127 | public function requireDefaultRecords() |
||
| 128 | { |
||
| 129 | parent::requireDefaultRecords(); |
||
| 130 | |||
| 131 | if (DB::getConn()->hasTable('PageComment')) { |
||
| 132 | $comments = DB::query('SELECT * FROM "PageComment"'); |
||
| 133 | |||
| 134 | if ($comments) { |
||
| 135 | while ($pageComment = $comments->nextRecord()) { |
||
| 136 | // create a new comment from the older page comment |
||
| 137 | $comment = new Comment(); |
||
| 138 | $comment->update($pageComment); |
||
| 139 | |||
| 140 | // set the variables which have changed |
||
| 141 | $comment->BaseClass = 'SiteTree'; |
||
| 142 | $comment->URL = (isset($pageComment['CommenterURL'])) ? $pageComment['CommenterURL'] : ''; |
||
| 143 | if ((int) $pageComment['NeedsModeration'] == 0) { |
||
| 144 | $comment->Moderated = true; |
||
| 145 | } |
||
| 146 | |||
| 147 | $comment->write(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | DB::alteration_message('Migrated PageComment to Comment', 'changed'); |
||
| 152 | DB::getConn()->dontRequireTable('PageComment'); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return a link to this comment |
||
| 158 | * |
||
| 159 | * @param string $action |
||
| 160 | * |
||
| 161 | * @return string link to this comment. |
||
| 162 | */ |
||
| 163 | public function Link($action = '') |
||
| 164 | { |
||
| 165 | if ($parent = $this->getParent()) { |
||
| 166 | return $parent->Link($action) . '#' . $this->Permalink(); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the permalink for this {@link Comment}. Inserted into |
||
| 172 | * the ID tag of the comment |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function Permalink() |
||
| 177 | { |
||
| 178 | $prefix = $this->getOption('comment_permalink_prefix'); |
||
| 179 | return $prefix . $this->ID; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Translate the form field labels for the CMS administration |
||
| 184 | * |
||
| 185 | * @param boolean $includerelations |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function fieldLabels($includerelations = true) |
||
| 190 | { |
||
| 191 | $labels = parent::fieldLabels($includerelations); |
||
| 192 | |||
| 193 | $labels['Name'] = _t('Comment.NAME', 'Author Name'); |
||
| 194 | $labels['Comment'] = _t('Comment.COMMENT', 'Comment'); |
||
| 195 | $labels['Email'] = _t('Comment.EMAIL', 'Email'); |
||
| 196 | $labels['URL'] = _t('Comment.URL', 'URL'); |
||
| 197 | $labels['IsSpam'] = _t('Comment.ISSPAM', 'Spam?'); |
||
| 198 | $labels['Moderated'] = _t('Comment.MODERATED', 'Moderated?'); |
||
| 199 | $labels['ParentTitle'] = _t('Comment.PARENTTITLE', 'Parent'); |
||
| 200 | $labels['Created'] = _t('Comment.CREATED', 'Date posted'); |
||
| 201 | |||
| 202 | return $labels; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get the commenting option |
||
| 207 | * |
||
| 208 | * @param string $key |
||
| 209 | * |
||
| 210 | * @return mixed Result if the setting is available, or null otherwise |
||
| 211 | */ |
||
| 212 | public function getOption($key) |
||
| 213 | { |
||
| 214 | // If possible use the current record |
||
| 215 | $record = $this->getParent(); |
||
| 216 | |||
| 217 | if (!$record && $this->BaseClass) { |
||
| 218 | // Otherwise a singleton of that record |
||
| 219 | $record = singleton($this->BaseClass); |
||
| 220 | } elseif (!$record) { |
||
| 221 | // Otherwise just use the default options |
||
| 222 | $record = singleton('CommentsExtension'); |
||
| 223 | } |
||
| 224 | |||
| 225 | return ($record->hasMethod('getCommentsOption')) ? $record->getCommentsOption($key) : null; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the parent {@link DataObject} this comment is attached too |
||
| 230 | * |
||
| 231 | * @return DataObject |
||
| 232 | */ |
||
| 233 | public function getParent() |
||
| 234 | { |
||
| 235 | return $this->BaseClass && $this->ParentID |
||
| 236 | ? DataObject::get_by_id($this->BaseClass, $this->ParentID, true) |
||
| 237 | : null; |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns a string to help identify the parent of the comment |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getParentTitle() |
||
| 247 | { |
||
| 248 | if ($parent = $this->getParent()) { |
||
| 249 | return $parent->Title ?: ($parent->ClassName . ' #' . $parent->ID); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Comment-parent classnames obviously vary, return the parent classname |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getParentClassName() |
||
| 262 | |||
| 263 | public function castingHelper($field) |
||
| 264 | { |
||
| 265 | // Safely escape the comment |
||
| 266 | if ($field === 'EscapedComment') { |
||
| 267 | return $this->AllowHtml ? 'HTMLText' : 'Text'; |
||
| 268 | } |
||
| 269 | return parent::castingHelper($field); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Content to be safely escaped on the frontend |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getEscapedComment() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Return whether this comment is a preview (has not been written to the db) |
||
| 284 | * |
||
| 285 | * @return boolean |
||
| 286 | */ |
||
| 287 | public function isPreview() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @todo needs to compare to the new {@link Commenting} configuration API |
||
| 294 | * |
||
| 295 | * @param Member $member |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function canCreate($member = null) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Checks for association with a page, and {@link SiteTree->ProvidePermission} |
||
| 306 | * flag being set to true. |
||
| 307 | * |
||
| 308 | * @param Member $member |
||
| 309 | * |
||
| 310 | * @return Boolean |
||
| 311 | */ |
||
| 312 | public function canView($member = null) |
||
| 313 | { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Checks if the comment can be edited. |
||
| 336 | * |
||
| 337 | * @param null|int|Member $member |
||
| 338 | * |
||
| 339 | * @return Boolean |
||
| 340 | */ |
||
| 341 | public function canEdit($member = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Checks if the comment can be deleted. |
||
| 367 | * |
||
| 368 | * @param null|int|Member $member |
||
| 369 | * |
||
| 370 | * @return Boolean |
||
| 371 | */ |
||
| 372 | public function canDelete($member = null) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Resolves Member object. |
||
| 390 | * |
||
| 391 | * @param Member|int|null $member |
||
| 392 | * @return Member|null |
||
| 393 | */ |
||
| 394 | protected function getMember($member = null) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Return the authors name for the comment |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | public function getAuthorName() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Generate a secure admin-action link authorised for the specified member |
||
| 423 | * |
||
| 424 | * @param string $action An action on CommentingController to link to |
||
| 425 | * @param Member $member The member authorised to invoke this action |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | protected function actionLink($action, $member = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Link to delete this comment |
||
| 452 | * |
||
| 453 | * @param Member $member |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | public function DeleteLink($member = null) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Link to mark as spam |
||
| 466 | * |
||
| 467 | * @param Member $member |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function SpamLink($member = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Link to mark as not-spam (ham) |
||
| 480 | * |
||
| 481 | * @param Member $member |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | public function HamLink($member = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Link to approve this comment |
||
| 494 | * |
||
| 495 | * @param Member $member |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public function ApproveLink($member = null) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Mark this comment as spam |
||
| 508 | */ |
||
| 509 | public function markSpam() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Mark this comment as approved |
||
| 519 | */ |
||
| 520 | public function markApproved() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Mark this comment as unapproved |
||
| 530 | */ |
||
| 531 | public function markUnapproved() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | public function SpamClass() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public function getTitle() |
||
| 567 | |||
| 568 | /* |
||
| 569 | * Modify the default fields shown to the user |
||
| 570 | */ |
||
| 571 | public function getCMSFields() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param String $dirtyHtml |
||
| 647 | * |
||
| 648 | * @return String |
||
| 649 | */ |
||
| 650 | public function purifyHtml($dirtyHtml) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
| 658 | */ |
||
| 659 | public function getHtmlPurifierService() |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Calculate the Gravatar link from the email address |
||
| 678 | * |
||
| 679 | * @return string |
||
| 680 | */ |
||
| 681 | public function Gravatar() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Determine if replies are enabled for this instance |
||
| 698 | * |
||
| 699 | * @return boolean |
||
| 700 | */ |
||
| 701 | public function getRepliesEnabled() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Returns the list of all replies |
||
| 716 | * |
||
| 717 | * @return SS_List |
||
| 718 | */ |
||
| 719 | public function AllReplies() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Ascertains whether or not to show the reply form |
||
| 739 | */ |
||
| 740 | public function ShowReplyToForm() { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
| 750 | * |
||
| 751 | * @return SS_List |
||
| 752 | */ |
||
| 753 | public function Replies() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
| 783 | * |
||
| 784 | * @return PaginatedList |
||
| 785 | */ |
||
| 786 | View Code Duplication | public function PagedReplies() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Generate a reply form for this comment |
||
| 801 | * |
||
| 802 | * @return Form |
||
| 803 | */ |
||
| 804 | public function ReplyForm() |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Refresh of this comment in the hierarchy |
||
| 828 | */ |
||
| 829 | public function updateDepth() |
||
| 839 | } |
||
| 840 | |||
| 952 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.