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() |
||
172 | { |
||
173 | Requirements::css(BLOGGER_DIR . '/css/cms.css'); |
||
174 | Requirements::javascript(BLOGGER_DIR . '/js/cms.js'); |
||
175 | |||
176 | $self =& $this; |
||
177 | |||
178 | $this->beforeUpdateCMSFields(function ($fields) use ($self) { |
||
179 | $uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Featured Image')); |
||
180 | $uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')); |
||
181 | |||
182 | /** |
||
183 | * @var FieldList $fields |
||
184 | */ |
||
185 | $fields->insertAfter($uploadField, 'Content'); |
||
186 | |||
187 | $summary = HtmlEditorField::create('Summary', false); |
||
188 | $summary->setRows(5); |
||
189 | $summary->setDescription(_t( |
||
190 | 'BlogPost.SUMMARY_DESCRIPTION', |
||
191 | 'If no summary is specified the first 30 words will be used.' |
||
192 | )); |
||
193 | |||
194 | $summaryHolder = ToggleCompositeField::create( |
||
195 | 'CustomSummary', |
||
196 | _t('BlogPost.CUSTOMSUMMARY', 'Add A Custom Summary'), |
||
197 | array( |
||
198 | $summary, |
||
199 | ) |
||
200 | ); |
||
201 | $summaryHolder->setHeadingLevel(4); |
||
202 | $summaryHolder->addExtraClass('custom-summary'); |
||
203 | |||
204 | $fields->insertAfter($summaryHolder, 'FeaturedImage'); |
||
205 | |||
206 | $fields->push(HiddenField::create('MenuTitle')); |
||
207 | |||
208 | $urlSegment = $fields->dataFieldByName('URLSegment'); |
||
209 | $urlSegment->setURLPrefix($self->Parent()->RelativeLink()); |
||
210 | |||
211 | $fields->removeFieldsFromTab('Root.Main', array( |
||
212 | 'MenuTitle', |
||
213 | 'URLSegment', |
||
214 | )); |
||
215 | |||
216 | $authorField = ListboxField::create( |
||
217 | 'Authors', |
||
218 | _t('BlogPost.Authors', 'Authors'), |
||
219 | $self->getCandidateAuthors()->map()->toArray() |
||
220 | )->setMultiple(true); |
||
221 | |||
222 | $authorNames = TextField::create( |
||
223 | 'AuthorNames', |
||
224 | _t('BlogPost.AdditionalCredits', 'Additional Credits'), |
||
225 | null, |
||
226 | 1024 |
||
227 | )->setDescription(_t( |
||
228 | 'BlogPost.AdditionalCredits_Description', |
||
229 | 'If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.') |
||
230 | ); |
||
231 | |||
232 | if (!$self->canEditAuthors()) { |
||
233 | $authorField = $authorField->performDisabledTransformation(); |
||
234 | $authorNames = $authorNames->performDisabledTransformation(); |
||
235 | } |
||
236 | |||
237 | $publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date')); |
||
238 | $publishDate->getDateField()->setConfig('showcalendar', true); |
||
239 | if (!$self->PublishDate) { |
||
240 | $publishDate->setDescription(_t( |
||
241 | 'BlogPost.PublishDate_Description', |
||
242 | 'Will be set to "now" if published without a value.') |
||
243 | ); |
||
244 | } |
||
245 | |||
246 | // Get categories and tags |
||
247 | $parent = $self->Parent(); |
||
248 | $categories = $parent instanceof Blog |
||
249 | ? $parent->Categories() |
||
250 | : BlogCategory::get(); |
||
251 | $tags = $parent instanceof Blog |
||
252 | ? $parent->Tags() |
||
253 | : BlogTag::get(); |
||
254 | |||
255 | $options = BlogAdminSidebar::create( |
||
256 | $publishDate, |
||
257 | $urlSegment, |
||
258 | TagField::create( |
||
259 | 'Categories', |
||
260 | _t('BlogPost.Categories', 'Categories'), |
||
261 | $categories, |
||
262 | $self->Categories() |
||
263 | ) |
||
264 | ->setCanCreate($self->canCreateCategories()) |
||
265 | ->setShouldLazyLoad(true), |
||
266 | TagField::create( |
||
267 | 'Tags', |
||
268 | _t('BlogPost.Tags', 'Tags'), |
||
269 | $tags, |
||
270 | $self->Tags() |
||
271 | ) |
||
272 | ->setCanCreate($self->canCreateTags()) |
||
273 | ->setShouldLazyLoad(true), |
||
274 | $authorField, |
||
275 | $authorNames |
||
276 | )->setTitle('Post Options'); |
||
277 | |||
278 | $options->setName('blog-admin-sidebar'); |
||
279 | |||
280 | $fields->insertBefore($options, 'Root'); |
||
281 | }); |
||
282 | |||
283 | $fields = parent::getCMSFields(); |
||
284 | |||
285 | $fields->fieldByName('Root')->setTemplate('TabSet_holder'); |
||
286 | |||
287 | return $fields; |
||
288 | } |
||
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 | * Proxy method for displaying the publish date in rss feeds. |
||
691 | * @see https://github.com/silverstripe/silverstripe-blog/issues/394 |
||
692 | * |
||
693 | * @return string|null |
||
694 | */ |
||
695 | public function getDate() |
||
699 | |||
700 | /** |
||
701 | * {@inheritdoc} |
||
702 | */ |
||
703 | protected function onBeforeWrite() |
||
711 | } |
||
712 | |||
720 |
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.