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 |
||
14 | class BlogController extends PageController |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private static $allowed_actions = [ |
||
|
|||
20 | 'archive', |
||
21 | 'tag', |
||
22 | 'category', |
||
23 | 'rss', |
||
24 | 'profile' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $url_handlers = [ |
||
31 | 'tag/$Tag!/$Rss' => 'tag', |
||
32 | 'category/$Category!/$Rss' => 'category', |
||
33 | 'archive/$Year!/$Month/$Day' => 'archive', |
||
34 | 'profile/$URLSegment!' => 'profile' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $casting = [ |
||
41 | 'MetaTitle' => 'Text', |
||
42 | 'FilterDescription' => 'Text' |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * The current Blog Post DataList query. |
||
47 | * |
||
48 | * @var DataList |
||
49 | */ |
||
50 | protected $blogPosts; |
||
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | public function index() |
||
56 | { |
||
57 | /** |
||
58 | * @var Blog $dataRecord |
||
59 | */ |
||
60 | $dataRecord = $this->dataRecord; |
||
61 | |||
62 | $this->blogPosts = $dataRecord->getBlogPosts(); |
||
63 | |||
64 | return $this->render(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Renders a Blog Member's profile. |
||
69 | * |
||
70 | * @return HTTPResponse |
||
71 | */ |
||
72 | public function profile() |
||
73 | { |
||
74 | $profile = $this->getCurrentProfile(); |
||
75 | |||
76 | if (!$profile) { |
||
77 | return $this->httpError(404, 'Not Found'); |
||
78 | } |
||
79 | |||
80 | $this->blogPosts = $this->getCurrentProfilePosts(); |
||
81 | |||
82 | return $this->render(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get the Member associated with the current URL segment. |
||
87 | * |
||
88 | * @return null|Member |
||
89 | */ |
||
90 | public function getCurrentProfile() |
||
91 | { |
||
92 | $urlSegment = $this->request->param('URLSegment'); |
||
93 | |||
94 | if ($urlSegment) { |
||
95 | $filter = URLSegmentFilter::create(); |
||
96 | |||
97 | return Member::get() |
||
98 | ->filter('URLSegment', $filter->filter($urlSegment)) |
||
99 | ->first(); |
||
100 | } |
||
101 | |||
102 | return null; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get posts related to the current Member profile. |
||
107 | * |
||
108 | * @return null|DataList |
||
109 | */ |
||
110 | public function getCurrentProfilePosts() |
||
120 | |||
121 | /** |
||
122 | * Renders an archive for a specified date. This can be by year or year/month. |
||
123 | * |
||
124 | * @return null|HTTPResponse |
||
125 | */ |
||
126 | public function archive() |
||
155 | |||
156 | /** |
||
157 | * Fetches the archive year from the url. |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | public function getArchiveYear() |
||
173 | |||
174 | /** |
||
175 | * Fetches the archive money from the url. |
||
176 | * |
||
177 | * @return null|int |
||
178 | */ |
||
179 | public function getArchiveMonth() |
||
193 | |||
194 | /** |
||
195 | * Fetches the archive day from the url. |
||
196 | * |
||
197 | * @return null|int |
||
198 | */ |
||
199 | public function getArchiveDay() |
||
211 | |||
212 | /** |
||
213 | * Renders the blog posts for a given tag. |
||
214 | * |
||
215 | * @return null|HTTPResponse |
||
216 | */ |
||
217 | View Code Duplication | public function tag() |
|
235 | |||
236 | /** |
||
237 | * Tag Getter for use in templates. |
||
238 | * |
||
239 | * @return null|BlogTag |
||
240 | */ |
||
241 | View Code Duplication | public function getCurrentTag() |
|
257 | |||
258 | /** |
||
259 | * Renders the blog posts for a given category. |
||
260 | * |
||
261 | * @return null|HTTPResponse |
||
262 | */ |
||
263 | View Code Duplication | public function category() |
|
281 | |||
282 | /** |
||
283 | * Category Getter for use in templates. |
||
284 | * |
||
285 | * @return null|BlogCategory |
||
286 | */ |
||
287 | View Code Duplication | public function getCurrentCategory() |
|
303 | |||
304 | /** |
||
305 | * Get the meta title for the current action. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getMetaTitle() |
||
322 | |||
323 | /** |
||
324 | * Returns a description of the current filter. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function getFilterDescription() |
||
408 | |||
409 | /** |
||
410 | * Returns a list of paginated blog posts based on the BlogPost dataList. |
||
411 | * |
||
412 | * @return PaginatedList |
||
413 | */ |
||
414 | public function PaginatedList() |
||
435 | |||
436 | |||
437 | /** |
||
438 | * Returns the absolute link to the next page for use in the page meta tags. This helps search engines |
||
439 | * find the pagination and index all pages properly. |
||
440 | * |
||
441 | * @example "<% if $PaginationAbsoluteNextLink %><link rel="next" href="$PaginationAbsoluteNextLink"><% end_if %>" |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | public function PaginationAbsoluteNextLink() |
||
452 | |||
453 | /** |
||
454 | * Returns the absolute link to the previous page for use in the page meta tags. This helps search engines |
||
455 | * find the pagination and index all pages properly. |
||
456 | * |
||
457 | * @example "<% if $PaginationAbsolutePrevLink %><link rel="prev" href="$PaginationAbsolutePrevLink"><% end_if %>" |
||
458 | * |
||
459 | * @return string |
||
460 | */ |
||
461 | public function PaginationAbsolutePrevLink() |
||
468 | |||
469 | /** |
||
470 | * Displays an RSS feed of blog posts. |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | public function rss() |
||
485 | |||
486 | /** |
||
487 | * Returns the current archive date. |
||
488 | * |
||
489 | * @return null|Date |
||
490 | */ |
||
491 | public function getArchiveDate() |
||
515 | |||
516 | /** |
||
517 | * Returns a link to the RSS feed. |
||
518 | * |
||
519 | * @return string |
||
520 | */ |
||
521 | public function getRSSLink() |
||
525 | |||
526 | /** |
||
527 | * Displays an RSS feed of the given blog posts. |
||
528 | * |
||
529 | * @param DataList $blogPosts |
||
530 | * @param string $link |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | protected function rssFeed($blogPosts, $link) |
||
542 | |||
543 | /** |
||
544 | * Returns true if the $Rss sub-action for categories/tags has been set to "rss" |
||
545 | * |
||
546 | * @return bool |
||
547 | */ |
||
548 | protected function isRSS() |
||
553 | } |
||
554 |
This check marks private properties in classes that are never used. Those properties can be removed.