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() |
||
| 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() |
||
| 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 = '') |
||
| 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() |
||
| 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) |
||
| 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) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the parent {@link DataObject} this comment is attached too |
||
| 230 | * |
||
| 231 | * @return DataObject |
||
| 232 | */ |
||
| 233 | public function getParent() |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns a string to help identify the parent of the comment |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getParentTitle() |
||
| 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) |
||
| 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) |
||
| 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() |
||
| 572 | { |
||
| 573 | $commentField = $this->AllowHtml ? 'HtmlEditorField' : 'TextareaField'; |
||
| 574 | $fields = new FieldList( |
||
| 575 | $this |
||
| 576 | ->obj('Created') |
||
| 577 | ->scaffoldFormField($this->fieldLabel('Created')) |
||
| 578 | ->performReadonlyTransformation(), |
||
| 579 | TextField::create('Name', $this->fieldLabel('Name')), |
||
| 580 | $commentField::create('Comment', $this->fieldLabel('Comment')), |
||
| 581 | EmailField::create('Email', $this->fieldLabel('Email')), |
||
| 582 | TextField::create('URL', $this->fieldLabel('URL')), |
||
| 583 | FieldGroup::create(array( |
||
| 584 | CheckboxField::create('Moderated', $this->fieldLabel('Moderated')), |
||
| 585 | CheckboxField::create('IsSpam', $this->fieldLabel('IsSpam')), |
||
| 586 | )) |
||
| 587 | ->setTitle(_t('Comment.OPTIONS', 'Options')) |
||
| 588 | ->setDescription(_t( |
||
| 589 | 'Comment.OPTION_DESCRIPTION', |
||
| 590 | 'Unmoderated and spam comments will not be displayed until approved' |
||
| 591 | )) |
||
| 592 | ); |
||
| 593 | |||
| 594 | // Show member name if given |
||
| 595 | if (($author = $this->Author()) && $author->exists()) { |
||
| 596 | $fields->insertAfter( |
||
| 597 | TextField::create('AuthorMember', $this->fieldLabel('Author'), $author->Title) |
||
| 598 | ->performReadonlyTransformation(), |
||
| 599 | 'Name' |
||
| 600 | ); |
||
| 601 | } |
||
| 602 | |||
| 603 | // Show parent comment if given |
||
| 604 | if (($parent = $this->ParentComment()) && $parent->exists()) { |
||
| 605 | $fields->push(new HeaderField( |
||
| 606 | 'ParentComment_Title', |
||
| 607 | _t('Comment.ParentComment_Title', 'This comment is a reply to the below') |
||
| 608 | )); |
||
| 609 | // Created date |
||
| 610 | // FIXME - the method setName in DatetimeField is not chainable, hence |
||
| 611 | // the lack of chaining here |
||
| 612 | $createdField = $parent |
||
| 613 | ->obj('Created') |
||
| 614 | ->scaffoldFormField($parent->fieldLabel('Created')); |
||
| 615 | $createdField->setName('ParentComment_Created'); |
||
| 616 | $createdField->setValue($parent->Created); |
||
| 617 | $createdField->performReadonlyTransformation(); |
||
| 618 | $fields->push($createdField); |
||
| 619 | |||
| 620 | // Name (could be member or string value) |
||
| 621 | $fields->push( |
||
| 622 | $parent |
||
| 623 | ->obj('AuthorName') |
||
| 624 | ->scaffoldFormField($parent->fieldLabel('AuthorName')) |
||
| 625 | ->setName('ParentComment_AuthorName') |
||
| 626 | ->setValue($parent->getAuthorName()) |
||
| 627 | ->performReadonlyTransformation() |
||
| 628 | ); |
||
| 629 | |||
| 630 | // Comment body |
||
| 631 | $fields->push( |
||
| 632 | $parent |
||
| 633 | ->obj('EscapedComment') |
||
| 634 | ->scaffoldFormField($parent->fieldLabel('Comment')) |
||
| 635 | ->setName('ParentComment_EscapedComment') |
||
| 636 | ->setValue($parent->Comment) |
||
| 637 | ->performReadonlyTransformation() |
||
| 638 | ); |
||
| 639 | } |
||
| 640 | |||
| 641 | $this->extend('updateCMSFields', $fields); |
||
| 642 | return $fields; |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param String $dirtyHtml |
||
| 647 | * |
||
| 648 | * @return String |
||
| 649 | */ |
||
| 650 | public function purifyHtml($dirtyHtml) |
||
| 651 | { |
||
| 652 | $purifier = $this->getHtmlPurifierService(); |
||
| 653 | return $purifier->purify($dirtyHtml); |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
| 658 | */ |
||
| 659 | public function getHtmlPurifierService() |
||
| 660 | { |
||
| 661 | $config = HTMLPurifier_Config::createDefault(); |
||
| 662 | $allowedElements = $this->getOption('html_allowed_elements'); |
||
| 663 | $config->set('HTML.AllowedElements', $allowedElements); |
||
| 664 | |||
| 665 | // This injector cannot be set unless the 'p' element is allowed |
||
| 666 | if (in_array('p', $allowedElements)) { |
||
| 667 | $config->set('AutoFormat.AutoParagraph', true); |
||
| 668 | } |
||
| 669 | |||
| 670 | $config->set('AutoFormat.Linkify', true); |
||
| 671 | $config->set('URI.DisableExternalResources', true); |
||
| 672 | $config->set('Cache.SerializerPath', getTempFolder()); |
||
| 673 | return new HTMLPurifier($config); |
||
| 674 | } |
||
| 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 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
| 739 | * |
||
| 740 | * @return SS_List |
||
| 741 | */ |
||
| 742 | public function Replies() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
| 772 | * |
||
| 773 | * @return PaginatedList |
||
| 774 | */ |
||
| 775 | View Code Duplication | public function PagedReplies() |
|
| 787 | |||
| 788 | /** |
||
| 789 | * Generate a reply form for this comment |
||
| 790 | * |
||
| 791 | * @return Form |
||
| 792 | */ |
||
| 793 | public function ReplyForm() |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Refresh of this comment in the hierarchy |
||
| 817 | */ |
||
| 818 | public function updateDepth() |
||
| 828 | } |
||
| 829 | |||
| 830 | |||
| 831 | /** |
||
| 832 | * Provides the ability to generate cryptographically secure tokens for comment moderation |
||
| 941 |
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.