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() |
||
| 149 | { |
||
| 150 | parent::onBeforeWrite(); |
||
| 151 | |||
| 152 | // Sanitize HTML, because its expected to be passed to the template unescaped later |
||
| 153 | if ($this->AllowHtml) { |
||
| 154 | $this->Comment = $this->purifyHtml($this->Comment); |
||
| 155 | } |
||
| 156 | |||
| 157 | // Check comment depth |
||
| 158 | $this->updateDepth(); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritDoc} |
||
| 163 | */ |
||
| 164 | public function onBeforeDelete() |
||
| 165 | { |
||
| 166 | parent::onBeforeDelete(); |
||
| 167 | |||
| 168 | // Delete all children |
||
| 169 | foreach ($this->ChildComments() as $comment) { |
||
| 170 | $comment->delete(); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return Comment_SecurityToken |
||
| 176 | */ |
||
| 177 | public function getSecurityToken() |
||
| 178 | { |
||
| 179 | return Injector::inst()->createWithArgs(SecurityToken::class, array($this)); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Migrates the old {@link PageComment} objects to {@link Comment} |
||
| 184 | */ |
||
| 185 | public function requireDefaultRecords() |
||
| 186 | { |
||
| 187 | parent::requireDefaultRecords(); |
||
| 188 | |||
| 189 | if (DB::get_schema()->hasTable('PageComment')) { |
||
| 190 | $comments = DB::query('SELECT * FROM "PageComment"'); |
||
| 191 | |||
| 192 | if ($comments) { |
||
| 193 | while ($pageComment = $comments->next()) { |
||
| 194 | // create a new comment from the older page comment |
||
| 195 | $comment = new Comment(); |
||
| 196 | $comment->update($pageComment); |
||
| 197 | |||
| 198 | // set the variables which have changed |
||
| 199 | $comment->BaseClass = SiteTree::class; |
||
| 200 | $comment->URL = (isset($pageComment['CommenterURL'])) ? $pageComment['CommenterURL'] : ''; |
||
| 201 | if ((int) $pageComment['NeedsModeration'] == 0) { |
||
| 202 | $comment->Moderated = true; |
||
| 203 | } |
||
| 204 | |||
| 205 | $comment->write(); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | DB::alteration_message('Migrated PageComment to Comment', 'changed'); |
||
| 210 | DB::get_schema()->dontRequireTable('PageComment'); |
||
| 211 | } |
||
| 212 | } |
||
| 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 = '') |
||
| 222 | { |
||
| 223 | if ($parent = $this->Parent()) { |
||
| 224 | return $parent->Link($action) . '#' . $this->Permalink(); |
||
| 225 | } |
||
| 226 | } |
||
| 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) |
||
| 248 | { |
||
| 249 | $labels = parent::fieldLabels($includerelations); |
||
| 250 | |||
| 251 | $labels['Name'] = _t('SilverStripe\\Comments\\Model\\Comment.NAME', 'Author Name'); |
||
| 252 | $labels['Comment'] = _t('SilverStripe\\Comments\\Model\\Comment.COMMENT', 'Comment'); |
||
| 253 | $labels['Email'] = _t('SilverStripe\\Comments\\Model\\Comment.EMAIL', 'Email'); |
||
| 254 | $labels['URL'] = _t('SilverStripe\\Comments\\Model\\Comment.URL', 'URL'); |
||
| 255 | $labels['IsSpam'] = _t('SilverStripe\\Comments\\Model\\Comment.ISSPAM', 'Spam?'); |
||
| 256 | $labels['Moderated'] = _t('SilverStripe\\Comments\\Model\\Comment.MODERATED', 'Moderated?'); |
||
| 257 | $labels['ParentTitle'] = _t('SilverStripe\\Comments\\Model\\Comment.PARENTTITLE', 'Parent'); |
||
| 258 | $labels['Created'] = _t('SilverStripe\\Comments\\Model\\Comment.CREATED', 'Date posted'); |
||
| 259 | |||
| 260 | return $labels; |
||
| 261 | } |
||
| 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) |
||
| 271 | { |
||
| 272 | // If possible use the current record |
||
| 273 | $record = $this->Parent(); |
||
| 274 | |||
| 275 | if (!$record && $this->Parent()) { |
||
| 276 | // Otherwise a singleton of that record |
||
| 277 | $record = singleton($this->Parent()->dataClass()); |
||
| 278 | } elseif (!$record) { |
||
| 279 | // Otherwise just use the default options |
||
| 280 | $record = singleton(CommentsExtension::class); |
||
| 281 | } |
||
| 282 | |||
| 283 | return ($record instanceof CommentsExtension || $record->hasExtension(CommentsExtension::class)) |
||
| 284 | ? $record->getCommentsOption($key) |
||
| 285 | : null; |
||
| 286 | } |
||
| 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() |
||
| 295 | { |
||
| 296 | return $this->BaseClass && $this->ParentID |
||
| 297 | ? DataObject::get_by_id($this->BaseClass, $this->ParentID, true) |
||
| 298 | : null; |
||
| 299 | } |
||
| 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() |
||
| 320 | { |
||
| 321 | return $this->Parent()->getClassName(); |
||
| 322 | } |
||
| 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) |
||
| 376 | { |
||
| 377 | $member = $this->getMember($member); |
||
| 378 | |||
| 379 | $extended = $this->extendedCan('canView', $member); |
||
| 380 | if ($extended !== null) { |
||
| 381 | return $extended; |
||
| 382 | } |
||
| 383 | |||
| 384 | if (Permission::checkMember($member, 'CMS_ACCESS_CommentAdmin')) { |
||
| 385 | return true; |
||
| 386 | } |
||
| 387 | |||
| 388 | if ($parent = $this->Parent()) { |
||
| 389 | return $parent->canView($member) |
||
| 390 | && $parent->hasExtension(CommentsExtension::class) |
||
| 391 | && $parent->CommentsEnabled; |
||
| 392 | } |
||
| 393 | |||
| 394 | return false; |
||
| 395 | } |
||
| 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) |
||
| 404 | { |
||
| 405 | $member = $this->getMember($member); |
||
| 406 | |||
| 407 | if (!$member) { |
||
| 408 | return false; |
||
| 409 | } |
||
| 410 | |||
| 411 | $extended = $this->extendedCan('canEdit', $member); |
||
| 412 | if ($extended !== null) { |
||
| 413 | return $extended; |
||
| 414 | } |
||
| 415 | |||
| 416 | if (Permission::checkMember($member, 'CMS_ACCESS_CommentAdmin')) { |
||
| 417 | return true; |
||
| 418 | } |
||
| 419 | |||
| 420 | if ($parent = $this->Parent()) { |
||
| 421 | return $parent->canEdit($member); |
||
| 422 | } |
||
| 423 | |||
| 424 | return false; |
||
| 425 | } |
||
| 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) |
||
| 434 | { |
||
| 435 | $member = $this->getMember($member); |
||
| 436 | |||
| 437 | if (!$member) { |
||
| 438 | return false; |
||
| 439 | } |
||
| 440 | |||
| 441 | $extended = $this->extendedCan('canDelete', $member); |
||
| 442 | if ($extended !== null) { |
||
| 443 | return $extended; |
||
| 444 | } |
||
| 445 | |||
| 446 | return $this->canEdit($member); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Resolves Member object. |
||
| 451 | * |
||
| 452 | * @param Member|int|null $member |
||
| 453 | * @return Member|null |
||
| 454 | */ |
||
| 455 | protected function getMember($member = null) |
||
| 456 | { |
||
| 457 | if (!$member) { |
||
| 458 | $member = Member::currentUser(); |
||
| 459 | } |
||
| 460 | |||
| 461 | if (is_numeric($member)) { |
||
| 462 | $member = DataObject::get_by_id(Member::class, $member, true); |
||
| 463 | } |
||
| 464 | |||
| 465 | return $member; |
||
| 466 | } |
||
| 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) |
||
| 491 | { |
||
| 492 | if (!$member) { |
||
| 493 | $member = Member::currentUser(); |
||
| 494 | } |
||
| 495 | if (!$member) { |
||
| 496 | return false; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @todo: How do we handle "DataObject" instances that don't have a Link to reject/spam/delete?? This may |
||
| 501 | * we have to make CMS a hard dependency instead. |
||
| 502 | */ |
||
| 503 | // if (!$this->Parent()->hasMethod('Link')) { |
||
| 504 | // return false; |
||
| 505 | // } |
||
| 506 | |||
| 507 | $url = Controller::join_links( |
||
| 508 | Director::baseURL(), |
||
| 509 | 'comments', |
||
| 510 | $action, |
||
| 511 | $this->ID |
||
| 512 | ); |
||
| 513 | |||
| 514 | // Limit access for this user |
||
| 515 | $token = $this->getSecurityToken(); |
||
| 516 | return $token->addToUrl($url, $member); |
||
| 517 | } |
||
| 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() |
||
| 611 | { |
||
| 612 | if ($this->IsSpam) { |
||
| 613 | return 'spam'; |
||
| 614 | } elseif (!$this->Moderated) { |
||
| 615 | return 'unmoderated'; |
||
| 616 | } else { |
||
| 617 | return 'notspam'; |
||
| 618 | } |
||
| 619 | } |
||
| 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() |
||
| 641 | { |
||
| 642 | $commentField = $this->AllowHtml ? HTMLEditorField::class : TextareaField::class; |
||
| 643 | $fields = new FieldList( |
||
| 644 | $this |
||
| 645 | ->obj('Created') |
||
| 646 | ->scaffoldFormField($this->fieldLabel('Created')) |
||
| 647 | ->performReadonlyTransformation(), |
||
| 648 | TextField::create('Name', $this->fieldLabel('Name')), |
||
| 649 | $commentField::create('Comment', $this->fieldLabel('Comment')), |
||
| 650 | EmailField::create('Email', $this->fieldLabel('Email')), |
||
| 651 | TextField::create('URL', $this->fieldLabel('URL')), |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @param string $dirtyHtml |
||
| 716 | * |
||
| 717 | * @return string |
||
| 718 | */ |
||
| 719 | public function purifyHtml($dirtyHtml) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
| 730 | */ |
||
| 731 | public function getHtmlPurifierService() |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Calculate the Gravatar link from the email address |
||
| 756 | * |
||
| 757 | * @return string |
||
| 758 | */ |
||
| 759 | public function Gravatar() |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Determine if replies are enabled for this instance |
||
| 777 | * |
||
| 778 | * @return boolean |
||
| 779 | */ |
||
| 780 | public function getRepliesEnabled() |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Returns the list of all replies |
||
| 795 | * |
||
| 796 | * @return SS_List |
||
| 797 | */ |
||
| 798 | public function AllReplies() |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
| 818 | * |
||
| 819 | * @return SS_List |
||
| 820 | */ |
||
| 821 | public function Replies() |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
| 851 | * |
||
| 852 | * @return PaginatedList |
||
| 853 | */ |
||
| 854 | View Code Duplication | public function PagedReplies() |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Generate a reply form for this comment |
||
| 869 | * |
||
| 870 | * @return Form |
||
| 871 | */ |
||
| 872 | public function ReplyForm() |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Refresh of this comment in the hierarchy |
||
| 896 | */ |
||
| 897 | public function updateDepth() |
||
| 907 | } |
||
| 908 |
This check marks private properties in classes that are never used. Those properties can be removed.