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  | 
            ||
| 3 | class ApprovalsDispatcher extends Dispatcher { | 
            ||
| 4 | |||
| 5 | const ACTION_APPROVALS = 'approvals';  | 
            ||
| 6 | |||
| 7 | const ALLOW_APPROVAL = 'ALLOW_APPROVAL';  | 
            ||
| 8 | |||
| 9 | /**  | 
            ||
| 10 | * @var array  | 
            ||
| 11 | */  | 
            ||
| 12 | private static $allowed_actions = [  | 
            ||
| 13 | 'approvers',  | 
            ||
| 14 | 'submit',  | 
            ||
| 15 | 'cancel',  | 
            ||
| 16 | 'approve',  | 
            ||
| 17 | 'reject'  | 
            ||
| 18 | ];  | 
            ||
| 19 | |||
| 20 | private static $dependencies = [  | 
            ||
| 21 | 'formatter' => '%$DeploynautAPIFormatter'  | 
            ||
| 22 | ];  | 
            ||
| 23 | |||
| 24 | /**  | 
            ||
| 25 | * @var \DNProject  | 
            ||
| 26 | */  | 
            ||
| 27 | protected $project = null;  | 
            ||
| 28 | |||
| 29 | /**  | 
            ||
| 30 | * @var \DNEnvironment  | 
            ||
| 31 | */  | 
            ||
| 32 | protected $environment = null;  | 
            ||
| 33 | |||
| 34 | /**  | 
            ||
| 35 | * @var array  | 
            ||
| 36 | */  | 
            ||
| 37 | private static $action_types = [  | 
            ||
| 38 | self::ACTION_APPROVALS  | 
            ||
| 39 | ];  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * This is a per request cache of $this->project()->listMembers()  | 
            ||
| 43 | * @var null|array  | 
            ||
| 44 | */  | 
            ||
| 45 | private static $_cache_project_members = null;  | 
            ||
| 46 | |||
| 47 | View Code Duplication | 	public function init() { | 
            |
| 48 | parent::init();  | 
            ||
| 49 | |||
| 50 | $this->project = $this->getCurrentProject();  | 
            ||
| 51 | 		if (!$this->project) { | 
            ||
| 52 | return $this->project404Response();  | 
            ||
| 53 | }  | 
            ||
| 54 | |||
| 55 | // Performs canView permission check by limiting visible projects  | 
            ||
| 56 | $this->environment = $this->getCurrentEnvironment($this->project);  | 
            ||
| 57 | 		if (!$this->environment) { | 
            ||
| 58 | return $this->environment404Response();  | 
            ||
| 59 | }  | 
            ||
| 60 | }  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @param \SS_HTTPRequest $request  | 
            ||
| 64 | * @return \SS_HTTPResponse  | 
            ||
| 65 | */  | 
            ||
| 66 | 	public function approvers(SS_HTTPRequest $request) { | 
            ||
| 67 | $list = [];  | 
            ||
| 68 | |||
| 69 | 		if (self::$_cache_project_members === null) { | 
            ||
| 70 | self::$_cache_project_members = $this->project->listMembers();  | 
            ||
| 71 | }  | 
            ||
| 72 | |||
| 73 | 		foreach (self::$_cache_project_members as $data) { | 
            ||
| 74 | 			if ($this->project->allowed(self::ALLOW_APPROVAL, Member::get()->byId($data['MemberID']))) { | 
            ||
| 75 | $list[] = [  | 
            ||
| 76 | 'id' => $data['MemberID'],  | 
            ||
| 77 | 'email' => $data['Email'],  | 
            ||
| 78 | 'role' => $data['RoleTitle'],  | 
            ||
| 79 | 'name' => $data['FullName']  | 
            ||
| 80 | ];  | 
            ||
| 81 | }  | 
            ||
| 82 | }  | 
            ||
| 83 | |||
| 84 | return $this->getAPIResponse([  | 
            ||
| 85 | 'approvers' => $list  | 
            ||
| 86 | ], 200);  | 
            ||
| 87 | }  | 
            ||
| 88 | |||
| 89 | /**  | 
            ||
| 90 | * @param \SS_HTTPRequest $request  | 
            ||
| 91 | * @return \SS_HTTPResponse  | 
            ||
| 92 | */  | 
            ||
| 93 | 	public function submit(SS_HTTPRequest $request) { | 
            ||
| 94 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 95 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 | 		$deployment = DNDeployment::get()->byId($request->postVar('id')); | 
            ||
| 99 | $errorResponse = $this->validateDeployment($deployment);  | 
            ||
| 100 | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            ||
| 101 | return $errorResponse;  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | 		$approver = Member::get()->byId($request->postVar('approver_id')); | 
            ||
| 105 | 		if (!$approver || !$approver->exists()) { | 
            ||
| 106 | return $this->getAPIResponse(['message' => 'Invalid approver. Does not exist'], 403);  | 
            ||
| 107 | }  | 
            ||
| 108 | 		if (!$this->project->allowed(self::ALLOW_APPROVAL, $approver)) { | 
            ||
| 109 | return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403);  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | 		try { | 
            ||
| 113 | $deployment->ApproverID = $approver->ID;  | 
            ||
| 114 | $deployment->write();  | 
            ||
| 115 | |||
| 116 | $deployment->getMachine()->apply(DNDeployment::TR_SUBMIT);  | 
            ||
| 117 | 		} catch (\Exception $e) { | 
            ||
| 118 | return $this->getAPIResponse([  | 
            ||
| 119 | 'status' => 'FAILED',  | 
            ||
| 120 | 'message' => $e->getMessage()  | 
            ||
| 121 | ], 400);  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | return $this->getAPIResponse([  | 
            ||
| 125 | 'status' => 'OK',  | 
            ||
| 126 | 'deployment' => $this->formatter->getDeploymentData($deployment)  | 
            ||
| 127 | ], 200);  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * @param \SS_HTTPRequest $request  | 
            ||
| 132 | * @return \SS_HTTPResponse  | 
            ||
| 133 | */  | 
            ||
| 134 | 	public function cancel(SS_HTTPRequest $request) { | 
            ||
| 135 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 136 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 137 | }  | 
            ||
| 138 | |||
| 139 | 		$deployment = DNDeployment::get()->byId($request->postVar('id')); | 
            ||
| 140 | $errorResponse = $this->validateDeployment($deployment);  | 
            ||
| 141 | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            ||
| 142 | return $errorResponse;  | 
            ||
| 143 | }  | 
            ||
| 144 | |||
| 145 | // @todo permission checking for cancelling an approval request  | 
            ||
| 146 | |||
| 147 | 		try { | 
            ||
| 148 | $deployment->getMachine()->apply(DNDeployment::TR_NEW);  | 
            ||
| 149 | 		} catch (\Exception $e) { | 
            ||
| 150 | return $this->getAPIResponse([  | 
            ||
| 151 | 'status' => 'FAILED',  | 
            ||
| 152 | 'message' => $e->getMessage()  | 
            ||
| 153 | ], 400);  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | return $this->getAPIResponse([  | 
            ||
| 157 | 'status' => 'OK',  | 
            ||
| 158 | 'deployment' => $this->formatter->getDeploymentData($deployment)  | 
            ||
| 159 | ], 200);  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * @param \SS_HTTPRequest $request  | 
            ||
| 164 | * @return \SS_HTTPResponse  | 
            ||
| 165 | */  | 
            ||
| 166 | View Code Duplication | 	public function approve(SS_HTTPRequest $request) { | 
            |
| 167 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 168 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | 		$deployment = DNDeployment::get()->byId($request->postVar('id')); | 
            ||
| 172 | $errorResponse = $this->validateDeployment($deployment);  | 
            ||
| 173 | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            ||
| 174 | return $errorResponse;  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | 		if (!$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser())) { | 
            ||
| 178 | return $this->getAPIResponse(['message' => 'You are not authorised to approve this deployment'], 403);  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | 		try { | 
            ||
| 182 | $deployment->getMachine()->apply(DNDeployment::TR_APPROVE);  | 
            ||
| 183 | 		} catch (\Exception $e) { | 
            ||
| 184 | return $this->getAPIResponse([  | 
            ||
| 185 | 'status' => 'FAILED',  | 
            ||
| 186 | 'message' => $e->getMessage()  | 
            ||
| 187 | ], 400);  | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 | return $this->getAPIResponse([  | 
            ||
| 191 | 'status' => 'OK',  | 
            ||
| 192 | 'deployment' => $this->formatter->getDeploymentData($deployment)  | 
            ||
| 193 | ], 200);  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | /**  | 
            ||
| 197 | * @param \SS_HTTPRequest $request  | 
            ||
| 198 | * @return \SS_HTTPResponse  | 
            ||
| 199 | */  | 
            ||
| 200 | View Code Duplication | 	public function reject(SS_HTTPRequest $request) { | 
            |
| 201 | 		if ($request->httpMethod() !== 'POST') { | 
            ||
| 202 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | 		$deployment = DNDeployment::get()->byId($request->postVar('id')); | 
            ||
| 206 | $errorResponse = $this->validateDeployment($deployment);  | 
            ||
| 207 | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            ||
| 208 | return $errorResponse;  | 
            ||
| 209 | }  | 
            ||
| 210 | // reject permissions are the same as can approve  | 
            ||
| 211 | 		if (!$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser())) { | 
            ||
| 212 | return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403);  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | 		try { | 
            ||
| 216 | $deployment->getMachine()->apply(DNDeployment::TR_REJECT);  | 
            ||
| 217 | 		} catch (\Exception $e) { | 
            ||
| 218 | return $this->getAPIResponse([  | 
            ||
| 219 | 'status' => 'FAILED',  | 
            ||
| 220 | 'message' => $e->getMessage()  | 
            ||
| 221 | ], 400);  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | return $this->getAPIResponse([  | 
            ||
| 225 | 'status' => 'OK',  | 
            ||
| 226 | 'deployment' => $this->formatter->getDeploymentData($deployment)  | 
            ||
| 227 | ], 200);  | 
            ||
| 228 | }  | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return  | 
            ||
| 232 | * an APIResponse with the error, otherwise null.  | 
            ||
| 233 | *  | 
            ||
| 234 | * @param \DNDeployment $deployment  | 
            ||
| 235 | *  | 
            ||
| 236 | * @return null|SS_HTTPResponse  | 
            ||
| 237 | */  | 
            ||
| 238 | View Code Duplication | 	protected function validateDeployment(\DNDeployment $deployment) { | 
            |
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * @return ArrayList  | 
            ||
| 253 | */  | 
            ||
| 254 | 	protected function getApprovers() { | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * @param string $name  | 
            ||
| 270 | * @return array  | 
            ||
| 271 | */  | 
            ||
| 272 | 	public function getModel($name = '') { | 
            ||
| 275 | |||
| 276 | /**  | 
            ||
| 277 | * @param string $action  | 
            ||
| 278 | * @return string  | 
            ||
| 279 | */  | 
            ||
| 280 | 	public function Link($action = '') { | 
            ||
| 283 | |||
| 284 | }  | 
            ||
| 285 | 
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.