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 | private static $allowed_actions = [ |
||
| 15 | 'history', |
||
| 16 | 'upcoming', |
||
| 17 | 'currentbuild', |
||
| 18 | 'show', |
||
| 19 | 'delete', |
||
| 20 | 'log', |
||
| 21 | 'redeploy', |
||
| 22 | 'summary', |
||
| 23 | 'createdeployment', |
||
| 24 | 'start' |
||
| 25 | ]; |
||
| 26 | |||
| 27 | private static $dependencies = [ |
||
| 28 | 'formatter' => '%$DeploynautAPIFormatter' |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \DNProject |
||
| 33 | */ |
||
| 34 | protected $project = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \DNEnvironment |
||
| 38 | */ |
||
| 39 | protected $environment = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private static $action_types = [ |
||
| 45 | self::ACTION_DEPLOY |
||
| 46 | ]; |
||
| 47 | |||
| 48 | public function init() { |
||
| 49 | parent::init(); |
||
| 50 | |||
| 51 | $this->project = $this->getCurrentProject(); |
||
| 52 | |||
| 53 | if (!$this->project) { |
||
| 54 | return $this->project404Response(); |
||
| 55 | } |
||
| 56 | |||
| 57 | // Performs canView permission check by limiting visible projects |
||
| 58 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
| 59 | if (!$this->environment) { |
||
| 60 | return $this->environment404Response(); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param \SS_HTTPRequest $request |
||
| 66 | * @return \HTMLText|\SS_HTTPResponse |
||
| 67 | */ |
||
| 68 | public function index(\SS_HTTPRequest $request) { |
||
| 69 | return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param \SS_HTTPRequest $request |
||
| 74 | * @return \SS_HTTPResponse |
||
| 75 | */ |
||
| 76 | public function history(\SS_HTTPRequest $request) { |
||
| 77 | $data = []; |
||
| 78 | |||
| 79 | $list = $this->environment->DeployHistory('DeployStarted'); |
||
| 80 | |||
| 81 | $fromTimestamp = $request->requestVar('from'); |
||
| 82 | if ($fromTimestamp) { |
||
| 83 | $from = SS_Datetime::create(); |
||
| 84 | $from->setValue($fromTimestamp); |
||
| 85 | $list = $list->filter('LastEdited:GreaterThan', $from->Format('Y-m-d H:i:s')); |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach ($list as $deployment) { |
||
| 89 | $data[] = $this->formatter->getDeploymentData($deployment); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->getAPIResponse([ |
||
| 93 | 'list' => $data, |
||
| 94 | ], 200); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param \SS_HTTPRequest $request |
||
| 99 | * @return \SS_HTTPResponse |
||
| 100 | */ |
||
| 101 | public function upcoming(\SS_HTTPRequest $request) { |
||
| 102 | $data = []; |
||
| 103 | $list = $this->environment->UpcomingDeployments(); |
||
| 104 | foreach ($list as $deployment) { |
||
| 105 | $data[] = $this->formatter->getDeploymentData($deployment); |
||
| 106 | } |
||
| 107 | return $this->getAPIResponse([ |
||
| 108 | 'list' => $data, |
||
| 109 | ], 200); |
||
| 110 | } |
||
| 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 delete(\SS_HTTPRequest $request) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param \SS_HTTPRequest $request |
||
| 165 | * @return \SS_HTTPResponse |
||
| 166 | */ |
||
| 167 | public function log(\SS_HTTPRequest $request) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param \SS_HTTPRequest $request |
||
| 188 | * @return \SS_HTTPResponse |
||
| 189 | */ |
||
| 190 | public function redeploy(\SS_HTTPRequest $request) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return a summary of the deployment changes without creating the deployment. |
||
| 212 | * |
||
| 213 | * @param \SS_HTTPRequest $request |
||
| 214 | * @return \SS_HTTPResponse |
||
| 215 | */ |
||
| 216 | public function summary(\SS_HTTPRequest $request) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Create deployment. Can't use {@link create()} as it's taken by Object. |
||
| 235 | * |
||
| 236 | * @param \SS_HTTPRequest $request |
||
| 237 | * @return \SS_HTTPResponse |
||
| 238 | */ |
||
| 239 | public function createdeployment(\SS_HTTPRequest $request) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param \SS_HTTPRequest $request |
||
| 295 | * @return \SS_HTTPResponse |
||
| 296 | */ |
||
| 297 | public function start(\SS_HTTPRequest $request) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $action |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function Link($action = '') { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $name |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function getModel($name = '') { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @var array |
||
| 365 | * @return \DeploymentStrategy |
||
| 366 | */ |
||
| 367 | protected function createStrategy($options) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param ArrayData $interface |
||
| 400 | * @param $changes |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | protected function canCompareCodeVersions(\ArrayData $interface, $changes) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 425 | * an APIResponse with the error, otherwise null. |
||
| 426 | * |
||
| 427 | * @param \DNDeployment $deployment |
||
| 428 | * |
||
| 429 | * @return null|SS_HTTPResponse |
||
| 430 | */ |
||
| 431 | View Code Duplication | protected function validateDeployment($deployment) { |
|
| 443 | |||
| 444 | } |
||
| 445 |
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.