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 = array( |
||
59 | 'PublishDate' => 'Datetime', |
||
60 | 'AuthorNames' => 'Varchar(1024)', |
||
61 | 'Summary' => 'HTMLText' |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private static $has_one = array( |
||
68 | 'FeaturedImage' => 'SilverStripe\\Assets\\Image' |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private static $many_many = array( |
||
75 | 'Categories' => 'SilverStripe\\Blog\\Model\\BlogCategory', |
||
76 | 'Tags' => 'SilverStripe\\Blog\\Model\\BlogTag', |
||
77 | 'Authors' => Member::class |
||
78 | ); |
||
79 | |||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | private static $defaults = array( |
||
84 | 'ShowInMenus' => false, |
||
85 | 'InheritSideBar' => true, |
||
86 | 'ProvideComments' => true |
||
87 | ); |
||
88 | |||
89 | /** |
||
90 | * @var array |
||
91 | */ |
||
92 | private static $extensions = array( |
||
93 | 'SilverStripe\\Blog\\Model\\BlogPostFilter' |
||
94 | ); |
||
95 | |||
96 | /** |
||
97 | * @var array |
||
98 | */ |
||
99 | private static $searchable_fields = array( |
||
100 | 'Title' |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * @var array |
||
105 | */ |
||
106 | private static $summary_fields = array( |
||
107 | 'Title' |
||
108 | ); |
||
109 | |||
110 | /** |
||
111 | * @var array |
||
112 | */ |
||
113 | private static $casting = array( |
||
114 | 'Excerpt' => 'HTMLText', |
||
115 | 'Date' => 'DBDatetime' |
||
116 | ); |
||
117 | |||
118 | /** |
||
119 | * @var array |
||
120 | */ |
||
121 | private static $allowed_children = array(); |
||
122 | |||
123 | /** |
||
124 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
125 | * |
||
126 | * @var string |
||
127 | */ |
||
128 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
129 | |||
130 | /** |
||
131 | * @var bool |
||
132 | */ |
||
133 | private static $can_be_root = false; |
||
134 | |||
135 | /** |
||
136 | * This will display or hide the current class from the SiteTree. This variable can be |
||
137 | * configured using YAML. |
||
138 | * |
||
139 | * @var bool |
||
140 | */ |
||
141 | private static $show_in_sitetree = false; |
||
142 | |||
143 | /** |
||
144 | * Determine the role of the given member. |
||
145 | * |
||
146 | * Call be called via template to determine the current user. |
||
147 | * |
||
148 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
149 | * |
||
150 | * @param null|int|Member $member |
||
151 | * |
||
152 | * @return null|string |
||
153 | */ |
||
154 | public function RoleOf($member = null) |
||
174 | |||
175 | /** |
||
176 | * Determine if the given member is an author of this post. |
||
177 | * |
||
178 | * @param null|Member $member |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | View Code Duplication | public function isAuthor($member = null) |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function getCMSFields() |
||
322 | |||
323 | /** |
||
324 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
325 | * |
||
326 | * @return SS_List |
||
327 | */ |
||
328 | public function getCandidateAuthors() |
||
338 | |||
339 | /** |
||
340 | * Determine if this user can edit the authors list. |
||
341 | * |
||
342 | * @param null|int|Member $member |
||
343 | * |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function canEditAuthors($member = null) |
||
370 | |||
371 | /** |
||
372 | * @param null|int|Member $member |
||
373 | * |
||
374 | * @return null|Member |
||
375 | */ |
||
376 | View Code Duplication | protected function getMember($member = null) |
|
388 | |||
389 | /** |
||
390 | * Determine whether user can create new categories. |
||
391 | * |
||
392 | * @param null|int|Member $member |
||
393 | * |
||
394 | * @return bool |
||
395 | */ |
||
396 | View Code Duplication | public function canCreateCategories($member = null) |
|
412 | |||
413 | /** |
||
414 | * Determine whether user can create new tags. |
||
415 | * |
||
416 | * @param null|int|Member $member |
||
417 | * |
||
418 | * @return bool |
||
419 | */ |
||
420 | View Code Duplication | public function canCreateTags($member = null) |
|
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | * |
||
444 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
445 | */ |
||
446 | public function onBeforePublish() |
||
458 | |||
459 | /** |
||
460 | * {@inheritdoc} |
||
461 | * |
||
462 | * Sets blog relationship on all categories and tags assigned to this post. |
||
463 | */ |
||
464 | public function onAfterWrite() |
||
484 | |||
485 | /** |
||
486 | * {@inheritdoc} |
||
487 | */ |
||
488 | public function canView($member = null) |
||
510 | |||
511 | /** |
||
512 | * {@inheritdoc} |
||
513 | */ |
||
514 | public function canPublish($member = null) |
||
546 | |||
547 | /** |
||
548 | * {@inheritdoc} |
||
549 | */ |
||
550 | public function canEdit($member = null) |
||
574 | |||
575 | /** |
||
576 | * Returns the post excerpt. |
||
577 | * |
||
578 | * @param int $wordsToDisplay |
||
579 | * |
||
580 | * @return string |
||
581 | */ |
||
582 | public function Excerpt($wordsToDisplay = 30) |
||
589 | |||
590 | /** |
||
591 | * Returns a monthly archive link for the current blog post. |
||
592 | * |
||
593 | * @param string $type |
||
594 | * |
||
595 | * @return string |
||
596 | */ |
||
597 | public function getMonthlyArchiveLink($type = 'day') |
||
619 | |||
620 | /** |
||
621 | * Returns a yearly archive link for the current blog post. |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | public function getYearlyArchiveLink() |
||
634 | |||
635 | /** |
||
636 | * Resolves static and dynamic authors linked to this post. |
||
637 | * |
||
638 | * @return ArrayList |
||
639 | */ |
||
640 | public function getCredits() |
||
649 | |||
650 | /** |
||
651 | * Resolves dynamic authors linked to this post. |
||
652 | * |
||
653 | * @return ArrayList |
||
654 | */ |
||
655 | protected function getDynamicCredits() |
||
681 | |||
682 | /** |
||
683 | * Resolves static authors linked to this post. |
||
684 | * |
||
685 | * @return ArrayList |
||
686 | */ |
||
687 | protected function getStaticCredits() |
||
703 | |||
704 | /** |
||
705 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
706 | * |
||
707 | * @param bool $includeRelations |
||
708 | * |
||
709 | * @return array |
||
710 | */ |
||
711 | public function fieldLabels($includeRelations = true) |
||
719 | |||
720 | /** |
||
721 | * Proxy method for displaying the publish date in rss feeds. |
||
722 | * @see https://github.com/silverstripe/silverstripe-blog/issues/394 |
||
723 | * |
||
724 | * @return string|null |
||
725 | */ |
||
726 | public function getDate() |
||
730 | |||
731 | /** |
||
732 | * {@inheritdoc} |
||
733 | */ |
||
734 | protected function onBeforeWrite() |
||
742 | } |
||
743 |
This check marks private properties in classes that are never used. Those properties can be removed.