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 | 'create', |
||
| 21 | 'createdeployment' |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \DNProject |
||
| 26 | */ |
||
| 27 | protected $project = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \DNEnvironment |
||
| 31 | */ |
||
| 32 | protected $environment = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private static $action_types = [ |
||
| 38 | self::ACTION_DEPLOY |
||
| 39 | ]; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * This is a per request cache of $this->project()->listMembers() |
||
| 43 | * |
||
| 44 | * @var null|array |
||
| 45 | */ |
||
| 46 | private static $_cache_project_members = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This is a per request cache of $this->environment->CurrentBuild(); |
||
| 50 | * |
||
| 51 | * @var null|DNDeployment |
||
| 52 | */ |
||
| 53 | private static $_cache_current_build = null; |
||
| 54 | |||
| 55 | View Code Duplication | public function init() { |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param \SS_HTTPRequest $request |
||
| 73 | * @return \HTMLText|\SS_HTTPResponse |
||
| 74 | */ |
||
| 75 | public function index(\SS_HTTPRequest $request) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param \SS_HTTPRequest $request |
||
| 81 | * @return \SS_HTTPResponse |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function history(SS_HTTPRequest $request) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param \SS_HTTPRequest $request |
||
| 99 | * @return \SS_HTTPResponse |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function upcoming(SS_HTTPRequest $request) { |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param \SS_HTTPRequest $request |
||
| 114 | * @return \SS_HTTPResponse |
||
| 115 | */ |
||
| 116 | public function currentbuild(SS_HTTPRequest $request) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param \SS_HTTPRequest $request |
||
| 126 | * @return \SS_HTTPResponse |
||
| 127 | */ |
||
| 128 | public function show(SS_HTTPRequest $request) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param \SS_HTTPRequest $request |
||
| 139 | * @return \SS_HTTPResponse |
||
| 140 | */ |
||
| 141 | public function log(SS_HTTPRequest $request) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Create deployment. Can't use {@link create()} as it's taken by Object. |
||
| 160 | * |
||
| 161 | * @param \SS_HTTPRequest $request |
||
| 162 | * @return \SS_HTTPResponse |
||
| 163 | */ |
||
| 164 | public function createdeployment(SS_HTTPRequest $request) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param \SS_HTTPRequest $request |
||
| 198 | * @return \SS_HTTPResponse |
||
| 199 | */ |
||
| 200 | public function start(SS_HTTPRequest $request) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $action |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function Link($action = '') { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $name |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function getModel($name = '') { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Return data about a single deployment for use in API response. |
||
| 258 | * @param DNDeployment $deployment |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | protected function getDeploymentData(DNDeployment $deployment) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
| 322 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
| 323 | * user who can create the deployment but is not part of the stack roles. |
||
| 324 | * |
||
| 325 | * @param Member $member |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | protected function getStackMemberData(Member $member) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 353 | * an APIResponse with the error, otherwise null. |
||
| 354 | * |
||
| 355 | * @param \DNDeployment $deployment |
||
| 356 | * |
||
| 357 | * @return null|SS_HTTPResponse |
||
| 358 | */ |
||
| 359 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
| 371 | |||
| 372 | } |
||
| 373 |
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.