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 |
||
| 7 | class CommentingController extends Controller |
||
|
|
|||
| 8 | { |
||
| 9 | |||
| 10 | private static $allowed_actions = array( |
||
| 11 | 'delete', |
||
| 12 | 'spam', |
||
| 13 | 'ham', |
||
| 14 | 'approve', |
||
| 15 | 'rss', |
||
| 16 | 'CommentsForm', |
||
| 17 | 'reply', |
||
| 18 | 'doPostComment', |
||
| 19 | 'doPreviewComment' |
||
| 20 | ); |
||
| 21 | |||
| 22 | private static $url_handlers = array( |
||
| 23 | 'reply/$ParentCommentID//$ID/$OtherID' => 'reply', |
||
| 24 | ); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Fields required for this form |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | * @config |
||
| 31 | */ |
||
| 32 | private static $required_fields = array( |
||
| 33 | 'Name', |
||
| 34 | 'Email', |
||
| 35 | 'Comment' |
||
| 36 | ); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Base class this commenting form is for |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $baseClass = ""; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The record this commenting form is for |
||
| 47 | * |
||
| 48 | * @var DataObject |
||
| 49 | */ |
||
| 50 | private $ownerRecord = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Parent controller record |
||
| 54 | * |
||
| 55 | * @var Controller |
||
| 56 | */ |
||
| 57 | private $ownerController = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Backup url to return to |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $fallbackReturnURL = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set the base class to use |
||
| 68 | * |
||
| 69 | * @param string $class |
||
| 70 | */ |
||
| 71 | public function setBaseClass($class) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the base class used |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function getBaseClass() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set the record this controller is working on |
||
| 88 | * |
||
| 89 | * @param DataObject $record |
||
| 90 | */ |
||
| 91 | public function setOwnerRecord($record) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the record |
||
| 98 | * |
||
| 99 | * @return DataObject |
||
| 100 | */ |
||
| 101 | public function getOwnerRecord() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set the parent controller |
||
| 108 | * |
||
| 109 | * @param Controller $controller |
||
| 110 | */ |
||
| 111 | public function setOwnerController($controller) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the parent controller |
||
| 118 | * |
||
| 119 | * @return Controller |
||
| 120 | */ |
||
| 121 | public function getOwnerController() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the commenting option for the current state |
||
| 128 | * |
||
| 129 | * @param string $key |
||
| 130 | * @return mixed Result if the setting is available, or null otherwise |
||
| 131 | */ |
||
| 132 | public function getOption($key) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Workaround for generating the link to this controller |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function Link($action = '', $id = '', $other = '') |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Outputs the RSS feed of comments |
||
| 160 | * |
||
| 161 | * @return HTMLText |
||
| 162 | */ |
||
| 163 | public function rss() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Return an RSSFeed of comments for a given set of comments or all |
||
| 170 | * comments on the website. |
||
| 171 | * |
||
| 172 | * To maintain backwards compatibility with 2.4 this supports mapping |
||
| 173 | * of PageComment/rss?pageid= as well as the new RSS format for comments |
||
| 174 | * of CommentingController/rss/{classname}/{id} |
||
| 175 | * |
||
| 176 | * @param SS_HTTPRequest |
||
| 177 | * |
||
| 178 | * @return RSSFeed |
||
| 179 | */ |
||
| 180 | public function getFeed(SS_HTTPRequest $request) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Deletes a given {@link Comment} via the URL. |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function delete() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Marks a given {@link Comment} as spam. Removes the comment from display |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function spam() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Marks a given {@link Comment} as ham (not spam). |
||
| 272 | */ |
||
| 273 | View Code Duplication | public function ham() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Marks a given {@link Comment} as approved. |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function approve() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Redirect back to referer if available, ensuring that only site URLs |
||
| 312 | * are allowed to avoid phishing. If it's an AJAX request render the |
||
| 313 | * comment in it's new state |
||
| 314 | */ |
||
| 315 | private function renderChangedCommentState($comment) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the comment referenced in the URL (by ID). Permission checking |
||
| 341 | * should be done in the callee. |
||
| 342 | * |
||
| 343 | * @return Comment|false |
||
| 344 | */ |
||
| 345 | public function getComment() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Create a reply form for a specified comment |
||
| 363 | * |
||
| 364 | * @param Comment $comment |
||
| 365 | */ |
||
| 366 | public function ReplyForm($comment) |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Request handler for reply form. |
||
| 388 | * This method will disambiguate multiple reply forms in the same method |
||
| 389 | * |
||
| 390 | * @param SS_HTTPRequest $request |
||
| 391 | */ |
||
| 392 | public function reply(SS_HTTPRequest $request) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Post a comment form |
||
| 406 | * |
||
| 407 | * @return Form |
||
| 408 | */ |
||
| 409 | public function CommentsForm() |
||
| 410 | { |
||
| 411 | $usePreview = $this->getOption('use_preview'); |
||
| 412 | |||
| 413 | $nameRequired = _t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name'); |
||
| 414 | $emailRequired = _t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'); |
||
| 415 | $emailInvalid = _t('CommentInterface.EMAILADDRESS_MESSAGE_EMAIL', 'Please enter a valid email address'); |
||
| 416 | $urlInvalid = _t('CommentInterface.COMMENT_MESSAGE_URL', 'Please enter a valid URL'); |
||
| 417 | $commentRequired = _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'); |
||
| 418 | |||
| 419 | $fields = new FieldList( |
||
| 420 | $dataFields = new CompositeField( |
||
| 421 | // Name |
||
| 422 | TextField::create("Name", _t('CommentInterface.YOURNAME', 'Your name')) |
||
| 423 | ->setCustomValidationMessage($nameRequired) |
||
| 424 | ->setAttribute('data-msg-required', $nameRequired), |
||
| 425 | |||
| 426 | |||
| 427 | EmailField::create( |
||
| 428 | "Email", |
||
| 429 | _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)") |
||
| 430 | ) |
||
| 431 | ->setCustomValidationMessage($emailRequired) |
||
| 432 | ->setAttribute('data-msg-required', $emailRequired) |
||
| 433 | ->setAttribute('data-msg-email', $emailInvalid) |
||
| 434 | ->setAttribute('data-rule-email', true), |
||
| 435 | |||
| 436 | // Url |
||
| 437 | TextField::create("URL", _t('CommentingController.WEBSITEURL', "Your website URL")) |
||
| 438 | ->setAttribute('data-msg-url', $urlInvalid) |
||
| 439 | ->setAttribute('data-rule-url', true), |
||
| 440 | |||
| 441 | // Comment |
||
| 442 | TextareaField::create("Comment", _t('CommentingController.COMMENTS', "Comments")) |
||
| 443 | ->setCustomValidationMessage($commentRequired) |
||
| 444 | ->setAttribute('data-msg-required', $commentRequired) |
||
| 445 | ), |
||
| 446 | HiddenField::create("ParentID"), |
||
| 447 | HiddenField::create("ReturnURL"), |
||
| 448 | HiddenField::create("ParentCommentID"), |
||
| 449 | HiddenField::create("BaseClass") |
||
| 450 | ); |
||
| 451 | |||
| 452 | // Preview formatted comment. Makes most sense when shortcodes or |
||
| 453 | // limited HTML is allowed. Populated by JS/Ajax. |
||
| 454 | if ($usePreview) { |
||
| 455 | $fields->insertAfter( |
||
| 456 | ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview')) |
||
| 457 | ->setAttribute('style', 'display: none'), // enable through JS |
||
| 458 | 'Comment' |
||
| 459 | ); |
||
| 460 | } |
||
| 461 | |||
| 462 | $dataFields->addExtraClass('data-fields'); |
||
| 463 | |||
| 464 | // save actions |
||
| 465 | $actions = new FieldList( |
||
| 466 | new FormAction("doPostComment", _t('CommentInterface.POST', 'Post')) |
||
| 467 | ); |
||
| 468 | if ($usePreview) { |
||
| 469 | $actions->push( |
||
| 470 | FormAction::create('doPreviewComment', _t('CommentInterface.PREVIEW', 'Preview')) |
||
| 471 | ->addExtraClass('action-minor') |
||
| 472 | ->setAttribute('style', 'display: none') // enable through JS |
||
| 473 | ); |
||
| 474 | } |
||
| 475 | |||
| 476 | // required fields for server side |
||
| 477 | $required = new RequiredFields($this->config()->required_fields); |
||
| 478 | |||
| 479 | // create the comment form |
||
| 480 | $form = new Form($this, 'CommentsForm', $fields, $actions, $required); |
||
| 481 | |||
| 482 | // if the record exists load the extra required data |
||
| 483 | if ($record = $this->getOwnerRecord()) { |
||
| 484 | |||
| 485 | // Load member data |
||
| 486 | $member = Member::currentUser(); |
||
| 487 | if (($record->CommentsRequireLogin || $record->PostingRequiredPermission) && $member) { |
||
| 488 | $fields = $form->Fields(); |
||
| 489 | |||
| 490 | $fields->removeByName('Name'); |
||
| 491 | $fields->removeByName('Email'); |
||
| 492 | $fields->insertBefore(new ReadonlyField("NameView", _t('CommentInterface.YOURNAME', 'Your name'), $member->getName()), 'URL'); |
||
| 493 | $fields->push(new HiddenField("Name", "", $member->getName())); |
||
| 494 | $fields->push(new HiddenField("Email", "", $member->Email)); |
||
| 495 | } |
||
| 496 | |||
| 497 | // we do not want to read a new URL when the form has already been submitted |
||
| 498 | // which in here, it hasn't been. |
||
| 499 | $form->loadDataFrom(array( |
||
| 500 | 'ParentID' => $record->ID, |
||
| 501 | 'ReturnURL' => $this->request->getURL(), |
||
| 502 | 'BaseClass' => $this->getBaseClass() |
||
| 503 | )); |
||
| 504 | } |
||
| 505 | |||
| 506 | // Set it so the user gets redirected back down to the form upon form fail |
||
| 507 | $form->setRedirectToFormOnValidationError(true); |
||
| 508 | |||
| 509 | // load any data from the cookies |
||
| 510 | if ($data = Cookie::get('CommentsForm_UserData')) { |
||
| 511 | $data = Convert::json2array($data); |
||
| 512 | |||
| 513 | $form->loadDataFrom(array( |
||
| 514 | "Name" => isset($data['Name']) ? $data['Name'] : '', |
||
| 515 | "URL" => isset($data['URL']) ? $data['URL'] : '', |
||
| 516 | "Email" => isset($data['Email']) ? $data['Email'] : '' |
||
| 517 | )); |
||
| 518 | // allow previous value to fill if comment not stored in cookie (i.e. validation error) |
||
| 519 | $prevComment = Cookie::get('CommentsForm_Comment'); |
||
| 520 | if ($prevComment && $prevComment != '') { |
||
| 521 | $form->loadDataFrom(array("Comment" => $prevComment)); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | if (!empty($member)) { |
||
| 526 | $form->loadDataFrom($member); |
||
| 527 | } |
||
| 528 | |||
| 529 | // hook to allow further extensions to alter the comments form |
||
| 530 | $this->extend('alterCommentForm', $form); |
||
| 531 | |||
| 532 | return $form; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Process which creates a {@link Comment} once a user submits a comment from this form. |
||
| 537 | * |
||
| 538 | * @param array $data |
||
| 539 | * @param Form $form |
||
| 540 | */ |
||
| 541 | public function doPostComment($data, $form) |
||
| 639 | |||
| 640 | public function doPreviewComment($data, $form) |
||
| 646 | |||
| 647 | public function redirectBack() |
||
| 681 | } |
||
| 682 |
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.