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 ApprovalsDispatcher 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 ApprovalsDispatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class ApprovalsDispatcher extends Dispatcher { |
||
| 4 | |||
| 5 | const ACTION_APPROVALS = 'approvals'; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @var array |
||
| 9 | */ |
||
| 10 | public static $allowed_actions = [ |
||
| 11 | 'approvers', |
||
| 12 | 'submit', |
||
| 13 | 'cancel', |
||
| 14 | 'approve', |
||
| 15 | 'reject' |
||
| 16 | ]; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var \DNProject |
||
| 20 | */ |
||
| 21 | protected $project = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \DNEnvironment |
||
| 25 | */ |
||
| 26 | protected $environment = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private static $action_types = [ |
||
| 32 | self::ACTION_APPROVALS |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * This is a per request cache of $this->project()->listMembers() |
||
| 37 | * |
||
| 38 | * @var null|array |
||
| 39 | */ |
||
| 40 | private static $_cache_project_members = null; |
||
| 41 | |||
| 42 | View Code Duplication | public function init() { |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param \SS_HTTPRequest $request |
||
| 59 | * @return \SS_HTTPResponse |
||
| 60 | */ |
||
| 61 | public function approvers(SS_HTTPRequest $request) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param \SS_HTTPRequest $request |
||
| 86 | * @return \SS_HTTPResponse |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function submit(SS_HTTPRequest $request) { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param \SS_HTTPRequest $request |
||
| 121 | * @return \SS_HTTPResponse |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function cancel(SS_HTTPRequest $request) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param \SS_HTTPRequest $request |
||
| 154 | * @return \SS_HTTPResponse |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function approve(SS_HTTPRequest $request) { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param \SS_HTTPRequest $request |
||
| 188 | * @return \SS_HTTPResponse |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function reject(SS_HTTPRequest $request) { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 223 | * an APIResponse with the error, otherwise null. |
||
| 224 | * |
||
| 225 | * @param \DNDeployment $deployment |
||
| 226 | * |
||
| 227 | * @return null|SS_HTTPResponse |
||
| 228 | */ |
||
| 229 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
| 241 | |||
| 242 | protected function canApprove(Member $member = null) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $name |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function getModel($name = '') { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $action |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function Link($action = '') { |
||
| 284 | |||
| 285 | } |
||
| 286 |
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.