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 DeployDispatcher 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 DeployDispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class DeployDispatcher extends Dispatcher { |
||
8 | |||
9 | const ACTION_DEPLOY = 'deploys'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | public static $allowed_actions = [ |
||
15 | 'history', |
||
16 | 'upcoming', |
||
17 | 'currentbuild', |
||
18 | 'show', |
||
19 | 'log', |
||
20 | 'start', |
||
21 | 'save' |
||
22 | |||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var \DNProject |
||
27 | */ |
||
28 | protected $project = null; |
||
29 | |||
30 | /** |
||
31 | * @var \DNEnvironment |
||
32 | */ |
||
33 | protected $environment = null; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private static $action_types = [ |
||
39 | self::ACTION_DEPLOY |
||
40 | ]; |
||
41 | |||
42 | View Code Duplication | public function init() { |
|
43 | parent::init(); |
||
44 | |||
45 | $this->project = $this->getCurrentProject(); |
||
46 | |||
47 | if (!$this->project) { |
||
48 | return $this->project404Response(); |
||
49 | } |
||
50 | |||
51 | // Performs canView permission check by limiting visible projects |
||
52 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
53 | if (!$this->environment) { |
||
54 | return $this->environment404Response(); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param \SS_HTTPRequest $request |
||
60 | * @return \HTMLText|\SS_HTTPResponse |
||
61 | */ |
||
62 | public function index(\SS_HTTPRequest $request) { |
||
65 | |||
66 | /** |
||
67 | * @param \SS_HTTPRequest $request |
||
68 | * @return \SS_HTTPResponse |
||
69 | */ |
||
70 | public function history(SS_HTTPRequest $request) { |
||
98 | |||
99 | /** |
||
100 | * @param \SS_HTTPRequest $request |
||
101 | * @return \SS_HTTPResponse |
||
102 | */ |
||
103 | public function upcoming(SS_HTTPRequest $request) { |
||
113 | |||
114 | /** |
||
115 | * @param \SS_HTTPRequest $request |
||
116 | * @return \SS_HTTPResponse |
||
117 | */ |
||
118 | public function currentbuild(SS_HTTPRequest $request) { |
||
125 | |||
126 | /** |
||
127 | * @param \SS_HTTPRequest $request |
||
128 | * @return \SS_HTTPResponse |
||
129 | */ |
||
130 | public function show(SS_HTTPRequest $request) { |
||
138 | |||
139 | /** |
||
140 | * @param \SS_HTTPRequest $request |
||
141 | * @return \SS_HTTPResponse |
||
142 | */ |
||
143 | public function log(SS_HTTPRequest $request) { |
||
155 | |||
156 | public function save(\SS_HTTPRequest $request) { |
||
186 | |||
187 | /** |
||
188 | * @param \SS_HTTPRequest $request |
||
189 | * @return \SS_HTTPResponse |
||
190 | */ |
||
191 | public function start(SS_HTTPRequest $request) { |
||
228 | |||
229 | /** |
||
230 | * @param string $action |
||
231 | * @return string |
||
232 | */ |
||
233 | public function Link($action = '') { |
||
236 | |||
237 | /** |
||
238 | * @param string $name |
||
239 | * @return array |
||
240 | */ |
||
241 | public function getModel($name = '') { |
||
244 | |||
245 | /** |
||
246 | * Return data about a single deployment for use in API response. |
||
247 | * @param DNDeployment $deployment |
||
248 | * @return array |
||
249 | */ |
||
250 | protected function getDeploymentData(DNDeployment $deployment) { |
||
295 | |||
296 | /** |
||
297 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
298 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
299 | * user who can create the deployment but is not part of the stack roles. |
||
300 | * |
||
301 | * @param Member $member |
||
302 | * @return array |
||
303 | */ |
||
304 | protected function getStackMemberData(Member $member) { |
||
323 | |||
324 | /** |
||
325 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
326 | * an APIResponse with the error, otherwise null. |
||
327 | * |
||
328 | * @param \DNDeployment $deployment |
||
329 | * |
||
330 | * @return null|SS_HTTPResponse |
||
331 | */ |
||
332 | protected function validateDeployment(\DNDeployment $deployment) { |
||
341 | |||
342 | } |
||
343 |
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
.