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 CommentingController 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 CommentingController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class CommentingController extends Controller |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * {@inheritDoc} |
||
| 29 | */ |
||
| 30 | private static $allowed_actions = array( |
||
| 31 | 'delete', |
||
| 32 | 'spam', |
||
| 33 | 'ham', |
||
| 34 | 'approve', |
||
| 35 | 'rss', |
||
| 36 | 'CommentsForm', |
||
| 37 | 'reply', |
||
| 38 | 'doPostComment', |
||
| 39 | 'doPreviewComment' |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritDoc} |
||
| 44 | */ |
||
| 45 | private static $url_handlers = array( |
||
| 46 | 'reply/$ParentCommentID//$ID/$OtherID' => 'reply', |
||
| 47 | ); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Fields required for this form |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | * @config |
||
| 54 | */ |
||
| 55 | private static $required_fields = array( |
||
| 56 | 'Name', |
||
| 57 | 'Email', |
||
| 58 | 'Comment' |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Parent class this commenting form is for |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $parentClass = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The record this commenting form is for |
||
| 70 | * |
||
| 71 | * @var DataObject |
||
| 72 | */ |
||
| 73 | private $ownerRecord = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Parent controller record |
||
| 77 | * |
||
| 78 | * @var Controller |
||
| 79 | */ |
||
| 80 | private $ownerController = null; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Backup url to return to |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $fallbackReturnURL = null; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set the parent class name to use |
||
| 91 | * |
||
| 92 | * @param string $class |
||
| 93 | */ |
||
| 94 | public function setParentClass($class) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the parent class name used |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getParentClass() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Encode a fully qualified class name to a URL-safe version |
||
| 111 | * |
||
| 112 | * @param string $input |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function encodeClassName($input) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Decode an "encoded" fully qualified class name back to its original |
||
| 122 | * |
||
| 123 | * @param string $input |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function decodeClassName($input) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Set the record this controller is working on |
||
| 133 | * |
||
| 134 | * @param DataObject $record |
||
| 135 | */ |
||
| 136 | public function setOwnerRecord($record) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the record |
||
| 143 | * |
||
| 144 | * @return DataObject |
||
| 145 | */ |
||
| 146 | public function getOwnerRecord() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set the parent controller |
||
| 153 | * |
||
| 154 | * @param Controller $controller |
||
| 155 | */ |
||
| 156 | public function setOwnerController($controller) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the parent controller |
||
| 163 | * |
||
| 164 | * @return Controller |
||
| 165 | */ |
||
| 166 | public function getOwnerController() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the commenting option for the current state |
||
| 173 | * |
||
| 174 | * @param string $key |
||
| 175 | * @return mixed Result if the setting is available, or null otherwise |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function getOption($key) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Returns all the commenting options for the current instance. |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function getOptions() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Workaround for generating the link to this controller |
||
| 215 | * |
||
| 216 | * @param string $action |
||
| 217 | * @param int $id |
||
| 218 | * @param string $other |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function Link($action = '', $id = '', $other = '') |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Outputs the RSS feed of comments |
||
| 228 | * |
||
| 229 | * @return HTMLText |
||
| 230 | */ |
||
| 231 | public function rss() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Return an RSSFeed of comments for a given set of comments or all |
||
| 238 | * comments on the website. |
||
| 239 | * |
||
| 240 | * @param HTTPRequest |
||
| 241 | * |
||
| 242 | * @return RSSFeed |
||
| 243 | */ |
||
| 244 | public function getFeed(HTTPRequest $request) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Deletes a given {@link Comment} via the URL. |
||
| 294 | */ |
||
| 295 | View Code Duplication | public function delete() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Marks a given {@link Comment} as spam. Removes the comment from display |
||
| 317 | */ |
||
| 318 | View Code Duplication | public function spam() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Marks a given {@link Comment} as ham (not spam). |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function ham() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Marks a given {@link Comment} as approved. |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function approve() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Redirect back to referer if available, ensuring that only site URLs |
||
| 376 | * are allowed to avoid phishing. If it's an AJAX request render the |
||
| 377 | * comment in it's new state |
||
| 378 | */ |
||
| 379 | private function renderChangedCommentState($comment) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns the comment referenced in the URL (by ID). Permission checking |
||
| 405 | * should be done in the callee. |
||
| 406 | * |
||
| 407 | * @return Comment|false |
||
| 408 | */ |
||
| 409 | public function getComment() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Create a reply form for a specified comment |
||
| 426 | * |
||
| 427 | * @param Comment $comment |
||
| 428 | * @return Form |
||
| 429 | */ |
||
| 430 | public function ReplyForm($comment) |
||
| 449 | |||
| 450 | |||
| 451 | /** |
||
| 452 | * Request handler for reply form. |
||
| 453 | * |
||
| 454 | * This method will disambiguate multiple reply forms in the same method |
||
| 455 | * |
||
| 456 | * @param HTTPRequest $request |
||
| 457 | * @throws HTTPResponse_Exception |
||
| 458 | */ |
||
| 459 | public function reply(HTTPRequest $request) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Post a comment form |
||
| 473 | * |
||
| 474 | * @return Form |
||
| 475 | */ |
||
| 476 | public function CommentsForm() |
||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * @return HTTPResponse|false |
||
| 484 | */ |
||
| 485 | public function redirectBack() |
||
| 519 | } |
||
| 520 |