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 |
||
| 49 | class Comment extends DataObject |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * {@inheritDoc} |
||
| 53 | */ |
||
| 54 | private static $db = array( |
||
| 55 | 'Name' => 'Varchar(200)', |
||
| 56 | 'Comment' => 'Text', |
||
| 57 | 'Email' => 'Varchar(200)', |
||
| 58 | 'URL' => 'Varchar(255)', |
||
| 59 | 'Moderated' => 'Boolean(0)', |
||
| 60 | 'IsSpam' => 'Boolean(0)', |
||
| 61 | 'AllowHtml' => 'Boolean', |
||
| 62 | 'SecretToken' => 'Varchar(255)', |
||
| 63 | 'Depth' => 'Int' |
||
| 64 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritDoc} |
||
| 68 | */ |
||
| 69 | private static $has_one = array( |
||
| 70 | 'Author' => Member::class, |
||
| 71 | 'ParentComment' => self::class, |
||
| 72 | 'Parent' => DataObject::class |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritDoc} |
||
| 77 | */ |
||
| 78 | private static $has_many = array( |
||
| 79 | 'ChildComments' => self::class |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritDoc} |
||
| 84 | */ |
||
| 85 | private static $default_sort = '"Created" DESC'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritDoc} |
||
| 89 | */ |
||
| 90 | private static $defaults = array( |
||
| 91 | 'Moderated' => 0, |
||
| 92 | 'IsSpam' => 0, |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritDoc} |
||
| 97 | */ |
||
| 98 | private static $casting = array( |
||
| 99 | 'Title' => 'Varchar', |
||
| 100 | 'ParentTitle' => 'Varchar', |
||
| 101 | 'ParentClassName' => 'Varchar', |
||
| 102 | 'AuthorName' => 'Varchar', |
||
| 103 | 'RSSName' => 'Varchar', |
||
| 104 | 'DeleteLink' => 'Varchar', |
||
| 105 | 'SpamLink' => 'Varchar', |
||
| 106 | 'HamLink' => 'Varchar', |
||
| 107 | 'ApproveLink' => 'Varchar', |
||
| 108 | 'Permalink' => 'Varchar' |
||
| 109 | ); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritDoc} |
||
| 113 | */ |
||
| 114 | private static $searchable_fields = array( |
||
| 115 | 'Name', |
||
| 116 | 'Email', |
||
| 117 | 'Comment', |
||
| 118 | 'Created' |
||
| 119 | ); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritDoc} |
||
| 123 | */ |
||
| 124 | private static $summary_fields = array( |
||
| 125 | 'Name' => 'Submitted By', |
||
| 126 | 'Email' => 'Email', |
||
| 127 | 'Comment.LimitWordCount' => 'Comment', |
||
| 128 | 'Created' => 'Date Posted', |
||
| 129 | 'Parent.Title' => 'Post', |
||
| 130 | 'IsSpam' => 'Is Spam' |
||
| 131 | ); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritDoc} |
||
| 135 | */ |
||
| 136 | private static $field_labels = array( |
||
| 137 | 'Author' => 'Author Member' |
||
| 138 | ); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritDoc} |
||
| 142 | */ |
||
| 143 | private static $table_name = 'Comment'; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritDoc} |
||
| 147 | */ |
||
| 148 | public function onBeforeWrite() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritDoc} |
||
| 163 | */ |
||
| 164 | public function onBeforeDelete() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return Comment_SecurityToken |
||
| 176 | */ |
||
| 177 | public function getSecurityToken() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Migrates the old {@link PageComment} objects to {@link Comment} |
||
| 184 | */ |
||
| 185 | public function requireDefaultRecords() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Return a link to this comment |
||
| 216 | * |
||
| 217 | * @param string $action |
||
| 218 | * |
||
| 219 | * @return string link to this comment. |
||
| 220 | */ |
||
| 221 | public function Link($action = '') |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the permalink for this {@link Comment}. Inserted into |
||
| 230 | * the ID tag of the comment |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function Permalink() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Translate the form field labels for the CMS administration |
||
| 242 | * |
||
| 243 | * @param boolean $includerelations |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function fieldLabels($includerelations = true) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the commenting option |
||
| 265 | * |
||
| 266 | * @param string $key |
||
| 267 | * |
||
| 268 | * @return mixed Result if the setting is available, or null otherwise |
||
| 269 | */ |
||
| 270 | public function getOption($key) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the parent {@link DataObject} this comment is attached too |
||
| 290 | * |
||
| 291 | * @deprecated 4.0.0 Use $this->Parent() instead |
||
| 292 | * @return DataObject |
||
| 293 | */ |
||
| 294 | public function getParent() |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns a string to help identify the parent of the comment |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function getParentTitle() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Comment-parent classnames obviously vary, return the parent classname |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public function getParentClassName() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritDoc} |
||
| 326 | */ |
||
| 327 | public function castingHelper($field) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Content to be safely escaped on the frontend |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getEscapedComment() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Return whether this comment is a preview (has not been written to the db) |
||
| 348 | * |
||
| 349 | * @return boolean |
||
| 350 | */ |
||
| 351 | public function isPreview() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @todo needs to compare to the new {@link Commenting} configuration API |
||
| 358 | * |
||
| 359 | * @param Member $member |
||
| 360 | * @param array $context |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | public function canCreate($member = null, $context = []) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Checks for association with a page, and {@link SiteTree->ProvidePermission} |
||
| 370 | * flag being set to true. |
||
| 371 | * |
||
| 372 | * @param Member $member |
||
| 373 | * @return Boolean |
||
| 374 | */ |
||
| 375 | public function canView($member = null) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Checks if the comment can be edited. |
||
| 399 | * |
||
| 400 | * @param null|int|Member $member |
||
| 401 | * @return Boolean |
||
| 402 | */ |
||
| 403 | public function canEdit($member = null) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Checks if the comment can be deleted. |
||
| 429 | * |
||
| 430 | * @param null|int|Member $member |
||
| 431 | * @return Boolean |
||
| 432 | */ |
||
| 433 | public function canDelete($member = null) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Resolves Member object. |
||
| 451 | * |
||
| 452 | * @param Member|int|null $member |
||
| 453 | * @return Member|null |
||
| 454 | */ |
||
| 455 | protected function getMember($member = null) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Return the authors name for the comment |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getAuthorName() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Generate a secure admin-action link authorised for the specified member |
||
| 484 | * |
||
| 485 | * @param string $action An action on CommentingController to link to |
||
| 486 | * @param Member $member The member authorised to invoke this action |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | protected function actionLink($action, $member = null) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Link to delete this comment |
||
| 521 | * |
||
| 522 | * @param Member $member |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public function DeleteLink($member = null) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Link to mark as spam |
||
| 535 | * |
||
| 536 | * @param Member $member |
||
| 537 | * |
||
| 538 | * @return string |
||
| 539 | */ |
||
| 540 | public function SpamLink($member = null) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Link to mark as not-spam (ham) |
||
| 549 | * |
||
| 550 | * @param Member $member |
||
| 551 | * |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | public function HamLink($member = null) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Link to approve this comment |
||
| 563 | * |
||
| 564 | * @param Member $member |
||
| 565 | * |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | public function ApproveLink($member = null) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Mark this comment as spam |
||
| 577 | */ |
||
| 578 | public function markSpam() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Mark this comment as approved |
||
| 588 | */ |
||
| 589 | public function markApproved() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Mark this comment as unapproved |
||
| 599 | */ |
||
| 600 | public function markUnapproved() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | public function SpamClass() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | public function getTitle() |
||
| 636 | |||
| 637 | /* |
||
| 638 | * Modify the default fields shown to the user |
||
| 639 | */ |
||
| 640 | public function getCMSFields() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @param string $dirtyHtml |
||
| 716 | * |
||
| 717 | * @return string |
||
| 718 | */ |
||
| 719 | public function purifyHtml($dirtyHtml) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
| 727 | */ |
||
| 728 | public function getHtmlPurifierService() |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Calculate the Gravatar link from the email address |
||
| 749 | * |
||
| 750 | * @return string |
||
| 751 | */ |
||
| 752 | public function Gravatar() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Determine if replies are enabled for this instance |
||
| 769 | * |
||
| 770 | * @return boolean |
||
| 771 | */ |
||
| 772 | public function getRepliesEnabled() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Returns the list of all replies |
||
| 787 | * |
||
| 788 | * @return SS_List |
||
| 789 | */ |
||
| 790 | public function AllReplies() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
| 810 | * |
||
| 811 | * @return SS_List |
||
| 812 | */ |
||
| 813 | public function Replies() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
| 843 | * |
||
| 844 | * @return PaginatedList |
||
| 845 | */ |
||
| 846 | View Code Duplication | public function PagedReplies() |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Generate a reply form for this comment |
||
| 861 | * |
||
| 862 | * @return Form |
||
| 863 | */ |
||
| 864 | public function ReplyForm() |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Refresh of this comment in the hierarchy |
||
| 888 | */ |
||
| 889 | public function updateDepth() |
||
| 899 | } |
||
| 900 |
This check marks private properties in classes that are never used. Those properties can be removed.