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 | View Code Duplication | public function submit(SS_HTTPRequest $request) { |
|
119 | |||
120 | /** |
||
121 | * @param \SS_HTTPRequest $request |
||
122 | * @return \SS_HTTPResponse |
||
123 | */ |
||
124 | View Code Duplication | public function cancel(SS_HTTPRequest $request) { |
|
149 | |||
150 | /** |
||
151 | * @param \SS_HTTPRequest $request |
||
152 | * @return \SS_HTTPResponse |
||
153 | */ |
||
154 | public function approve(SS_HTTPRequest $request) { |
||
202 | |||
203 | /** |
||
204 | * @param \SS_HTTPRequest $request |
||
205 | * @return \SS_HTTPResponse |
||
206 | */ |
||
207 | public function reject(SS_HTTPRequest $request) { |
||
235 | |||
236 | /** |
||
237 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
238 | * an APIResponse with the error, otherwise null. |
||
239 | * |
||
240 | * @param \DNDeployment $deployment |
||
241 | * |
||
242 | * @return null|SS_HTTPResponse |
||
243 | */ |
||
244 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
256 | |||
257 | /** |
||
258 | * @return ArrayList |
||
259 | */ |
||
260 | protected function getApprovers() { |
||
273 | |||
274 | /** |
||
275 | * @param string $name |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getModel($name = '') { |
||
281 | |||
282 | /** |
||
283 | * @param string $action |
||
284 | * @return string |
||
285 | */ |
||
286 | public function Link($action = '') { |
||
289 | |||
290 | } |
||
291 |
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
.