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 CMSPageHistoryController 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 CMSPageHistoryController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class CMSPageHistoryController extends CMSMain |
||
| 25 | { |
||
| 26 | |||
| 27 | private static $url_segment = 'pages/history'; |
||
|
|
|||
| 28 | |||
| 29 | private static $url_rule = '/$Action/$ID/$VersionID/$OtherVersionID'; |
||
| 30 | |||
| 31 | private static $url_priority = 42; |
||
| 32 | |||
| 33 | private static $menu_title = 'History'; |
||
| 34 | |||
| 35 | private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; |
||
| 36 | |||
| 37 | private static $allowed_actions = array( |
||
| 38 | 'VersionsForm', |
||
| 39 | 'CompareVersionsForm', |
||
| 40 | 'show', |
||
| 41 | 'compare' |
||
| 42 | ); |
||
| 43 | |||
| 44 | private static $url_handlers = array( |
||
| 45 | '$Action/$ID/$VersionID/$OtherVersionID' => 'handleAction' |
||
| 46 | ); |
||
| 47 | |||
| 48 | public function getResponseNegotiator() |
||
| 49 | { |
||
| 50 | $negotiator = parent::getResponseNegotiator(); |
||
| 51 | $controller = $this; |
||
| 52 | $negotiator->setCallback('CurrentForm', function () use (&$controller) { |
||
| 53 | $form = $controller->ShowVersionForm($controller->getRequest()->param('VersionID')); |
||
| 54 | if ($form) { |
||
| 55 | return $form->forTemplate(); |
||
| 56 | } else { |
||
| 57 | return $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); |
||
| 58 | } |
||
| 59 | }); |
||
| 60 | $negotiator->setCallback('default', function () use (&$controller) { |
||
| 61 | return $controller->renderWith($controller->getViewer('show')); |
||
| 62 | }); |
||
| 63 | return $negotiator; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param HTTPRequest $request |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function show($request) |
|
| 71 | { |
||
| 72 | $form = $this->ShowVersionForm($request->param('VersionID')); |
||
| 73 | |||
| 74 | $negotiator = $this->getResponseNegotiator(); |
||
| 75 | $controller = $this; |
||
| 76 | $negotiator->setCallback('CurrentForm', function () use (&$controller, &$form) { |
||
| 77 | return $form ? $form->forTemplate() : $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); |
||
| 78 | }); |
||
| 79 | $negotiator->setCallback('default', function () use (&$controller, &$form) { |
||
| 80 | return $controller->customise(array('EditForm' => $form))->renderWith($controller->getViewer('show')); |
||
| 81 | }); |
||
| 82 | |||
| 83 | return $negotiator->respond($request); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param HTTPRequest $request |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function compare($request) |
|
| 91 | { |
||
| 92 | $form = $this->CompareVersionsForm( |
||
| 93 | $request->param('VersionID'), |
||
| 94 | $request->param('OtherVersionID') |
||
| 95 | ); |
||
| 96 | |||
| 97 | $negotiator = $this->getResponseNegotiator(); |
||
| 98 | $controller = $this; |
||
| 99 | $negotiator->setCallback('CurrentForm', function () use (&$controller, &$form) { |
||
| 100 | return $form ? $form->forTemplate() : $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); |
||
| 101 | }); |
||
| 102 | $negotiator->setCallback('default', function () use (&$controller, &$form) { |
||
| 103 | return $controller->customise(array('EditForm' => $form))->renderWith($controller->getViewer('show')); |
||
| 104 | }); |
||
| 105 | |||
| 106 | return $negotiator->respond($request); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getSilverStripeNavigator() |
||
| 110 | { |
||
| 111 | $record = $this->getRecord($this->currentPageID(), $this->getRequest()->param('VersionID')); |
||
| 112 | if ($record) { |
||
| 113 | $navigator = new SilverStripeNavigator($record); |
||
| 114 | return $navigator->renderWith($this->getTemplatesWithSuffix('_SilverStripeNavigator')); |
||
| 115 | } else { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the read only version of the edit form. Detaches all {@link FormAction} |
||
| 122 | * instances attached since only action relates to revert. |
||
| 123 | * |
||
| 124 | * Permission checking is done at the {@link CMSMain::getEditForm()} level. |
||
| 125 | * |
||
| 126 | * @param int $id ID of the record to show |
||
| 127 | * @param array $fields optional |
||
| 128 | * @param int $versionID |
||
| 129 | * @param int $compareID Compare mode |
||
| 130 | * |
||
| 131 | * @return Form |
||
| 132 | */ |
||
| 133 | public function getEditForm($id = null, $fields = null, $versionID = null, $compareID = null) |
||
| 134 | { |
||
| 135 | if (!$id) { |
||
| 136 | $id = $this->currentPageID(); |
||
| 137 | } |
||
| 138 | |||
| 139 | $record = $this->getRecord($id, $versionID); |
||
| 140 | $versionID = ($record) ? $record->Version : $versionID; |
||
| 141 | |||
| 142 | $form = parent::getEditForm($record, ($record) ? $record->getCMSFields() : null); |
||
| 143 | // Respect permission failures from parent implementation |
||
| 144 | if (!($form instanceof Form)) { |
||
| 145 | return $form; |
||
| 146 | } |
||
| 147 | |||
| 148 | // TODO: move to the SilverStripeNavigator structure so the new preview can pick it up. |
||
| 149 | //$nav = new SilverStripeNavigatorItem_ArchiveLink($record); |
||
| 150 | |||
| 151 | $form->setActions(new FieldList( |
||
| 152 | $revert = FormAction::create('doRollback', _t('CMSPageHistoryController.REVERTTOTHISVERSION', 'Revert to this version'))->setUseButtonTag(true) |
||
| 153 | )); |
||
| 154 | |||
| 155 | $fields = $form->Fields(); |
||
| 156 | $fields->removeByName("Status"); |
||
| 157 | $fields->push(new HiddenField("ID")); |
||
| 158 | $fields->push(new HiddenField("Version")); |
||
| 159 | |||
| 160 | $fields = $fields->makeReadonly(); |
||
| 161 | |||
| 162 | if ($compareID) { |
||
| 163 | $link = Controller::join_links( |
||
| 164 | $this->Link('show'), |
||
| 165 | $id |
||
| 166 | ); |
||
| 167 | |||
| 168 | $view = _t('CMSPageHistoryController.VIEW', "view"); |
||
| 169 | |||
| 170 | $message = _t( |
||
| 171 | 'CMSPageHistoryController.COMPARINGVERSION', |
||
| 172 | "Comparing versions {version1} and {version2}.", |
||
| 173 | array( |
||
| 174 | 'version1' => sprintf('%s (<a href="%s">%s</a>)', $versionID, Controller::join_links($link, $versionID), $view), |
||
| 175 | 'version2' => sprintf('%s (<a href="%s">%s</a>)', $compareID, Controller::join_links($link, $compareID), $view) |
||
| 176 | ) |
||
| 177 | ); |
||
| 178 | |||
| 179 | $revert->setReadonly(true); |
||
| 180 | } else { |
||
| 181 | if ($record->isLatestVersion()) { |
||
| 182 | $message = _t('CMSPageHistoryController.VIEWINGLATEST', 'Currently viewing the latest version.'); |
||
| 183 | } else { |
||
| 184 | $message = _t( |
||
| 185 | 'CMSPageHistoryController.VIEWINGVERSION', |
||
| 186 | "Currently viewing version {version}.", |
||
| 187 | array('version' => $versionID) |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | $fields->fieldByName('Root.Main')->unshift( |
||
| 193 | new LiteralField('CurrentlyViewingMessage', ArrayData::create(array( |
||
| 194 | 'Content' => DBField::create_field('HTMLFragment', $message), |
||
| 195 | 'Classes' => 'notice' |
||
| 196 | ))->renderWith($this->getTemplatesWithSuffix('_notice'))) |
||
| 197 | ); |
||
| 198 | |||
| 199 | $form->setFields($fields->makeReadonly()); |
||
| 200 | $form->loadDataFrom(array( |
||
| 201 | "ID" => $id, |
||
| 202 | "Version" => $versionID, |
||
| 203 | )); |
||
| 204 | |||
| 205 | if (($record && $record->isLatestVersion())) { |
||
| 206 | $revert->setReadonly(true); |
||
| 207 | } |
||
| 208 | |||
| 209 | $form->removeExtraClass('cms-content'); |
||
| 210 | |||
| 211 | return $form; |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Version select form. Main interface between selecting versions to view |
||
| 217 | * and comparing multiple versions. |
||
| 218 | * |
||
| 219 | * Because we can reload the page directly to a compare view (history/compare/1/2/3) |
||
| 220 | * this form has to adapt to those parameters as well. |
||
| 221 | * |
||
| 222 | * @return Form |
||
| 223 | */ |
||
| 224 | public function VersionsForm() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Process the {@link VersionsForm} compare function between two pages. |
||
| 316 | * |
||
| 317 | * @param array $data |
||
| 318 | * @param Form $form |
||
| 319 | * @return HTTPResponse|DBHTMLText |
||
| 320 | */ |
||
| 321 | public function doCompare($data, $form) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Process the {@link VersionsForm} show version function. Only requires |
||
| 353 | * one page to be selected. |
||
| 354 | * |
||
| 355 | * @param array |
||
| 356 | * @param Form |
||
| 357 | * |
||
| 358 | * @return DBHTMLText|HTTPResponse |
||
| 359 | */ |
||
| 360 | public function doShowVersion($data, $form) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param int|null $versionID |
||
| 391 | * @return Form |
||
| 392 | */ |
||
| 393 | public function ShowVersionForm($versionID = null) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param int $versionID |
||
| 407 | * @param int $otherVersionID |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | public function CompareVersionsForm($versionID, $otherVersionID) |
||
| 468 | |||
| 469 | public function Breadcrumbs($unlinked = false) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Replace all data fields with HTML readonly fields to display diff |
||
| 478 | * |
||
| 479 | * @param FieldList $fields |
||
| 480 | * @return FieldList |
||
| 481 | */ |
||
| 482 | public function transformReadonly(FieldList $fields) |
||
| 493 | } |
||
| 494 |