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:
| 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 | 'log',  | 
            ||
| 20 | 'create',  | 
            ||
| 21 | 'createdeployment'  | 
            ||
| 22 | ];  | 
            ||
| 23 | |||
| 24 | private static $dependencies = [  | 
            ||
| 25 | 'formatter' => '%$DeploynautAPIFormatter'  | 
            ||
| 26 | ];  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * @var \DNProject  | 
            ||
| 30 | */  | 
            ||
| 31 | protected $project = null;  | 
            ||
| 32 | |||
| 33 | /**  | 
            ||
| 34 | * @var \DNEnvironment  | 
            ||
| 35 | */  | 
            ||
| 36 | protected $environment = null;  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * @var array  | 
            ||
| 40 | */  | 
            ||
| 41 | private static $action_types = [  | 
            ||
| 42 | self::ACTION_DEPLOY  | 
            ||
| 43 | ];  | 
            ||
| 44 | |||
| 45 | View Code Duplication | 	public function init() { | 
            |
| 46 | parent::init();  | 
            ||
| 47 | |||
| 48 | $this->project = $this->getCurrentProject();  | 
            ||
| 49 | |||
| 50 | 		if (!$this->project) { | 
            ||
| 51 | return $this->project404Response();  | 
            ||
| 52 | }  | 
            ||
| 53 | |||
| 54 | // Performs canView permission check by limiting visible projects  | 
            ||
| 55 | $this->environment = $this->getCurrentEnvironment($this->project);  | 
            ||
| 56 | 		if (!$this->environment) { | 
            ||
| 57 | return $this->environment404Response();  | 
            ||
| 58 | }  | 
            ||
| 59 | }  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * @param \SS_HTTPRequest $request  | 
            ||
| 63 | * @return \HTMLText|\SS_HTTPResponse  | 
            ||
| 64 | */  | 
            ||
| 65 | 	public function index(\SS_HTTPRequest $request) { | 
            ||
| 66 | return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302);  | 
            ||
| 67 | }  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * @param \SS_HTTPRequest $request  | 
            ||
| 71 | * @return \SS_HTTPResponse  | 
            ||
| 72 | */  | 
            ||
| 73 | View Code Duplication | 	public function history(SS_HTTPRequest $request) { | 
            |
| 74 | $data = [];  | 
            ||
| 75 | |||
| 76 | 		$list = $this->environment->DeployHistory('DeployStarted'); | 
            ||
| 77 | |||
| 78 | 		foreach ($list as $deployment) { | 
            ||
| 79 | $data[] = $this->formatter->getDeploymentData($deployment);  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | return $this->getAPIResponse([  | 
            ||
| 83 | 'list' => $data,  | 
            ||
| 84 | ], 200);  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * @param \SS_HTTPRequest $request  | 
            ||
| 89 | * @return \SS_HTTPResponse  | 
            ||
| 90 | */  | 
            ||
| 91 | View Code Duplication | 	public function upcoming(SS_HTTPRequest $request) { | 
            |
| 92 | $data = [];  | 
            ||
| 93 | $list = $this->environment->UpcomingDeployments();  | 
            ||
| 94 | 		foreach ($list as $deployment) { | 
            ||
| 95 | $data[] = $this->formatter->getDeploymentData($deployment);  | 
            ||
| 96 | }  | 
            ||
| 97 | return $this->getAPIResponse([  | 
            ||
| 98 | 'list' => $data,  | 
            ||
| 99 | ], 200);  | 
            ||
| 100 | }  | 
            ||
| 101 | |||
| 102 | /**  | 
            ||
| 103 | * @param \SS_HTTPRequest $request  | 
            ||
| 104 | * @return \SS_HTTPResponse  | 
            ||
| 105 | */  | 
            ||
| 106 | 	public function currentbuild(SS_HTTPRequest $request) { | 
            ||
| 107 | $currentBuild = $this->environment->CurrentBuild();  | 
            ||
| 108 | 		if (!$currentBuild) { | 
            ||
| 109 | return $this->getAPIResponse(['deployment' => []], 200);  | 
            ||
| 110 | }  | 
            ||
| 111 | return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($currentBuild)], 200);  | 
            ||
| 112 | }  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * @param \SS_HTTPRequest $request  | 
            ||
| 116 | * @return \SS_HTTPResponse  | 
            ||
| 117 | */  | 
            ||
| 118 | 	public function show(SS_HTTPRequest $request) { | 
            ||
| 119 | 		$deployment = DNDeployment::get()->byId($request->param('ID')); | 
            ||
| 120 | $errorResponse = $this->validateDeployment($deployment);  | 
            ||
| 121 | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            ||
| 122 | return $errorResponse;  | 
            ||
| 123 | }  | 
            ||
| 124 | return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($deployment)], 200);  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * @param \SS_HTTPRequest $request  | 
            ||
| 129 | * @return \SS_HTTPResponse  | 
            ||
| 130 | */  | 
            ||
| 131 | 	public function log(SS_HTTPRequest $request) { | 
            ||
| 132 | 		$deployment = DNDeployment::get()->byId($request->param('ID')); | 
            ||
| 133 | $errorResponse = $this->validateDeployment($deployment);  | 
            ||
| 134 | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            ||
| 135 | return $errorResponse;  | 
            ||
| 136 | }  | 
            ||
| 137 | $log = $deployment->log();  | 
            ||
| 138 | $content = $log->exists() ? $log->content() : 'Waiting for action to start';  | 
            ||
| 139 | $lines = explode(PHP_EOL, $content);  | 
            ||
