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 | 'createdeployment', |
||
| 23 | 'start' |
||
| 24 | ]; |
||
| 25 | |||
| 26 | private static $dependencies = [ |
||
| 27 | 'formatter' => '%$DeploynautAPIFormatter' |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \DNProject |
||
| 32 | */ |
||
| 33 | protected $project = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \DNEnvironment |
||
| 37 | */ |
||
| 38 | protected $environment = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private static $action_types = [ |
||
| 44 | self::ACTION_DEPLOY |
||
| 45 | ]; |
||
| 46 | |||
| 47 | View Code Duplication | public function init() { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param \SS_HTTPRequest $request |
||
| 65 | * @return \HTMLText|\SS_HTTPResponse |
||
| 66 | */ |
||
| 67 | public function index(\SS_HTTPRequest $request) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param \SS_HTTPRequest $request |
||
| 73 | * @return \SS_HTTPResponse |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function history(\SS_HTTPRequest $request) { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param \SS_HTTPRequest $request |
||
| 91 | * @return \SS_HTTPResponse |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function upcoming(\SS_HTTPRequest $request) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param \SS_HTTPRequest $request |
||
| 106 | * @return \SS_HTTPResponse |
||
| 107 | */ |
||
| 108 | public function currentbuild(\SS_HTTPRequest $request) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param \SS_HTTPRequest $request |
||
| 118 | * @return \SS_HTTPResponse |
||
| 119 | */ |
||
| 120 | public function show(\SS_HTTPRequest $request) { |
||
| 128 | |||
| 129 | public function delete(\SS_HTTPRequest $request) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param \SS_HTTPRequest $request |
||
| 153 | * @return \SS_HTTPResponse |
||
| 154 | */ |
||
| 155 | public function log(\SS_HTTPRequest $request) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param \SS_HTTPRequest $request |
||
| 174 | * @return \SS_HTTPResponse |
||
| 175 | */ |
||
| 176 | public function redeploy(\SS_HTTPRequest $request) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Create deployment. Can't use {@link create()} as it's taken by Object. |
||
| 194 | * |
||
| 195 | * @param \SS_HTTPRequest $request |
||
| 196 | * @return \SS_HTTPResponse |
||
| 197 | */ |
||
| 198 | public function createdeployment(\SS_HTTPRequest $request) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param \SS_HTTPRequest $request |
||
| 247 | * @return \SS_HTTPResponse |
||
| 248 | */ |
||
| 249 | public function start(\SS_HTTPRequest $request) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $action |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function Link($action = '') { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $name |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function getModel($name = '') { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 307 | * an APIResponse with the error, otherwise null. |
||
| 308 | * |
||
| 309 | * @param \DNDeployment $deployment |
||
| 310 | * |
||
| 311 | * @return null|SS_HTTPResponse |
||
| 312 | */ |
||
| 313 | View Code Duplication | protected function validateDeployment($deployment) { |
|
| 325 | |||
| 326 | } |
||
| 327 |
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.