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) { |
||
| 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) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
| 285 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
| 286 | * user who can create the deployment but is not part of the stack roles. |
||
| 287 | * |
||
| 288 | * @param Member $member |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | protected function getStackMemberData(Member $member) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 313 | * an APIResponse with the error, otherwise null. |
||
| 314 | * |
||
| 315 | * @param \DNDeployment $deployment |
||
| 316 | * |
||
| 317 | * @return null|SS_HTTPResponse |
||
| 318 | */ |
||
| 319 | protected function validateDeployment(\DNDeployment $deployment) { |
||
| 328 | |||
| 329 | } |
||
| 330 |
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.