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 DeployDispatcher 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 DeployDispatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 7 | class DeployDispatcher extends Dispatcher { | 
            ||
| 8 | |||
| 9 | const ACTION_DEPLOY = 'deploys';  | 
            ||
| 10 | |||
| 11 | /**  | 
            ||
| 12 | * @var array  | 
            ||
| 13 | */  | 
            ||
| 14 | private static $allowed_actions = [  | 
            ||
| 15 | 'history',  | 
            ||
| 16 | 'upcoming',  | 
            ||
| 17 | 'currentbuild',  | 
            ||
| 18 | 'show',  | 
            ||
| 19 | 'delete',  | 
            ||
| 20 | 'log',  | 
            ||
| 21 | 'redeploy',  | 
            ||
| 22 | 'summary',  | 
            ||
| 23 | 'createdeployment',  | 
            ||
| 24 | 'start'  | 
            ||
| 25 | ];  | 
            ||
| 26 | |||
| 27 | private static $dependencies = [  | 
            ||
| 28 | 'formatter' => '%$DeploynautAPIFormatter'  | 
            ||
| 29 | ];  | 
            ||
| 30 | |||
| 31 | /**  | 
            ||
| 32 | * @var \DNProject  | 
            ||
| 33 | */  | 
            ||
| 34 | protected $project = null;  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * @var \DNEnvironment  | 
            ||
| 38 | */  | 
            ||
| 39 | protected $environment = null;  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * @var array  | 
            ||
| 43 | */  | 
            ||
| 44 | private static $action_types = [  | 
            ||
| 45 | self::ACTION_DEPLOY  | 
            ||
| 46 | ];  | 
            ||
| 47 | |||
| 48 | View Code Duplication | 	public function init() { | 
            |
| 49 | parent::init();  | 
            ||
| 50 | |||
| 51 | $this->project = $this->getCurrentProject();  | 
            ||
| 52 | |||
| 53 | 		if (!$this->project) { | 
            ||
| 54 | return $this->project404Response();  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 | // Performs canView permission check by limiting visible projects  | 
            ||
| 58 | $this->environment = $this->getCurrentEnvironment($this->project);  | 
            ||
| 59 | 		if (!$this->environment) { | 
            ||
| 60 | return $this->environment404Response();  | 
            ||
| 61 | }  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * @param \SS_HTTPRequest $request  | 
            ||
| 66 | * @return \HTMLText|\SS_HTTPResponse  | 
            ||
| 67 | */  | 
            ||
| 68 | 	public function index(\SS_HTTPRequest $request) { | 
            ||
| 69 | return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302);  | 
            ||
| 70 | }  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * @param \SS_HTTPRequest $request  | 
            ||
| 74 | * @return \SS_HTTPResponse  | 
            ||
| 75 | */  | 
            ||
| 76 | 	public function history(\SS_HTTPRequest $request) { | 
            ||
| 77 | $data = [];  | 
            ||
| 78 | |||
| 79 | 		$list = $this->environment->DeployHistory('DeployStarted'); | 
            ||
| 80 | |||
| 81 | 		$fromTimestamp = $request->requestVar('from'); | 
            ||
| 82 | 		if ($fromTimestamp) { | 
            ||
| 83 | $from = SS_Datetime::create();  | 
            ||
| 84 | $from->setValue($fromTimestamp);  | 
            ||
| 85 | 			$list = $list->filter('LastEdited:GreaterThan', $from->Format('Y-m-d H:i:s')); | 
            ||
| 86 | }  | 
            ||
| 87 | |||
| 88 | 		foreach ($list as $deployment) { | 
            ||
| 89 | $data[] = $this->formatter->getDeploymentData($deployment);  | 
            ||
| 90 | }  | 
            ||
| 91 | |||
| 92 | return $this->getAPIResponse([  | 
            ||
| 93 | 'list' => $data,  | 
            ||
| 94 | ], 200);  | 
            ||
| 95 | }  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * @param \SS_HTTPRequest $request  | 
            ||
| 99 | * @return \SS_HTTPResponse  | 
            ||
| 100 | */  | 
            ||
| 101 | 	public function upcoming(\SS_HTTPRequest $request) { | 
            ||
| 102 | $data = [];  | 
            ||
| 103 | $list = $this->environment->UpcomingDeployments();  | 
            ||
| 104 | 		foreach ($list as $deployment) { | 
            ||
| 105 | $data[] = $this->formatter->getDeploymentData($deployment);  | 
            ||
| 106 | }  | 
            ||
| 107 | return $this->getAPIResponse([  | 
            ||
| 108 | 'list' => $data,  | 
            ||
| 109 | ], 200);  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * @param \SS_HTTPRequest $request  | 
            ||
| 114 | * @return \SS_HTTPResponse  | 
            ||
| 115 | */  | 
            ||
| 116 | 	public function currentbuild(\SS_HTTPRequest $request) { | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * @param \SS_HTTPRequest $request  | 
            ||
| 126 | * @return \SS_HTTPResponse  | 
            ||
| 127 | */  | 
            ||
| 128 | 	public function show(\SS_HTTPRequest $request) { | 
            ||
| 136 | |||
| 137 | /**  | 
            ||
| 138 | * @param \SS_HTTPRequest $request  | 
            ||
| 139 | * @return \SS_HTTPResponse  | 
            ||
| 140 | */  | 
            ||
| 141 | 	public function delete(\SS_HTTPRequest $request) { | 
            ||
| 162 | |||
| 163 | /**  | 
            ||
| 164 | * @param \SS_HTTPRequest $request  | 
            ||
| 165 | * @return \SS_HTTPResponse  | 
            ||
| 166 | */  | 
            ||
| 167 | 	public function log(\SS_HTTPRequest $request) { | 
            ||
| 185 | |||
| 186 | /**  | 
            ||
| 187 | * @param \SS_HTTPRequest $request  | 
            ||
| 188 | * @return \SS_HTTPResponse  | 
            ||
| 189 | */  | 
            ||
| 190 | 	public function redeploy(\SS_HTTPRequest $request) { | 
            ||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * Return a summary of the deployment changes without creating the deployment.  | 
            ||
| 212 | *  | 
            ||
| 213 | * @param \SS_HTTPRequest $request  | 
            ||
| 214 | * @return \SS_HTTPResponse  | 
            ||
| 215 | */  | 
            ||
| 216 | 	public function summary(\SS_HTTPRequest $request) { | 
            ||
| 217 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 218 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 219 | }  | 
            ||
| 220 | $this->checkSecurityToken();  | 
            ||
| 221 | |||
| 222 | 		$options = ['sha' => $request->postVar('ref')]; | 
            ||
| 223 | View Code Duplication | 		if ($request->requestVar('options')) { | 
            |
| 224 | 			foreach (explode(',', $request->postVar('options')) as $option) { | 
            ||
| 225 | $options[$option] = true;  | 
            ||
| 226 | }  | 
            ||
| 227 | }  | 
            ||
| 228 | |||
| 229 | $strategy = $this->createStrategy($options);  | 
            ||
| 230 | return $this->getAPIResponse($strategy->toArray(), 201);  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | 	 * Create deployment. Can't use {@link create()} as it's taken by Object. | 
            ||
| 235 | *  | 
            ||
| 236 | * @param \SS_HTTPRequest $request  | 
            ||
| 237 | * @return \SS_HTTPResponse  | 
            ||
| 238 | */  | 
            ||
| 239 | 	public function createdeployment(\SS_HTTPRequest $request) { | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * @param \SS_HTTPRequest $request  | 
            ||
| 288 | * @return \SS_HTTPResponse  | 
            ||
| 289 | */  | 
            ||
| 290 | 	public function start(\SS_HTTPRequest $request) { | 
            ||
| 339 | |||
| 340 | /**  | 
            ||
| 341 | * @param string $action  | 
            ||
| 342 | * @return string  | 
            ||
| 343 | */  | 
            ||
| 344 | 	public function Link($action = '') { | 
            ||
| 347 | |||
| 348 | /**  | 
            ||
| 349 | * @param string $name  | 
            ||
| 350 | * @return array  | 
            ||
| 351 | */  | 
            ||
| 352 | 	public function getModel($name = '') { | 
            ||
| 355 | |||
| 356 | /**  | 
            ||
| 357 | * @var array  | 
            ||
| 358 | * @return \DeploymentStrategy  | 
            ||
| 359 | */  | 
            ||
| 360 | 	protected function createStrategy($options) { | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * @param ArrayData $interface  | 
            ||
| 393 | * @param $changes  | 
            ||
| 394 | * @return bool  | 
            ||
| 395 | */  | 
            ||
| 396 | 	protected function canCompareCodeVersions(\ArrayData $interface, $changes) { | 
            ||
| 415 | |||
| 416 | /**  | 
            ||
| 417 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return  | 
            ||
| 418 | * an APIResponse with the error, otherwise null.  | 
            ||
| 419 | *  | 
            ||
| 420 | * @param \DNDeployment $deployment  | 
            ||
| 421 | *  | 
            ||
| 422 | * @return null|SS_HTTPResponse  | 
            ||
| 423 | */  | 
            ||
| 424 | View Code Duplication | 	protected function validateDeployment($deployment) { | 
            |
| 436 | |||
| 437 | }  | 
            ||
| 438 | 
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.