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  | 
            ||
| 21 | class CMSPageHistoryController extends CMSMain { | 
            ||
| 22 | |||
| 23 | private static $url_segment = 'pages/history';  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 24 | |||
| 25 | private static $url_rule = '/$Action/$ID/$VersionID/$OtherVersionID';  | 
            ||
| 26 | |||
| 27 | private static $url_priority = 42;  | 
            ||
| 28 | |||
| 29 | private static $menu_title = 'History';  | 
            ||
| 30 | |||
| 31 | private static $required_permission_codes = 'CMS_ACCESS_CMSMain';  | 
            ||
| 32 | |||
| 33 | private static $allowed_actions = array(  | 
            ||
| 34 | 'VersionsForm',  | 
            ||
| 35 | 'CompareVersionsForm',  | 
            ||
| 36 | 'show',  | 
            ||
| 37 | 'compare'  | 
            ||
| 38 | );  | 
            ||
| 39 | |||
| 40 | private static $url_handlers = array(  | 
            ||
| 41 | '$Action/$ID/$VersionID/$OtherVersionID' => 'handleAction'  | 
            ||
| 42 | );  | 
            ||
| 43 | |||
| 44 | 	public function getResponseNegotiator() { | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * @param HTTPRequest $request  | 
            ||
| 60 | * @return array  | 
            ||
| 61 | */  | 
            ||
| 62 | View Code Duplication | 	public function show($request) { | 
            |
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * @param HTTPRequest $request  | 
            ||
| 79 | * @return array  | 
            ||
| 80 | */  | 
            ||
| 81 | View Code Duplication | 	public function compare($request) { | 
            |
| 98 | |||
| 99 | 	public function getSilverStripeNavigator() { | 
            ||
| 108 | |||
| 109 | /**  | 
            ||
| 110 | 	 * Returns the read only version of the edit form. Detaches all {@link FormAction} | 
            ||
| 111 | * instances attached since only action relates to revert.  | 
            ||
| 112 | *  | 
            ||
| 113 | 	 * Permission checking is done at the {@link CMSMain::getEditForm()} level. | 
            ||
| 114 | *  | 
            ||
| 115 | * @param int $id ID of the record to show  | 
            ||
| 116 | * @param array $fields optional  | 
            ||
| 117 | * @param int $versionID  | 
            ||
| 118 | * @param int $compareID Compare mode  | 
            ||
| 119 | *  | 
            ||
| 120 | * @return Form  | 
            ||
| 121 | */  | 
            ||
| 122 | 	public function getEditForm($id = null, $fields = null, $versionID = null, $compareID = null) { | 
            ||
| 198 | |||
| 199 | |||
| 200 | /**  | 
            ||
| 201 | * Version select form. Main interface between selecting versions to view  | 
            ||
| 202 | * and comparing multiple versions.  | 
            ||
| 203 | *  | 
            ||
| 204 | * Because we can reload the page directly to a compare view (history/compare/1/2/3)  | 
            ||
| 205 | * this form has to adapt to those parameters as well.  | 
            ||
| 206 | *  | 
            ||
| 207 | * @return Form  | 
            ||
| 208 | */  | 
            ||
| 209 | 	public function VersionsForm() { | 
            ||
| 210 | $id = $this->currentPageID();  | 
            ||
| 211 | $page = $this->getRecord($id);  | 
            ||
| 212 | $versionsHtml = '';  | 
            ||
| 213 | |||
| 214 | 		$action = $this->getRequest()->param('Action'); | 
            ||
| 215 | 		$versionID = $this->getRequest()->param('VersionID'); | 
            ||
| 216 | 		$otherVersionID = $this->getRequest()->param('OtherVersionID'); | 
            ||
| 217 | |||
| 218 | $showUnpublishedChecked = 0;  | 
            ||
| 219 | $compareModeChecked = ($action == "compare");  | 
            ||
| 220 | |||
| 221 | 		if($page) { | 
            ||
| 222 | $versions = $page->allVersions();  | 
            ||
| 223 | $versionID = (!$versionID) ? $page->Version : $versionID;  | 
            ||
| 224 | |||
| 225 | 			if($versions) { | 
            ||
| 226 | 				foreach($versions as $k => $version) { | 
            ||
| 227 | $active = false;  | 
            ||
| 228 | |||
| 229 | 					if($version->Version == $versionID || $version->Version == $otherVersionID) { | 
            ||
| 230 | $active = true;  | 
            ||
| 231 | |||
| 232 | if(!$version->WasPublished) $showUnpublishedChecked = 1;  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | $version->Active = ($active);  | 
            ||
| 236 | }  | 
            ||
| 237 | }  | 
            ||
| 238 | |||
| 239 | $vd = new ViewableData();  | 
            ||
| 240 | |||
| 241 | $versionsHtml = $vd->customise(array(  | 
            ||
| 242 | 'Versions' => $versions  | 
            ||
| 243 | 			))->renderWith($this->getTemplatesWithSuffix('_versions')); | 
            ||
| 244 | }  | 
            ||
| 245 | |||
| 246 | $fields = new FieldList(  | 
            ||
| 247 | new CheckboxField(  | 
            ||
| 248 | 'ShowUnpublished',  | 
            ||
| 249 | 				_t('CMSPageHistoryController.SHOWUNPUBLISHED','Show unpublished versions'), | 
            ||
| 250 | $showUnpublishedChecked  | 
            ||
| 251 | ),  | 
            ||
| 252 | new CheckboxField(  | 
            ||
| 253 | 'CompareMode',  | 
            ||
| 254 | 				_t('CMSPageHistoryController.COMPAREMODE', 'Compare mode (select two)'), | 
            ||
| 255 | $compareModeChecked  | 
            ||
| 256 | ),  | 
            ||
| 257 | 			new LiteralField('VersionsHtml', $versionsHtml), | 
            ||
| 258 | 			$hiddenID = new HiddenField('ID', false, "") | 
            ||
| 259 | );  | 
            ||
| 260 | |||
| 261 | $actions = new FieldList(  | 
            ||
| 262 | new FormAction(  | 
            ||
| 263 | 				'doCompare', _t('CMSPageHistoryController.COMPAREVERSIONS','Compare Versions') | 
            ||
| 264 | ),  | 
            ||
| 265 | new FormAction(  | 
            ||
| 266 | 				'doShowVersion', _t('CMSPageHistoryController.SHOWVERSION','Show Version') | 
            ||
| 267 | )  | 
            ||
| 268 | );  | 
            ||
| 269 | |||
| 270 | // Use <button> to allow full jQuery UI styling  | 
            ||
| 271 | 		foreach($actions->dataFields() as $action) { | 
            ||
| 272 | /** @var FormAction $action */  | 
            ||
| 273 | $action->setUseButtonTag(true);  | 
            ||
| 274 | }  | 
            ||
| 275 | |||
| 276 | $form = Form::create(  | 
            ||
| 277 | $this,  | 
            ||
| 278 | 'VersionsForm',  | 
            ||
| 279 | $fields,  | 
            ||
| 280 | $actions  | 
            ||
| 281 | 		)->setHTMLID('Form_VersionsForm'); | 
            ||
| 282 | $form->loadDataFrom($this->getRequest()->requestVars());  | 
            ||
| 283 | $hiddenID->setValue($id);  | 
            ||
| 284 | $form->unsetValidator();  | 
            ||
| 285 | |||
| 286 | $form  | 
            ||
| 287 | 			->addExtraClass('cms-versions-form') // placeholder, necessary for $.metadata() to work | 
            ||
| 288 | 			->setAttribute('data-link-tmpl-compare', Controller::join_links($this->Link('compare'), '%s', '%s', '%s')) | 
            ||
| 289 | 			->setAttribute('data-link-tmpl-show', Controller::join_links($this->Link('show'), '%s', '%s')); | 
            ||
| 290 | |||
| 291 | return $form;  | 
            ||
| 292 | }  | 
            ||
| 293 | |||
| 294 | /**  | 
            ||
| 295 | 	 * Process the {@link VersionsForm} compare function between two pages. | 
            ||
| 296 | *  | 
            ||
| 297 | * @param array $data  | 
            ||
| 298 | * @param Form $form  | 
            ||
| 299 | * @return HTTPResponse|DBHTMLText  | 
            ||
| 300 | */  | 
            ||
| 301 | 	public function doCompare($data, $form) { | 
            ||
| 302 | $versions = $data['Versions'];  | 
            ||
| 303 | 		if(count($versions) < 2) { | 
            ||
| 304 | return null;  | 
            ||
| 305 | }  | 
            ||
| 306 | |||
| 307 | $version1 = array_shift($versions);  | 
            ||
| 308 | $version2 = array_shift($versions);  | 
            ||
| 309 | |||
| 310 | $form = $this->CompareVersionsForm($version1, $version2);  | 
            ||
| 311 | |||
| 312 | // javascript solution, render into template  | 
            ||
| 313 | View Code Duplication | 		if($this->getRequest()->isAjax()) { | 
            |
| 314 | return $this->customise(array(  | 
            ||
| 315 | "EditForm" => $form  | 
            ||
| 316 | ))->renderWith(array(  | 
            ||
| 317 | static::class . '_EditForm',  | 
            ||
| 318 | 'LeftAndMain_Content'  | 
            ||
| 319 | ));  | 
            ||
| 320 | }  | 
            ||
| 321 | |||
| 322 | // non javascript, redirect the user to the page  | 
            ||
| 323 | return $this->redirect(Controller::join_links(  | 
            ||
| 324 | 			$this->Link('compare'), | 
            ||
| 325 | $version1,  | 
            ||
| 326 | $version2  | 
            ||
| 327 | ));  | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | /**  | 
            ||
| 331 | 	 * Process the {@link VersionsForm} show version function. Only requires | 
            ||
| 332 | * one page to be selected.  | 
            ||
| 333 | *  | 
            ||
| 334 | * @param array  | 
            ||
| 335 | * @param Form  | 
            ||
| 336 | *  | 
            ||
| 337 | * @return DBHTMLText|HTTPResponse  | 
            ||
| 338 | */  | 
            ||
| 339 | 	public function doShowVersion($data, $form) { | 
            ||
| 340 | $versionID = null;  | 
            ||
| 341 | |||
| 342 | 		if(isset($data['Versions']) && is_array($data['Versions'])) { | 
            ||
| 343 | $versionID = array_shift($data['Versions']);  | 
            ||
| 344 | }  | 
            ||
| 345 | |||
| 346 | 		if(!$versionID) { | 
            ||
| 347 | return null;  | 
            ||
| 348 | }  | 
            ||
| 349 | |||
| 350 | $request = $this->getRequest();  | 
            ||
| 351 | View Code Duplication | 		if($request->isAjax()) { | 
            |
| 352 | return $this->customise(array(  | 
            ||
| 353 | "EditForm" => $this->ShowVersionForm($versionID)  | 
            ||
| 354 | ))->renderWith(array(  | 
            ||
| 355 | static::class . '_EditForm',  | 
            ||
| 356 | 'LeftAndMain_Content'  | 
            ||
| 357 | ));  | 
            ||
| 358 | }  | 
            ||
| 359 | |||
| 360 | // non javascript, redirect the user to the page  | 
            ||
| 361 | return $this->redirect(Controller::join_links(  | 
            ||
| 362 | 			$this->Link('version'), | 
            ||
| 363 | $versionID  | 
            ||
| 364 | ));  | 
            ||
| 365 | }  | 
            ||
| 366 | |||
| 367 | /**  | 
            ||
| 368 | * @param int|null $versionID  | 
            ||
| 369 | * @return Form  | 
            ||
| 370 | */  | 
            ||
| 371 | 	public function ShowVersionForm($versionID = null) { | 
            ||
| 379 | |||
| 380 | /**  | 
            ||
| 381 | * @param int $versionID  | 
            ||
| 382 | * @param int $otherVersionID  | 
            ||
| 383 | * @return mixed  | 
            ||
| 384 | */  | 
            ||
| 385 | 	public function CompareVersionsForm($versionID, $otherVersionID) { | 
            ||
| 386 | 		if($versionID > $otherVersionID) { | 
            ||
| 387 | $toVersion = $versionID;  | 
            ||
| 388 | $fromVersion = $otherVersionID;  | 
            ||
| 389 | 		} else { | 
            ||
| 390 | $toVersion = $otherVersionID;  | 
            ||
| 391 | $fromVersion = $versionID;  | 
            ||
| 392 | }  | 
            ||
| 393 | |||
| 394 | 		if(!$toVersion || !$fromVersion) { | 
            ||
| 395 | return null;  | 
            ||
| 396 | }  | 
            ||
| 397 | |||
| 398 | $id = $this->currentPageID();  | 
            ||
| 399 | /** @var SiteTree $page */  | 
            ||
| 400 | $page = SiteTree::get()->byID($id);  | 
            ||
| 401 | |||
| 402 | $record = null;  | 
            ||
| 403 |  		if($page && $page->exists()) { | 
            ||
| 404 | 			if(!$page->canView()) { | 
            ||
| 405 | return Security::permissionFailure($this);  | 
            ||
| 406 | }  | 
            ||
| 407 | |||
| 408 | $record = $page->compareVersions($fromVersion, $toVersion);  | 
            ||
| 409 | }  | 
            ||
| 410 | |||
| 411 | 		$fromVersionRecord = Versioned::get_version('SilverStripe\\CMS\\Model\\SiteTree', $id, $fromVersion); | 
            ||
| 412 | 		$toVersionRecord = Versioned::get_version('SilverStripe\\CMS\\Model\\SiteTree', $id, $toVersion); | 
            ||
| 413 | |||
| 414 | 		if(!$fromVersionRecord) { | 
            ||
| 415 | 			user_error("Can't find version $fromVersion of page $id", E_USER_ERROR); | 
            ||
| 416 | }  | 
            ||
| 417 | |||
| 418 | 		if(!$toVersionRecord) { | 
            ||
| 419 | 			user_error("Can't find version $toVersion of page $id", E_USER_ERROR); | 
            ||
| 420 | }  | 
            ||
| 421 | |||
| 422 | 		if(!$record) { | 
            ||
| 423 | return null;  | 
            ||
| 424 | }  | 
            ||
| 425 | $form = $this->getEditForm($id, null, null, true);  | 
            ||
| 426 | $form->setActions(new FieldList());  | 
            ||
| 427 | 		$form->addExtraClass('compare'); | 
            ||
| 428 | |||
| 429 | // Comparison views shouldn't be editable.  | 
            ||
| 430 | // Its important to convert fields *before* loading data,  | 
            ||
| 431 | // as the comparison output is HTML and not valid values for the various field types  | 
            ||
| 432 | $readonlyFields = $form->Fields()->makeReadonly();  | 
            ||
| 433 | $form->setFields($readonlyFields);  | 
            ||
| 434 | |||
| 435 | $form->loadDataFrom($record);  | 
            ||
| 436 | $form->loadDataFrom(array(  | 
            ||
| 437 | "ID" => $id,  | 
            ||
| 438 | "Version" => $fromVersion,  | 
            ||
| 439 | ));  | 
            ||
| 440 | |||
| 441 | 		foreach($form->Fields()->dataFields() as $field) { | 
            ||
| 442 | $field->dontEscape = true;  | 
            ||
| 443 | }  | 
            ||
| 444 | |||
| 445 | return $form;  | 
            ||
| 446 | }  | 
            ||
| 447 | |||
| 448 | 	public function Breadcrumbs($unlinked = false) { | 
            ||
| 453 | |||
| 454 | }  | 
            ||
| 455 |