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 |
||
| 39 | class BlogPost extends Page |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * Same as above, but for list of users that can be |
||
| 43 | * given credit in the author field for blog posts |
||
| 44 | * @var string|bool false or group code |
||
| 45 | */ |
||
| 46 | private static $restrict_authors_to_group = false; |
||
|
|
|||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritDoc} |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private static $table_name = 'BlogPost'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private static $db = [ |
||
| 58 | 'PublishDate' => 'Datetime', |
||
| 59 | 'AuthorNames' => 'Varchar(1024)', |
||
| 60 | 'Summary' => 'HTMLText' |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $has_one = [ |
||
| 67 | 'FeaturedImage' => Image::class |
||
| 68 | ]; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private static $owns = [ |
||
| 74 | 'FeaturedImage', |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private static $many_many = [ |
||
| 81 | 'Categories' => BlogCategory::class, |
||
| 82 | 'Tags' => BlogTag::class, |
||
| 83 | 'Authors' => Member::class |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private static $defaults = [ |
||
| 90 | 'ShowInMenus' => false, |
||
| 91 | 'InheritSideBar' => true, |
||
| 92 | 'ProvideComments' => true |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private static $extensions = [ |
||
| 99 | BlogPostFilter::class |
||
| 100 | ]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private static $searchable_fields = [ |
||
| 106 | 'Title' |
||
| 107 | ]; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private static $summary_fields = [ |
||
| 113 | 'Title' |
||
| 114 | ]; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | private static $casting = [ |
||
| 120 | 'Excerpt' => 'HTMLText', |
||
| 121 | 'Date' => 'DBDatetime' |
||
| 122 | ]; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | private static $allowed_children = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
| 131 | * |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | private static $can_be_root = false; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * This will display or hide the current class from the SiteTree. This variable can be |
||
| 143 | * configured using YAML. |
||
| 144 | * |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | private static $show_in_sitetree = false; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Determine the role of the given member. |
||
| 151 | * |
||
| 152 | * Call be called via template to determine the current user. |
||
| 153 | * |
||
| 154 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 155 | * |
||
| 156 | * @param null|int|Member $member |
||
| 157 | * |
||
| 158 | * @return null|string |
||
| 159 | */ |
||
| 160 | public function RoleOf($member = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Determine if the given member is an author of this post. |
||
| 183 | * |
||
| 184 | * @param null|Member $member |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function isAuthor($member = null) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | public function getCMSFields() |
||
| 207 | { |
||
| 208 | Requirements::css('silverstripe/blog:client/dist/styles/main.css'); |
||
| 209 | Requirements::javascript('silverstripe/blog:client/dist/js/main.bundle.js'); |
||
| 210 | |||
| 211 | $this->beforeUpdateCMSFields(function ($fields) { |
||
| 212 | $uploadField = UploadField::create('FeaturedImage', _t(__CLASS__ . '.FeaturedImage', 'Featured Image')); |
||
| 213 | $uploadField->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @var FieldList $fields |
||
| 217 | */ |
||
| 218 | $fields->insertAfter($uploadField, 'Content'); |
||
| 219 | |||
| 220 | $summary = HtmlEditorField::create('Summary', false); |
||
| 221 | $summary->setRows(5); |
||
| 222 | $summary->setDescription(_t( |
||
| 223 | __CLASS__ . '.SUMMARY_DESCRIPTION', |
||
| 224 | 'If no summary is specified the first 30 words will be used.' |
||
| 225 | )); |
||
| 226 | |||
| 227 | $summaryHolder = ToggleCompositeField::create( |
||
| 228 | 'CustomSummary', |
||
| 229 | _t(__CLASS__ . '.CUSTOMSUMMARY', 'Add A Custom Summary'), |
||
| 230 | [ |
||
| 231 | $summary, |
||
| 232 | ] |
||
| 233 | ); |
||
| 234 | $summaryHolder->setHeadingLevel(4); |
||
| 235 | $summaryHolder->addExtraClass('custom-summary'); |
||
| 236 | |||
| 237 | $fields->insertAfter($summaryHolder, 'FeaturedImage'); |
||
| 238 | |||
| 239 | $urlSegment = $fields->dataFieldByName('URLSegment'); |
||
| 240 | $urlSegment->setURLPrefix($this->Parent()->RelativeLink()); |
||
| 241 | |||
| 242 | $fields->removeFieldsFromTab('Root.Main', [ |
||
| 243 | 'MenuTitle', |
||
| 244 | 'URLSegment', |
||
| 245 | ]); |
||
| 246 | |||
| 247 | $authorField = ListboxField::create( |
||
| 248 | 'Authors', |
||
| 249 | _t(__CLASS__ . '.Authors', 'Authors'), |
||
| 250 | $this->getCandidateAuthors()->map()->toArray() |
||
| 251 | ); |
||
| 252 | |||
| 253 | $authorNames = TextField::create( |
||
| 254 | 'AuthorNames', |
||
| 255 | _t(__CLASS__ . '.AdditionalCredits', 'Additional Credits'), |
||
| 256 | null, |
||
| 257 | 1024 |
||
| 258 | )->setDescription( |
||
| 259 | _t( |
||
| 260 | __CLASS__ . '.AdditionalCredits_Description', |
||
| 261 | 'If some authors of this post don\'t have CMS access, enter their name(s) here. '. |
||
| 262 | 'You can separate multiple names with a comma.' |
||
| 263 | ) |
||
| 264 | ); |
||
| 265 | |||
| 266 | if (!$this->canEditAuthors()) { |
||
| 267 | $authorField = $authorField->performDisabledTransformation(); |
||
| 268 | $authorNames = $authorNames->performDisabledTransformation(); |
||
| 269 | } |
||
| 270 | |||
| 271 | $publishDate = DatetimeField::create('PublishDate', _t(__CLASS__ . '.PublishDate', 'Publish Date')); |
||
| 272 | |||
| 273 | if (!$this->PublishDate) { |
||
| 274 | $publishDate->setDescription( |
||
| 275 | _t( |
||
| 276 | __CLASS__ . '.PublishDate_Description', |
||
| 277 | 'Will be set to "now" if published without a value.' |
||
| 278 | ) |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | |||
| 282 | // Get categories and tags |
||
| 283 | $parent = $this->Parent(); |
||
| 284 | $categories = $parent instanceof Blog |
||
| 285 | ? $parent->Categories() |
||
| 286 | : BlogCategory::get(); |
||
| 287 | $tags = $parent instanceof Blog |
||
| 288 | ? $parent->Tags() |
||
| 289 | : BlogTag::get(); |
||
| 290 | |||
| 291 | // @todo: Reimplement the sidebar |
||
| 292 | // $options = BlogAdminSidebar::create( |
||
| 293 | $fields->addFieldsToTab( |
||
| 294 | 'Root.PostOptions', |
||
| 295 | [ |
||
| 296 | $publishDate, |
||
| 297 | $urlSegment, |
||
| 298 | TagField::create( |
||
| 299 | 'Categories', |
||
| 300 | _t(__CLASS__ . '.Categories', 'Categories'), |
||
| 301 | $categories, |
||
| 302 | $this->Categories() |
||
| 303 | ) |
||
| 304 | ->setCanCreate($this->canCreateCategories()) |
||
| 305 | ->setShouldLazyLoad(true), |
||
| 306 | TagField::create( |
||
| 307 | 'Tags', |
||
| 308 | _t(__CLASS__ . '.Tags', 'Tags'), |
||
| 309 | $tags, |
||
| 310 | $this->Tags() |
||
| 311 | ) |
||
| 312 | ->setCanCreate($this->canCreateTags()) |
||
| 313 | ->setShouldLazyLoad(true), |
||
| 314 | $authorField, |
||
| 315 | $authorNames |
||
| 316 | ] |
||
| 317 | ); |
||
| 318 | // )->setTitle('Post Options'); |
||
| 319 | // $options->setName('blog-admin-sidebar'); |
||
| 320 | // $fields->insertBefore($options, 'Root'); |
||
| 321 | }); |
||
| 322 | |||
| 323 | $fields = parent::getCMSFields(); |
||
| 324 | |||
| 325 | $fields->fieldByName('Root')->setTemplate('TabSet_holder'); |
||
| 326 | |||
| 327 | $fields->fieldByName('Root.PostOptions') |
||
| 328 | ->setTitle(_t(__CLASS__ . '.PostOptions', 'Post Options')); |
||
| 329 | |||
| 330 | return $fields; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
| 335 | * |
||
| 336 | * @return SS_List |
||
| 337 | */ |
||
| 338 | public function getCandidateAuthors() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Determine if this user can edit the authors list. |
||
| 351 | * |
||
| 352 | * @param null|int|Member $member |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function canEditAuthors($member = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param null|int|Member $member |
||
| 383 | * |
||
| 384 | * @return null|Member |
||
| 385 | */ |
||
| 386 | View Code Duplication | protected function getMember($member = null) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Determine whether user can create new categories. |
||
| 401 | * |
||
| 402 | * @param null|int|Member $member |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | View Code Duplication | public function canCreateCategories($member = null) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Determine whether user can create new tags. |
||
| 425 | * |
||
| 426 | * @param null|int|Member $member |
||
| 427 | * |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | View Code Duplication | public function canCreateTags($member = null) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | * |
||
| 454 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
| 455 | */ |
||
| 456 | public function onBeforePublish() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | * |
||
| 472 | * Sets blog relationship on all categories and tags assigned to this post. |
||
| 473 | */ |
||
| 474 | public function onAfterWrite() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | public function canView($member = null) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * {@inheritdoc} |
||
| 528 | */ |
||
| 529 | public function canPublish($member = null) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * {@inheritdoc} |
||
| 564 | */ |
||
| 565 | public function canEdit($member = null) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns the post excerpt. |
||
| 592 | * |
||
| 593 | * @param int $wordsToDisplay |
||
| 594 | * |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | public function Excerpt($wordsToDisplay = 30) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Returns a monthly archive link for the current blog post. |
||
| 607 | * |
||
| 608 | * @param string $type |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | public function getMonthlyArchiveLink($type = 'day') |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Returns a yearly archive link for the current blog post. |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | public function getYearlyArchiveLink() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Resolves static and dynamic authors linked to this post. |
||
| 652 | * |
||
| 653 | * @return ArrayList |
||
| 654 | */ |
||
| 655 | public function getCredits() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Resolves dynamic authors linked to this post. |
||
| 667 | * |
||
| 668 | * @return ArrayList |
||
| 669 | */ |
||
| 670 | protected function getDynamicCredits() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Resolves static authors linked to this post. |
||
| 699 | * |
||
| 700 | * @return ArrayList |
||
| 701 | */ |
||
| 702 | protected function getStaticCredits() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Checks to see if User Profiles has been disabled via config |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | */ |
||
| 724 | public function getProfilesDisabled() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
| 731 | * |
||
| 732 | * @param bool $includeRelations |
||
| 733 | * |
||
| 734 | * @return array |
||
| 735 | */ |
||
| 736 | public function fieldLabels($includeRelations = true) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Proxy method for displaying the publish date in rss feeds. |
||
| 747 | * @see https://github.com/silverstripe/silverstripe-blog/issues/394 |
||
| 748 | * |
||
| 749 | * @return string|null |
||
| 750 | */ |
||
| 751 | public function getDate() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Provides a rough estimate of how long this post will take to read based on wikipedias answer to "How fast can a |
||
| 761 | * human read" of 200wpm. Source https://en.wikipedia.org/wiki/Speed_reading |
||
| 762 | * |
||
| 763 | * @return string |
||
| 764 | */ |
||
| 765 | public function getMinutesToRead() |
||
| 766 | { |
||
| 767 | $wpm = 200; |
||
| 768 | $wordCount = count(array_filter(explode(" ", strip_tags($this->Content)))); |
||
| 769 | |||
| 770 | return round($wordCount / $wpm, 0); |
||
| 771 | } |
||
| 772 | |||
| 773 | /** |
||
| 774 | * {@inheritdoc} |
||
| 775 | */ |
||
| 776 | protected function onBeforeWrite() |
||
| 784 | } |
||
| 785 |
This check marks private properties in classes that are never used. Those properties can be removed.