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 |
||
| 18 | class BlogPost extends Page |
||
|
|
|||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Same as above, but for list of users that can be |
||
| 22 | * given credit in the author field for blog posts |
||
| 23 | * @var string|bool false or group code |
||
| 24 | */ |
||
| 25 | private static $restrict_authors_to_group = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $db = array( |
||
| 31 | 'PublishDate' => 'SS_Datetime', |
||
| 32 | 'AuthorNames' => 'Varchar(1024)', |
||
| 33 | 'Summary' => 'HTMLText', |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $has_one = array( |
||
| 40 | 'FeaturedImage' => 'Image', |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $many_many = array( |
||
| 47 | 'Categories' => 'BlogCategory', |
||
| 48 | 'Tags' => 'BlogTag', |
||
| 49 | 'Authors' => 'Member', |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private static $defaults = array( |
||
| 56 | 'ShowInMenus' => false, |
||
| 57 | 'InheritSideBar' => true, |
||
| 58 | 'ProvideComments' => true, |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $extensions = array( |
||
| 65 | 'BlogPostFilter', |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private static $searchable_fields = array( |
||
| 72 | 'Title', |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | private static $summary_fields = array( |
||
| 79 | 'Title', |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private static $casting = array( |
||
| 86 | 'Excerpt' => 'Text', |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private static $allowed_children = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private static $can_be_root = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * This will display or hide the current class from the SiteTree. This variable can be |
||
| 108 | * configured using YAML. |
||
| 109 | * |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | private static $show_in_sitetree = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Determine the role of the given member. |
||
| 116 | * |
||
| 117 | * Call be called via template to determine the current user. |
||
| 118 | * |
||
| 119 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 120 | * |
||
| 121 | * @param null|int|Member $member |
||
| 122 | * |
||
| 123 | * @return null|string |
||
| 124 | */ |
||
| 125 | public function RoleOf($member = null) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Determine if the given member is an author of this post. |
||
| 148 | * |
||
| 149 | * @param null|Member $member |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function isAuthor($member = null) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function getCMSFields() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
| 292 | * |
||
| 293 | * @return SS_List |
||
| 294 | */ |
||
| 295 | public function getCandidateAuthors() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Determine if this user can edit the authors list. |
||
| 308 | * |
||
| 309 | * @param null|int|Member $member |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function canEditAuthors($member = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param null|int|Member $member |
||
| 340 | * |
||
| 341 | * @return null|Member |
||
| 342 | */ |
||
| 343 | View Code Duplication | protected function getMember($member = null) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Determine whether user can create new categories. |
||
| 358 | * |
||
| 359 | * @param null|int|Member $member |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | View Code Duplication | public function canCreateCategories($member = null) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Determine whether user can create new tags. |
||
| 382 | * |
||
| 383 | * @param null|int|Member $member |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | View Code Duplication | public function canCreateTags($member = null) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | * |
||
| 411 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
| 412 | */ |
||
| 413 | public function onBeforePublish() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | * |
||
| 429 | * Sets blog relationship on all categories and tags assigned to this post. |
||
| 430 | */ |
||
| 431 | public function onAfterWrite() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | public function canView($member = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * {@inheritdoc} |
||
| 480 | */ |
||
| 481 | public function canPublish($member = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * {@inheritdoc} |
||
| 516 | */ |
||
| 517 | public function canEdit($member = null) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the post excerpt. |
||
| 544 | * |
||
| 545 | * @param int $wordsToDisplay |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function Excerpt($wordsToDisplay = 30) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Returns a monthly archive link for the current blog post. |
||
| 561 | * |
||
| 562 | * @param string $type |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public function getMonthlyArchiveLink($type = 'day') |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Returns a yearly archive link for the current blog post. |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function getYearlyArchiveLink() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Resolves static and dynamic authors linked to this post. |
||
| 606 | * |
||
| 607 | * @return ArrayList |
||
| 608 | */ |
||
| 609 | public function getCredits() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Resolves dynamic authors linked to this post. |
||
| 621 | * |
||
| 622 | * @return ArrayList |
||
| 623 | */ |
||
| 624 | protected function getDynamicCredits() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Resolves static authors linked to this post. |
||
| 653 | * |
||
| 654 | * @return ArrayList |
||
| 655 | */ |
||
| 656 | protected function getStaticCredits() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
| 675 | * |
||
| 676 | * @param bool $includeRelations |
||
| 677 | * |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | public function fieldLabels($includeRelations = true) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * {@inheritdoc} |
||
| 691 | */ |
||
| 692 | protected function onBeforeWrite() |
||
| 700 | } |
||
| 701 | |||
| 709 |
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.