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 | '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 | public function init() { |
||
43 | |||
44 | /** |
||
45 | * @param \SS_HTTPRequest $request |
||
46 | * @return \SS_HTTPResponse |
||
47 | */ |
||
48 | public function approvers(SS_HTTPRequest $request) { |
||
65 | |||
66 | /** |
||
67 | * @param \SS_HTTPRequest $request |
||
68 | * @return \SS_HTTPResponse |
||
69 | */ |
||
70 | public function submit(SS_HTTPRequest $request) { |
||
77 | |||
78 | /** |
||
79 | * @param \SS_HTTPRequest $request |
||
80 | * @return \SS_HTTPResponse |
||
81 | */ |
||
82 | public function cancel(SS_HTTPRequest $request) { |
||
109 | |||
110 | /** |
||
111 | * @param \SS_HTTPRequest $request |
||
112 | * @return \SS_HTTPResponse |
||
113 | */ |
||
114 | View Code Duplication | public function approve(SS_HTTPRequest $request) { |
|
142 | |||
143 | /** |
||
144 | * @param \SS_HTTPRequest $request |
||
145 | * @return \SS_HTTPResponse |
||
146 | */ |
||
147 | View Code Duplication | public function reject(SS_HTTPRequest $request) { |
|
177 | |||
178 | /** |
||
179 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
180 | * an APIResponse with the error, otherwise null. |
||
181 | * |
||
182 | * @param \DNDeployment $deployment |
||
183 | * |
||
184 | * @return null|SS_HTTPResponse |
||
185 | */ |
||
186 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
195 | |||
196 | protected function canApprove(Member $member = null) { |
||
218 | |||
219 | /** |
||
220 | * @param string $name |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getModel($name = '') { |
||
227 | |||
228 | /** |
||
229 | * @param string $action |
||
230 | * @return string |
||
231 | */ |
||
232 | public function Link($action = '') { |
||
235 | |||
236 | } |
||
237 |
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
.