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 BlogPost 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 BlogPost, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class BlogPost extends Page |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Same as above, but for list of users that can be |
||
| 49 | * given credit in the author field for blog posts |
||
| 50 | * @var string|bool false or group code |
||
| 51 | */ |
||
| 52 | private static $restrict_authors_to_group = false; |
||
|
|
|||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritDoc} |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private static $table_name = 'BlogPost'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private static $db = [ |
||
| 64 | 'PublishDate' => 'Datetime', |
||
| 65 | 'AuthorNames' => 'Varchar(1024)', |
||
| 66 | 'Summary' => 'HTMLText' |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private static $has_one = [ |
||
| 73 | 'FeaturedImage' => Image::class |
||
| 74 | ]; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private static $owns = [ |
||
| 80 | 'FeaturedImage', |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private static $many_many = [ |
||
| 87 | 'Categories' => BlogCategory::class, |
||
| 88 | 'Tags' => BlogTag::class, |
||
| 89 | 'Authors' => Member::class |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private static $defaults = [ |
||
| 96 | 'ShowInMenus' => false, |
||
| 97 | 'InheritSideBar' => true, |
||
| 98 | 'ProvideComments' => true |
||
| 99 | ]; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private static $extensions = [ |
||
| 105 | BlogPostFilter::class |
||
| 106 | ]; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private static $searchable_fields = [ |
||
| 112 | 'Title' |
||
| 113 | ]; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | private static $summary_fields = [ |
||
| 119 | 'Title' |
||
| 120 | ]; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | private static $casting = [ |
||
| 126 | 'Excerpt' => 'HTMLText', |
||
| 127 | 'Date' => 'DBDatetime' |
||
| 128 | ]; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | private static $allowed_children = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
| 137 | * |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var bool |
||
| 144 | */ |
||
| 145 | private static $can_be_root = false; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * This will display or hide the current class from the SiteTree. This variable can be |
||
| 149 | * configured using YAML. |
||
| 150 | * |
||
| 151 | * @var bool |
||
| 152 | */ |
||
| 153 | private static $show_in_sitetree = false; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Determine the role of the given member. |
||
| 157 | * |
||
| 158 | * Call be called via template to determine the current user. |
||
| 159 | * |
||
| 160 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 161 | * |
||
| 162 | * @param null|int|Member $member |
||
| 163 | * |
||
| 164 | * @return null|string |
||
| 165 | */ |
||
| 166 | public function RoleOf($member = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Determine if the given member is an author of this post. |
||
| 189 | * |
||
| 190 | * @param null|Member $member |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | View Code Duplication | public function isAuthor($member = null) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function getCMSFields() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
| 338 | * |
||
| 339 | * @return SS_List |
||
| 340 | */ |
||
| 341 | public function getCandidateAuthors() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Determine if this user can edit the authors list. |
||
| 354 | * |
||
| 355 | * @param null|int|Member $member |
||
| 356 | * |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function canEditAuthors($member = null) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param null|int|Member $member |
||
| 386 | * |
||
| 387 | * @return null|Member |
||
| 388 | */ |
||
| 389 | View Code Duplication | protected function getMember($member = null) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Determine whether user can create new categories. |
||
| 404 | * |
||
| 405 | * @param null|int|Member $member |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function canCreateCategories($member = null) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Determine whether user can create new tags. |
||
| 428 | * |
||
| 429 | * @param null|int|Member $member |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | View Code Duplication | public function canCreateTags($member = null) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | * |
||
| 457 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
| 458 | */ |
||
| 459 | public function onBeforePublish() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * {@inheritdoc} |
||
| 474 | * |
||
| 475 | * Sets blog relationship on all categories and tags assigned to this post. |
||
| 476 | */ |
||
| 477 | public function onAfterWrite() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * {@inheritdoc} |
||
| 500 | */ |
||
| 501 | public function canView($member = null) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritdoc} |
||
| 531 | */ |
||
| 532 | public function canPublish($member = null) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * {@inheritdoc} |
||
| 567 | */ |
||
| 568 | public function canEdit($member = null) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Returns the post excerpt. |
||
| 595 | * |
||
| 596 | * @param int $wordsToDisplay |
||
| 597 | * |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | public function Excerpt($wordsToDisplay = 30) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Returns a monthly archive link for the current blog post. |
||
| 610 | * |
||
| 611 | * @param string $type |
||
| 612 | * |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | public function getMonthlyArchiveLink($type = 'day') |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Returns a yearly archive link for the current blog post. |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public function getYearlyArchiveLink() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Resolves static and dynamic authors linked to this post. |
||
| 655 | * |
||
| 656 | * @return ArrayList |
||
| 657 | */ |
||
| 658 | public function getCredits() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Resolves dynamic authors linked to this post. |
||
| 670 | * |
||
| 671 | * @return ArrayList |
||
| 672 | */ |
||
| 673 | protected function getDynamicCredits() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Resolves static authors linked to this post. |
||
| 702 | * |
||
| 703 | * @return ArrayList |
||
| 704 | */ |
||
| 705 | protected function getStaticCredits() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Checks to see if User Profiles has been disabled via config |
||
| 724 | * |
||
| 725 | * @return bool |
||
| 726 | */ |
||
| 727 | public function getProfilesDisabled() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
| 734 | * |
||
| 735 | * @param bool $includeRelations |
||
| 736 | * |
||
| 737 | * @return array |
||
| 738 | */ |
||
| 739 | public function fieldLabels($includeRelations = true) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Proxy method for displaying the publish date in rss feeds. |
||
| 750 | * @see https://github.com/silverstripe/silverstripe-blog/issues/394 |
||
| 751 | * |
||
| 752 | * @return string|null |
||
| 753 | */ |
||
| 754 | public function getDate() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * {@inheritdoc} |
||
| 764 | */ |
||
| 765 | protected function onBeforeWrite() |
||
| 773 | } |
||
| 774 |
This check marks private properties in classes that are never used. Those properties can be removed.