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 |
||
| 16 | class BlogController extends PageController |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private static $allowed_actions = array( |
||
|
|
|||
| 22 | 'archive', |
||
| 23 | 'tag', |
||
| 24 | 'category', |
||
| 25 | 'rss', |
||
| 26 | 'profile' |
||
| 27 | ); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private static $url_handlers = array( |
||
| 33 | 'tag/$Tag!/$Rss' => 'tag', |
||
| 34 | 'category/$Category!/$Rss' => 'category', |
||
| 35 | 'archive/$Year!/$Month/$Day' => 'archive', |
||
| 36 | 'profile/$URLSegment!' => 'profile' |
||
| 37 | ); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private static $casting = array( |
||
| 43 | 'MetaTitle' => 'Text', |
||
| 44 | 'FilterDescription' => 'Text' |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The current Blog Post DataList query. |
||
| 49 | * |
||
| 50 | * @var DataList |
||
| 51 | */ |
||
| 52 | protected $blogPosts; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function index() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Renders a Blog Member's profile. |
||
| 71 | * |
||
| 72 | * @return HTTPResponse |
||
| 73 | */ |
||
| 74 | public function profile() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the Member associated with the current URL segment. |
||
| 89 | * |
||
| 90 | * @return null|Member |
||
| 91 | */ |
||
| 92 | public function getCurrentProfile() |
||
| 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() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Renders the blog posts for a given category. |
||
| 258 | * |
||
| 259 | * @return null|HTTPResponse |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function category() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Category Getter for use in templates. |
||
| 282 | * |
||
| 283 | * @return null|BlogCategory |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function getCurrentCategory() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get the meta title for the current action. |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getMetaTitle() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns a description of the current filter. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getFilterDescription() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns a list of paginated blog posts based on the BlogPost dataList. |
||
| 407 | * |
||
| 408 | * @return PaginatedList |
||
| 409 | */ |
||
| 410 | public function PaginatedList() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Displays an RSS feed of blog posts. |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public function rss() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Returns the current archive date. |
||
| 451 | * |
||
| 452 | * @return null|Date |
||
| 453 | */ |
||
| 454 | public function getArchiveDate() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns a link to the RSS feed. |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function getRSSLink() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Displays an RSS feed of the given blog posts. |
||
| 491 | * |
||
| 492 | * @param DataList $blogPosts |
||
| 493 | * @param string $link |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | protected function rssFeed($blogPosts, $link) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Returns true if the $Rss sub-action for categories/tags has been set to "rss" |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | protected function isRSS() |
||
| 516 | } |
||
| 517 |
This check marks private properties in classes that are never used. Those properties can be removed.