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 |
||
40 | class BlogPost extends Page |
||
41 | { |
||
42 | /** |
||
43 | * Same as above, but for list of users that can be |
||
44 | * given credit in the author field for blog posts |
||
45 | * @var string|bool false or group code |
||
46 | */ |
||
47 | private static $restrict_authors_to_group = false; |
||
|
|||
48 | |||
49 | /** |
||
50 | * {@inheritDoc} |
||
51 | * @var string |
||
52 | */ |
||
53 | private static $table_name = 'BlogPost'; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private static $db = [ |
||
59 | 'PublishDate' => 'Datetime', |
||
60 | 'AuthorNames' => 'Varchar(1024)', |
||
61 | 'Summary' => 'HTMLText' |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private static $has_one = [ |
||
68 | 'FeaturedImage' => Image::class |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private static $owns = [ |
||
75 | 'FeaturedImage', |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * @var array |
||
80 | */ |
||
81 | private static $many_many = [ |
||
82 | 'Categories' => BlogCategory::class, |
||
83 | 'Tags' => BlogTag::class, |
||
84 | 'Authors' => Member::class |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * @var array |
||
89 | */ |
||
90 | private static $defaults = [ |
||
91 | 'ShowInMenus' => false, |
||
92 | 'InheritSideBar' => true, |
||
93 | 'ProvideComments' => true |
||
94 | ]; |
||
95 | |||
96 | /** |
||
97 | * @var array |
||
98 | */ |
||
99 | private static $extensions = [ |
||
100 | BlogPostFilter::class |
||
101 | ]; |
||
102 | |||
103 | /** |
||
104 | * @var array |
||
105 | */ |
||
106 | private static $searchable_fields = [ |
||
107 | 'Title' |
||
108 | ]; |
||
109 | |||
110 | /** |
||
111 | * @var array |
||
112 | */ |
||
113 | private static $summary_fields = [ |
||
114 | 'Title' |
||
115 | ]; |
||
116 | |||
117 | /** |
||
118 | * @var array |
||
119 | */ |
||
120 | private static $casting = [ |
||
121 | 'Excerpt' => 'HTMLText', |
||
122 | 'Date' => 'DBDatetime' |
||
123 | ]; |
||
124 | |||
125 | /** |
||
126 | * @var array |
||
127 | */ |
||
128 | private static $allowed_children = []; |
||
129 | |||
130 | /** |
||
131 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
136 | |||
137 | /** |
||
138 | * @var bool |
||
139 | */ |
||
140 | private static $can_be_root = false; |
||
141 | |||
142 | /** |
||
143 | * This will display or hide the current class from the SiteTree. This variable can be |
||
144 | * configured using YAML. |
||
145 | * |
||
146 | * @var bool |
||
147 | */ |
||
148 | private static $show_in_sitetree = false; |
||
149 | |||
150 | /** |
||
151 | * This helps estimate how long an article will take to read, if your target audience |
||
152 | * is elderly then you should lower this value. See {@link getMinutesToRead()} |
||
153 | * |
||
154 | * @var int |
||
155 | */ |
||
156 | private static $minutes_to_read_wpm = 200; |
||
157 | |||
158 | /** |
||
159 | * Determine the role of the given member. |
||
160 | * |
||
161 | * Call be called via template to determine the current user. |
||
162 | * |
||
163 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
164 | * |
||
165 | * @param null|int|Member $member |
||
166 | * |
||
167 | * @return null|string |
||
168 | */ |
||
169 | public function RoleOf($member = null) |
||
189 | |||
190 | /** |
||
191 | * Determine if the given member is an author of this post. |
||
192 | * |
||
193 | * @param null|Member $member |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | View Code Duplication | public function isAuthor($member = null) |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | public function getCMSFields() |
||
341 | |||
342 | /** |
||
343 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
344 | * |
||
345 | * @return SS_List |
||
346 | */ |
||
347 | public function getCandidateAuthors() |
||
357 | |||
358 | /** |
||
359 | * Determine if this user can edit the authors list. |
||
360 | * |
||
361 | * @param null|int|Member $member |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function canEditAuthors($member = null) |
||
389 | |||
390 | /** |
||
391 | * @param null|int|Member $member |
||
392 | * |
||
393 | * @return null|Member |
||
394 | */ |
||
395 | View Code Duplication | protected function getMember($member = null) |
|
407 | |||
408 | /** |
||
409 | * Determine whether user can create new categories. |
||
410 | * |
||
411 | * @param null|int|Member $member |
||
412 | * |
||
413 | * @return bool |
||
414 | */ |
||
415 | View Code Duplication | public function canCreateCategories($member = null) |
|
431 | |||
432 | /** |
||
433 | * Determine whether user can create new tags. |
||
434 | * |
||
435 | * @param null|int|Member $member |
||
436 | * |
||
437 | * @return bool |
||
438 | */ |
||
439 | View Code Duplication | public function canCreateTags($member = null) |
|
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | * |
||
463 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
464 | */ |
||
465 | public function onBeforePublish() |
||
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | * |
||
481 | * Sets blog relationship on all categories and tags assigned to this post. |
||
482 | */ |
||
483 | public function onAfterWrite() |
||
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | */ |
||
507 | public function canView($member = null) |
||
534 | |||
535 | /** |
||
536 | * {@inheritdoc} |
||
537 | */ |
||
538 | public function canPublish($member = null) |
||
570 | |||
571 | /** |
||
572 | * {@inheritdoc} |
||
573 | */ |
||
574 | public function canEdit($member = null) |
||
598 | |||
599 | /** |
||
600 | * Returns the post excerpt. |
||
601 | * |
||
602 | * @param int $wordsToDisplay |
||
603 | * |
||
604 | * @return string |
||
605 | */ |
||
606 | public function Excerpt($wordsToDisplay = 30) |
||
613 | |||
614 | /** |
||
615 | * Returns a monthly archive link for the current blog post. |
||
616 | * |
||
617 | * @param string $type |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | public function getMonthlyArchiveLink($type = 'day') |
||
643 | |||
644 | /** |
||
645 | * Returns a yearly archive link for the current blog post. |
||
646 | * |
||
647 | * @return string |
||
648 | */ |
||
649 | public function getYearlyArchiveLink() |
||
658 | |||
659 | /** |
||
660 | * Resolves static and dynamic authors linked to this post. |
||
661 | * |
||
662 | * @return ArrayList |
||
663 | */ |
||
664 | public function getCredits() |
||
673 | |||
674 | /** |
||
675 | * Resolves dynamic authors linked to this post. |
||
676 | * |
||
677 | * @return ArrayList |
||
678 | */ |
||
679 | protected function getDynamicCredits() |
||
705 | |||
706 | /** |
||
707 | * Resolves static authors linked to this post. |
||
708 | * |
||
709 | * @return ArrayList |
||
710 | */ |
||
711 | protected function getStaticCredits() |
||
727 | |||
728 | /** |
||
729 | * Checks to see if User Profiles has been disabled via config |
||
730 | * |
||
731 | * @return bool |
||
732 | */ |
||
733 | public function getProfilesDisabled() |
||
737 | |||
738 | /** |
||
739 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
740 | * |
||
741 | * @param bool $includeRelations |
||
742 | * |
||
743 | * @return array |
||
744 | */ |
||
745 | public function fieldLabels($includeRelations = true) |
||
753 | |||
754 | /** |
||
755 | * Proxy method for displaying the publish date in rss feeds. |
||
756 | * @see https://github.com/silverstripe/silverstripe-blog/issues/394 |
||
757 | * |
||
758 | * @return string|null |
||
759 | */ |
||
760 | public function getDate() |
||
767 | |||
768 | /** |
||
769 | * Provides a rough estimate of how long this post will take to read based on wikipedias answer to "How fast can a |
||
770 | * human read" of 200wpm. Source https://en.wikipedia.org/wiki/Speed_reading |
||
771 | * |
||
772 | * @param null|integer $wpm |
||
773 | * |
||
774 | * @return string |
||
775 | */ |
||
776 | public function MinutesToRead($wpm = null) |
||
792 | |||
793 | /** |
||
794 | * {@inheritdoc} |
||
795 | */ |
||
796 | protected function onBeforeWrite() |
||
804 | } |
||
805 |
This check marks private properties in classes that are never used. Those properties can be removed.