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 |
||
44 | class BlogPost extends Page |
||
45 | { |
||
46 | /** |
||
47 | * Same as above, but for list of users that can be |
||
48 | * given credit in the author field for blog posts |
||
49 | * @var string|bool false or group code |
||
50 | */ |
||
51 | private static $restrict_authors_to_group = false; |
||
|
|||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | * @var string |
||
56 | */ |
||
57 | private static $table_name = 'BlogPost'; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private static $db = array( |
||
63 | 'PublishDate' => 'Datetime', |
||
64 | 'AuthorNames' => 'Varchar(1024)', |
||
65 | 'Summary' => 'HTMLText' |
||
66 | ); |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | private static $has_one = array( |
||
72 | 'FeaturedImage' => Image::class |
||
73 | ); |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | private static $many_many = array( |
||
79 | 'Categories' => BlogCategory::class, |
||
80 | 'Tags' => BlogTag::class, |
||
81 | 'Authors' => Member::class |
||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | private static $defaults = array( |
||
88 | 'ShowInMenus' => false, |
||
89 | 'InheritSideBar' => true, |
||
90 | 'ProvideComments' => true |
||
91 | ); |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | */ |
||
96 | private static $extensions = array( |
||
97 | BlogPostFilter::class |
||
98 | ); |
||
99 | |||
100 | /** |
||
101 | * @var array |
||
102 | */ |
||
103 | private static $searchable_fields = array( |
||
104 | 'Title' |
||
105 | ); |
||
106 | |||
107 | /** |
||
108 | * @var array |
||
109 | */ |
||
110 | private static $summary_fields = array( |
||
111 | 'Title' |
||
112 | ); |
||
113 | |||
114 | /** |
||
115 | * @var array |
||
116 | */ |
||
117 | private static $casting = array( |
||
118 | 'Excerpt' => 'HTMLText', |
||
119 | 'Date' => 'DBDatetime' |
||
120 | ); |
||
121 | |||
122 | /** |
||
123 | * @var array |
||
124 | */ |
||
125 | private static $allowed_children = array(); |
||
126 | |||
127 | /** |
||
128 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
129 | * |
||
130 | * @var string |
||
131 | */ |
||
132 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
133 | |||
134 | /** |
||
135 | * @var bool |
||
136 | */ |
||
137 | private static $can_be_root = false; |
||
138 | |||
139 | /** |
||
140 | * This will display or hide the current class from the SiteTree. This variable can be |
||
141 | * configured using YAML. |
||
142 | * |
||
143 | * @var bool |
||
144 | */ |
||
145 | private static $show_in_sitetree = false; |
||
146 | |||
147 | /** |
||
148 | * Determine the role of the given member. |
||
149 | * |
||
150 | * Call be called via template to determine the current user. |
||
151 | * |
||
152 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
153 | * |
||
154 | * @param null|int|Member $member |
||
155 | * |
||
156 | * @return null|string |
||
157 | */ |
||
158 | public function RoleOf($member = null) |
||
178 | |||
179 | /** |
||
180 | * Determine if the given member is an author of this post. |
||
181 | * |
||
182 | * @param null|Member $member |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | View Code Duplication | public function isAuthor($member = null) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function getCMSFields() |
||
326 | |||
327 | /** |
||
328 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
329 | * |
||
330 | * @return SS_List |
||
331 | */ |
||
332 | public function getCandidateAuthors() |
||
342 | |||
343 | /** |
||
344 | * Determine if this user can edit the authors list. |
||
345 | * |
||
346 | * @param null|int|Member $member |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function canEditAuthors($member = null) |
||
374 | |||
375 | /** |
||
376 | * @param null|int|Member $member |
||
377 | * |
||
378 | * @return null|Member |
||
379 | */ |
||
380 | View Code Duplication | protected function getMember($member = null) |
|
392 | |||
393 | /** |
||
394 | * Determine whether user can create new categories. |
||
395 | * |
||
396 | * @param null|int|Member $member |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | View Code Duplication | public function canCreateCategories($member = null) |
|
416 | |||
417 | /** |
||
418 | * Determine whether user can create new tags. |
||
419 | * |
||
420 | * @param null|int|Member $member |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | View Code Duplication | public function canCreateTags($member = null) |
|
444 | |||
445 | /** |
||
446 | * {@inheritdoc} |
||
447 | * |
||
448 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
449 | */ |
||
450 | public function onBeforePublish() |
||
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | * |
||
466 | * Sets blog relationship on all categories and tags assigned to this post. |
||
467 | */ |
||
468 | public function onAfterWrite() |
||
488 | |||
489 | /** |
||
490 | * {@inheritdoc} |
||
491 | */ |
||
492 | public function canView($member = null) |
||
514 | |||
515 | /** |
||
516 | * {@inheritdoc} |
||
517 | */ |
||
518 | public function canPublish($member = null) |
||
550 | |||
551 | /** |
||
552 | * {@inheritdoc} |
||
553 | */ |
||
554 | public function canEdit($member = null) |
||
578 | |||
579 | /** |
||
580 | * Returns the post excerpt. |
||
581 | * |
||
582 | * @param int $wordsToDisplay |
||
583 | * |
||
584 | * @return string |
||
585 | */ |
||
586 | public function Excerpt($wordsToDisplay = 30) |
||
593 | |||
594 | /** |
||
595 | * Returns a monthly archive link for the current blog post. |
||
596 | * |
||
597 | * @param string $type |
||
598 | * |
||
599 | * @return string |
||
600 | */ |
||
601 | public function getMonthlyArchiveLink($type = 'day') |
||
623 | |||
624 | /** |
||
625 | * Returns a yearly archive link for the current blog post. |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | public function getYearlyArchiveLink() |
||
638 | |||
639 | /** |
||
640 | * Resolves static and dynamic authors linked to this post. |
||
641 | * |
||
642 | * @return ArrayList |
||
643 | */ |
||
644 | public function getCredits() |
||
653 | |||
654 | /** |
||
655 | * Resolves dynamic authors linked to this post. |
||
656 | * |
||
657 | * @return ArrayList |
||
658 | */ |
||
659 | protected function getDynamicCredits() |
||
685 | |||
686 | /** |
||
687 | * Resolves static authors linked to this post. |
||
688 | * |
||
689 | * @return ArrayList |
||
690 | */ |
||
691 | protected function getStaticCredits() |
||
707 | |||
708 | /** |
||
709 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
710 | * |
||
711 | * @param bool $includeRelations |
||
712 | * |
||
713 | * @return array |
||
714 | */ |
||
715 | public function fieldLabels($includeRelations = true) |
||
723 | |||
724 | /** |
||
725 | * Proxy method for displaying the publish date in rss feeds. |
||
726 | * @see https://github.com/silverstripe/silverstripe-blog/issues/394 |
||
727 | * |
||
728 | * @return string|null |
||
729 | */ |
||
730 | public function getDate() |
||
734 | |||
735 | /** |
||
736 | * {@inheritdoc} |
||
737 | */ |
||
738 | protected function onBeforeWrite() |
||
746 | } |
||
747 |
This check marks private properties in classes that are never used. Those properties can be removed.