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) { |
||
131 | |||
132 | /** |
||
133 | * @param \SS_HTTPRequest $request |
||
134 | * @return \SS_HTTPResponse |
||
135 | */ |
||
136 | public function cancel(SS_HTTPRequest $request) { |
||
161 | |||
162 | /** |
||
163 | * @param \SS_HTTPRequest $request |
||
164 | * @return \SS_HTTPResponse |
||
165 | */ |
||
166 | public function approve(SS_HTTPRequest $request) { |
||
214 | |||
215 | /** |
||
216 | * @param \SS_HTTPRequest $request |
||
217 | * @return \SS_HTTPResponse |
||
218 | */ |
||
219 | public function reject(SS_HTTPRequest $request) { |
||
247 | |||
248 | /** |
||
249 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
250 | * an APIResponse with the error, otherwise null. |
||
251 | * |
||
252 | * @param \DNDeployment $deployment |
||
253 | * |
||
254 | * @return null|SS_HTTPResponse |
||
255 | */ |
||
256 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
268 | |||
269 | /** |
||
270 | * @return ArrayList |
||
271 | */ |
||
272 | protected function getApprovers() { |
||
285 | |||
286 | /** |
||
287 | * @param string $name |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getModel($name = '') { |
||
293 | |||
294 | /** |
||
295 | * @param string $action |
||
296 | * @return string |
||
297 | */ |
||
298 | public function Link($action = '') { |
||
301 | |||
302 | } |
||
303 |
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
.