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 | public function init() { |
||
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) { |
||
102 | |||
103 | /** |
||
104 | * @param \SS_HTTPRequest $request |
||
105 | * @return \SS_HTTPResponse |
||
106 | */ |
||
107 | public function upcoming(SS_HTTPRequest $request) { |
||
118 | |||
119 | /** |
||
120 | * @param \SS_HTTPRequest $request |
||
121 | * @return \SS_HTTPResponse |
||
122 | */ |
||
123 | public function currentbuild(SS_HTTPRequest $request) { |
||
130 | |||
131 | /** |
||
132 | * @param \SS_HTTPRequest $request |
||
133 | * @return \SS_HTTPResponse |
||
134 | */ |
||
135 | public function show(SS_HTTPRequest $request) { |
||
143 | |||
144 | /** |
||
145 | * @param \SS_HTTPRequest $request |
||
146 | * @return \SS_HTTPResponse |
||
147 | */ |
||
148 | public function log(SS_HTTPRequest $request) { |
||
160 | |||
161 | public function save(\SS_HTTPRequest $request) { |
||
191 | |||
192 | /** |
||
193 | * @param \SS_HTTPRequest $request |
||
194 | * @return \SS_HTTPResponse |
||
195 | */ |
||
196 | public function start(SS_HTTPRequest $request) { |
||
233 | |||
234 | /** |
||
235 | * @param string $action |
||
236 | * @return string |
||
237 | */ |
||
238 | public function Link($action = '') { |
||
241 | |||
242 | /** |
||
243 | * @param string $name |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getModel($name = '') { |
||
249 | |||
250 | /** |
||
251 | * Return data about a single deployment for use in API response. |
||
252 | * @param DNDeployment $deployment |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function getDeploymentData(DNDeployment $deployment) { |
||
287 | |||
288 | /** |
||
289 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
290 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
291 | * user who can create the deployment but is not part of the stack roles. |
||
292 | * |
||
293 | * @param Member $member |
||
294 | * @return array |
||
295 | */ |
||
296 | protected function getStackMemberData(Member $member) { |
||
315 | |||
316 | /** |
||
317 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
318 | * an APIResponse with the error, otherwise null. |
||
319 | * |
||
320 | * @param \DNDeployment $deployment |
||
321 | * |
||
322 | * @return null|SS_HTTPResponse |
||
323 | */ |
||
324 | protected function validateDeployment(\DNDeployment $deployment) { |
||
333 | |||
334 | } |
||
335 |
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
.