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 ApprovalsDispatcher 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 ApprovalsDispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class ApprovalsDispatcher extends Dispatcher { |
||
4 | |||
5 | const ACTION_APPROVALS = 'approvals'; |
||
6 | |||
7 | const ALLOW_APPROVAL = 'ALLOW_APPROVAL'; |
||
8 | |||
9 | const ALLOW_APPROVAL_BYPASS = 'ALLOW_APPROVAL_BYPASS'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $allowed_actions = [ |
||
15 | 'approvers', |
||
16 | 'submit', |
||
17 | 'cancel', |
||
18 | 'approve', |
||
19 | 'reject' |
||
20 | ]; |
||
21 | |||
22 | private static $dependencies = [ |
||
23 | 'formatter' => '%$DeploynautAPIFormatter' |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var \DNProject |
||
28 | */ |
||
29 | protected $project = null; |
||
30 | |||
31 | /** |
||
32 | * @var \DNEnvironment |
||
33 | */ |
||
34 | protected $environment = null; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $action_types = [ |
||
40 | self::ACTION_APPROVALS |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * This is a per request cache of $this->project()->listMembers() |
||
45 | * @var null|array |
||
46 | */ |
||
47 | private static $_cache_project_members = null; |
||
48 | |||
49 | View Code Duplication | public function init() { |
|
63 | |||
64 | /** |
||
65 | * @param \SS_HTTPRequest $request |
||
66 | * @return \SS_HTTPResponse |
||
67 | */ |
||
68 | public function approvers(SS_HTTPRequest $request) { |
||
90 | |||
91 | /** |
||
92 | * @param \SS_HTTPRequest $request |
||
93 | * @return \SS_HTTPResponse |
||
94 | */ |
||
95 | public function submit(SS_HTTPRequest $request) { |
||
96 | if ($request->httpMethod() !== 'POST') { |
||
97 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
98 | } |
||
99 | |||
100 | $deployment = DNDeployment::get()->byId($request->postVar('id')); |
||
101 | $errorResponse = $this->validateDeployment($deployment); |
||
102 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
103 | return $errorResponse; |
||
104 | } |
||
105 | |||
106 | $approver = Member::get()->byId($request->postVar('approver_id')); |
||
107 | if ($approver && $approver->exists()) { |
||
108 | if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) { |
||
109 | return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403); |
||
110 | } |
||
111 | |||
112 | $deployment->ApproverID = $approver->ID; |
||
113 | $deployment->write(); |
||
114 | } |
||
115 | |||
116 | try { |
||
117 | $deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
||
118 | } catch (\Exception $e) { |
||
119 | return $this->getAPIResponse([ |
||
120 | 'message' => $e->getMessage() |
||
121 | ], 400); |
||
122 | } |
||
123 | |||
124 | return $this->getAPIResponse([ |
||
125 | 'message' => 'Deployment request has been submitted', |
||
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) { |
||
159 | |||
160 | /** |
||
161 | * @param \SS_HTTPRequest $request |
||
162 | * @return \SS_HTTPResponse |
||
163 | */ |
||
164 | public function approve(SS_HTTPRequest $request) { |
||
219 | |||
220 | /** |
||
221 | * @param \SS_HTTPRequest $request |
||
222 | * @return \SS_HTTPResponse |
||
223 | */ |
||
224 | public function reject(SS_HTTPRequest $request) { |
||
259 | |||
260 | /** |
||
261 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
262 | * an APIResponse with the error, otherwise null. |
||
263 | * |
||
264 | * @param \DNDeployment $deployment |
||
265 | * |
||
266 | * @return null|SS_HTTPResponse |
||
267 | */ |
||
268 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
280 | |||
281 | /** |
||
282 | * @param string $name |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getModel($name = '') { |
||
288 | |||
289 | /** |
||
290 | * @param string $action |
||
291 | * @return string |
||
292 | */ |
||
293 | public function Link($action = '') { |
||
296 | |||
297 | } |
||
298 |
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
.