| 140 | |||
| 141 | return $this->getAPIResponse([  | 
            ||
| 142 | 'message' => $lines,  | 
            ||
| 143 | 'status' => $deployment->Status,  | 
            ||
| 144 | 'deployment' => $this->formatter->getDeploymentData($deployment),  | 
            ||
| 145 | ], 200);  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | 	 * Create deployment. Can't use {@link create()} as it's taken by Object. | 
            ||
| 150 | *  | 
            ||
| 151 | * @param \SS_HTTPRequest $request  | 
            ||
| 152 | * @return \SS_HTTPResponse  | 
            ||
| 153 | */  | 
            ||
| 154 | 	public function createdeployment(SS_HTTPRequest $request) { | 
            ||
| 155 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 156 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 157 | }  | 
            ||
| 158 | |||
| 159 | $this->checkSecurityToken();  | 
            ||
| 160 | |||
| 161 | // @todo the strategy should have been saved when there has been a request for an  | 
            ||
| 162 | // approval or a bypass. This saved state needs to be checked if it's invalidated  | 
            ||
| 163 | // if another deploy happens before this one  | 
            ||
| 164 | 		$isBranchDeploy = (int) $request->postVar('ref_type') === GitDispatcher::REF_TYPE_BRANCH; | 
            ||
| 165 | |||
| 166 | $options = [  | 
            ||
| 167 | 			'sha' => $request->postVar('ref'), | 
            ||
| 168 | 			'ref_type' => $request->postVar('ref_type'), | 
            ||
| 169 | 			'branch' => $isBranchDeploy ? $request->postVar('ref_name') : null, | 
            ||
| 170 | 			'summary' => $request->postVar('summary') | 
            ||
| 171 | ];  | 
            ||
| 172 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options);  | 
            ||
| 173 | |||
| 174 | $strategy->fromArray($request->postVars());  | 
            ||
| 175 | $deployment = $strategy->createDeployment();  | 
            ||
| 176 | |||
| 177 | $deployment->getMachine()->apply(DNDeployment::TR_SUBMIT);  | 
            ||
| 178 | |||
| 179 | return $this->getAPIResponse([  | 
            ||
| 180 | 'message' => 'Deployment has been created',  | 
            ||
| 181 | 'deployment' => $this->formatter->getDeploymentData($deployment),  | 
            ||
| 182 | ], 201);  | 
            ||
| 183 | }  | 
            ||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * @param \SS_HTTPRequest $request  | 
            ||
| 187 | * @return \SS_HTTPResponse  | 
            ||
| 188 | */  | 
            ||
| 189 | 	public function start(SS_HTTPRequest $request) { | 
            ||
| 190 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 191 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 192 | }  | 
            ||
| 193 | |||
| 194 | $this->checkSecurityToken();  | 
            ||
| 195 | |||
| 196 | 		$deployment = DNDeployment::get()->byId($request->postVar('id')); | 
            ||
| 197 | 		if (!$deployment || !$deployment->exists()) { | 
            ||
| 198 | return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404);  | 
            ||
| 199 | }  | 
            ||
| 200 | |||
| 201 | // The deployment cannot be started until it has been approved, or bypassed straight to approved state  | 
            ||
| 202 | 		if ($deployment->State != DNDeployment::STATE_APPROVED) { | 
            ||
| 203 | return $this->getAPIResponse(['message' => 'This deployment has not been approved. Cannot deploy'], 403);  | 
            ||
| 204 | }  | 
            ||
| 205 | |||
| 206 | // until we have a system that can invalidate currently scheduled deployments due  | 
            ||
| 207 | // to emergency deploys etc, replan the deployment to check if it's still valid.  | 
            ||
| 208 | $options = $deployment->getDeploymentStrategy()->getOptions();  | 
            ||
| 209 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options);  | 
            ||
| 210 | $deployment->Strategy = $strategy->toJSON();  | 
            ||
| 211 | $deployment->write();  | 
            ||
| 212 | |||
| 213 | $deployment->getMachine()->apply(DNDeployment::TR_QUEUE);  | 
            ||
| 214 | |||
| 215 | 		$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); | 
            ||
| 216 | |||
| 217 | $response = $this->getAPIResponse([  | 
            ||
| 218 | 'message' => 'Deployment has been queued',  | 
            ||
| 219 | 'location' => $location,  | 
            ||
| 220 | 'deployment' => $this->formatter->getDeploymentData($deployment),  | 
            ||
| 221 | ], 201);  | 
            ||
| 222 | |||
| 223 | 		$response->addHeader('Location', $location); | 
            ||
| 224 | |||
| 225 | return $response;  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * @param string $action  | 
            ||
| 230 | * @return string  | 
            ||
| 231 | */  | 
            ||
| 232 | 	public function Link($action = '') { | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * @param string $name  | 
            ||
| 238 | * @return array  | 
            ||
| 239 | */  | 
            ||
| 240 | 	public function getModel($name = '') { | 
            ||
| 243 | |||
| 244 | /**  | 
            ||
| 245 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return  | 
            ||
| 246 | * an APIResponse with the error, otherwise null.  | 
            ||
| 247 | *  | 
            ||
| 248 | * @param \DNDeployment $deployment  | 
            ||
| 249 | *  | 
            ||
| 250 | * @return null|SS_HTTPResponse  | 
            ||
| 251 | */  | 
            ||
| 252 | View Code Duplication | 	protected function validateDeployment(\DNDeployment $deployment) { | 
            |
| 264 | |||
| 265 | }  | 
            ||
| 266 | 
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.