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 BlogController 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 BlogController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class BlogController extends PageController |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private static $allowed_actions = [ |
||
26 | 'archive', |
||
27 | 'tag', |
||
28 | 'category', |
||
29 | 'rss', |
||
30 | 'profile' |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private static $url_handlers = [ |
||
37 | 'tag/$Tag!/$Rss' => 'tag', |
||
38 | 'category/$Category!/$Rss' => 'category', |
||
39 | 'archive/$Year!/$Month/$Day' => 'archive', |
||
40 | 'profile/$Profile!' => 'profile' |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private static $casting = [ |
||
47 | 'MetaTitle' => 'Text', |
||
48 | 'FilterDescription' => 'Text' |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * If enabled, blog author profiles will be turned off for this site |
||
53 | * |
||
54 | * @config |
||
55 | * @var bool |
||
56 | */ |
||
57 | private static $disable_profiles = false; |
||
58 | |||
59 | /** |
||
60 | * The current Blog Post DataList query. |
||
61 | * |
||
62 | * @var DataList |
||
63 | */ |
||
64 | protected $blogPosts; |
||
65 | |||
66 | /** |
||
67 | * Renders a Blog Member's profile. |
||
68 | * |
||
69 | * @throws HTTPResponse_Exception |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function profile() |
||
87 | |||
88 | /** |
||
89 | * Get the Member associated with the current URL segment. |
||
90 | * |
||
91 | * @return null|Member|BlogMemberExtension |
||
92 | */ |
||
93 | public function getCurrentProfile() |
||
105 | |||
106 | /** |
||
107 | * Get URL Segment of current profile |
||
108 | * |
||
109 | * @return null|string |
||
110 | */ |
||
111 | View Code Duplication | public function getCurrentProfileURLSegment() |
|
126 | |||
127 | /** |
||
128 | * Get posts related to the current Member profile. |
||
129 | * |
||
130 | * @return null|DataList|BlogPost[] |
||
131 | */ |
||
132 | public function getCurrentProfilePosts() |
||
142 | |||
143 | /** |
||
144 | * Renders an archive for a specified date. This can be by year or year/month. |
||
145 | * |
||
146 | * @return $this |
||
147 | * @throws HTTPResponse_Exception |
||
148 | */ |
||
149 | public function archive() |
||
164 | |||
165 | /** |
||
166 | * Fetches the archive year from the url. |
||
167 | * |
||
168 | * Returns int if valid, current year if not provided, false if invalid value |
||
169 | * |
||
170 | * @return int|false |
||
171 | */ |
||
172 | public function getArchiveYear() |
||
188 | |||
189 | /** |
||
190 | * Fetches the archive money from the url. |
||
191 | * |
||
192 | * Returns int if valid, null if not provided, false if invalid value |
||
193 | * |
||
194 | * @return null|int|false |
||
195 | */ |
||
196 | public function getArchiveMonth() |
||
215 | |||
216 | /** |
||
217 | * Fetches the archive day from the url. |
||
218 | * |
||
219 | * Returns int if valid, null if not provided, false if invalid value |
||
220 | * |
||
221 | * @return null|int|false |
||
222 | */ |
||
223 | public function getArchiveDay() |
||
246 | |||
247 | /** |
||
248 | * Renders the blog posts for a given tag. |
||
249 | * |
||
250 | * @return DBHTMLText|$this |
||
251 | * @throws HTTPResponse_Exception |
||
252 | */ |
||
253 | View Code Duplication | public function tag() |
|
276 | |||
277 | /** |
||
278 | * Get BlogTag assigned to current filter |
||
279 | * |
||
280 | * @return null|BlogTag |
||
281 | */ |
||
282 | public function getCurrentTag() |
||
296 | |||
297 | /** |
||
298 | * Get URLSegment of selected category (not: URLEncoded based on multibyte) |
||
299 | * |
||
300 | * @return string|null |
||
301 | */ |
||
302 | View Code Duplication | public function getCurrentTagURLSegment() |
|
314 | |||
315 | /** |
||
316 | * Renders the blog posts for a given category. |
||
317 | * |
||
318 | * @return DBHTMLText|$this |
||
319 | * @throws HTTPResponse_Exception |
||
320 | */ |
||
321 | View Code Duplication | public function category() |
|
341 | |||
342 | /** |
||
343 | * Category Getter for use in templates. |
||
344 | * |
||
345 | * @return null|BlogCategory |
||
346 | */ |
||
347 | public function getCurrentCategory() |
||
361 | |||
362 | /** |
||
363 | * Get URLSegment of selected category |
||
364 | * |
||
365 | * @return string|null |
||
366 | */ |
||
367 | View Code Duplication | public function getCurrentCategoryURLSegment() |
|
379 | |||
380 | /** |
||
381 | * Get the meta title for the current action. |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | public function getMetaTitle() |
||
398 | |||
399 | /** |
||
400 | * Returns a description of the current filter. |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getFilterDescription() |
||
484 | |||
485 | /** |
||
486 | * Get filtered blog posts |
||
487 | * |
||
488 | * @return DataList|BlogPost[] |
||
489 | */ |
||
490 | public function getFilteredPosts() |
||
494 | |||
495 | /** |
||
496 | * Set filtered posts |
||
497 | * |
||
498 | * @param SS_List|BlogPost[] $posts |
||
499 | * @return $this |
||
500 | */ |
||
501 | public function setFilteredPosts($posts) |
||
506 | |||
507 | /** |
||
508 | * Returns a list of paginated blog posts based on the BlogPost dataList. |
||
509 | * |
||
510 | * @return PaginatedList |
||
511 | */ |
||
512 | public function PaginatedList() |
||
533 | |||
534 | |||
535 | /** |
||
536 | * Returns the absolute link to the next page for use in the page meta tags. This helps search engines |
||
537 | * find the pagination and index all pages properly. |
||
538 | * |
||
539 | * @example "<% if $PaginationAbsoluteNextLink %><link rel="next" href="$PaginationAbsoluteNextLink"><% end_if %>" |
||
540 | * |
||
541 | * @return string|null |
||
542 | */ |
||
543 | public function PaginationAbsoluteNextLink() |
||
552 | |||
553 | /** |
||
554 | * Returns the absolute link to the previous page for use in the page meta tags. This helps search engines |
||
555 | * find the pagination and index all pages properly. |
||
556 | * |
||
557 | * @example "<% if $PaginationAbsolutePrevLink %><link rel="prev" href="$PaginationAbsolutePrevLink"><% end_if %>" |
||
558 | * |
||
559 | * @return string|null |
||
560 | */ |
||
561 | public function PaginationAbsolutePrevLink() |
||
570 | |||
571 | /** |
||
572 | * Displays an RSS feed of blog posts. |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function rss() |
||
580 | |||
581 | /** |
||
582 | * Returns the current archive date. |
||
583 | * |
||
584 | * @return null|DBDatetime |
||
585 | */ |
||
586 | public function getArchiveDate() |
||
610 | |||
611 | /** |
||
612 | * Returns a link to the RSS feed. |
||
613 | * |
||
614 | * @return string |
||
615 | */ |
||
616 | public function getRSSLink() |
||
620 | |||
621 | /** |
||
622 | * Displays an RSS feed of the given blog posts. |
||
623 | * |
||
624 | * @param DataList $blogPosts |
||
625 | * @param string $link |
||
626 | * |
||
627 | * @return DBHTMLText |
||
628 | */ |
||
629 | protected function rssFeed($blogPosts, $link) |
||
642 | |||
643 | /** |
||
644 | * Returns true if the $Rss sub-action for categories/tags has been set to "rss" |
||
645 | * |
||
646 | * @return bool |
||
647 | */ |
||
648 | protected function isRSS() |
||
652 | } |
||
653 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: