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 | * @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 | public function init() { |
||
| 56 | parent::init(); |
||
| 57 | |||
| 58 | $this->project = $this->getCurrentProject(); |
||
| 59 | |||
| 60 | if (!$this->project) { |
||
| 61 | return $this->project404Response(); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Performs canView permission check by limiting visible projects |
||
| 65 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
| 66 | if (!$this->environment) { |
||
| 67 | return $this->environment404Response(); |
||
| 68 | } |
||
| 69 | } |
||
| 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 | public function save(\SS_HTTPRequest $request) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param \SS_HTTPRequest $request |
||
| 193 | * @return \SS_HTTPResponse |
||
| 194 | */ |
||
| 195 | public function start(SS_HTTPRequest $request) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $action |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function Link($action = '') { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $name |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public function getModel($name = '') { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return data about a single deployment for use in API response. |
||
| 254 | * @param DNDeployment $deployment |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | protected function getDeploymentData(DNDeployment $deployment) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
| 318 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
| 319 | * user who can create the deployment but is not part of the stack roles. |
||
| 320 | * |
||
| 321 | * @param Member $member |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | protected function getStackMemberData(Member $member) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 349 | * an APIResponse with the error, otherwise null. |
||
| 350 | * |
||
| 351 | * @param \DNDeployment $deployment |
||
| 352 | * |
||
| 353 | * @return null|SS_HTTPResponse |
||
| 354 | */ |
||
| 355 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
| 367 | |||
| 368 | } |
||
| 369 |
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.