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:
| 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 | 'approve', |
||
| 13 | 'reject' |
||
| 14 | ]; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var \DNProject |
||
| 18 | */ |
||
| 19 | protected $project = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var \DNEnvironment |
||
| 23 | */ |
||
| 24 | protected $environment = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $action_types = [ |
||
| 30 | self::ACTION_APPROVALS |
||
| 31 | ]; |
||
| 32 | |||
| 33 | public function init() { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param \SS_HTTPRequest $request |
||
| 44 | * @return \SS_HTTPResponse |
||
| 45 | */ |
||
| 46 | public function approvers(SS_HTTPRequest $request) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param \SS_HTTPRequest $request |
||
| 66 | * @return \SS_HTTPResponse |
||
| 67 | */ |
||
| 68 | public function submit(SS_HTTPRequest $request) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param \SS_HTTPRequest $request |
||
| 78 | * @return \SS_HTTPResponse |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function approve(SS_HTTPRequest $request) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param \SS_HTTPRequest $request |
||
| 111 | * @return \SS_HTTPResponse |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function reject(SS_HTTPRequest $request) { |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
| 146 | * an APIResponse with the error, otherwise null. |
||
| 147 | * |
||
| 148 | * @param \DNDeployment $deployment |
||
| 149 | * |
||
| 150 | * @return null|SS_HTTPResponse |
||
| 151 | */ |
||
| 152 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
| 161 | |||
| 162 | protected function canApprove(Member $member = null) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $name |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function getModel($name = '') { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $action |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function Link($action = '') { |
||
| 201 | |||
| 202 | } |
||
| 203 |
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.