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 | /** |
||
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 | public function submit(SS_HTTPRequest $request) { |
||
95 | |||
96 | /** |
||
97 | * @param \SS_HTTPRequest $request |
||
98 | * @return \SS_HTTPResponse |
||
99 | */ |
||
100 | public function cancel(SS_HTTPRequest $request) { |
||
127 | |||
128 | /** |
||
129 | * @param \SS_HTTPRequest $request |
||
130 | * @return \SS_HTTPResponse |
||
131 | */ |
||
132 | View Code Duplication | public function approve(SS_HTTPRequest $request) { |
|
160 | |||
161 | /** |
||
162 | * @param \SS_HTTPRequest $request |
||
163 | * @return \SS_HTTPResponse |
||
164 | */ |
||
165 | View Code Duplication | public function reject(SS_HTTPRequest $request) { |
|
195 | |||
196 | /** |
||
197 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
198 | * an APIResponse with the error, otherwise null. |
||
199 | * |
||
200 | * @param \DNDeployment $deployment |
||
201 | * |
||
202 | * @return null|SS_HTTPResponse |
||
203 | */ |
||
204 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
213 | |||
214 | protected function canApprove(Member $member = null) { |
||
240 | |||
241 | /** |
||
242 | * @param string $name |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getModel($name = '') { |
||
248 | |||
249 | /** |
||
250 | * @param string $action |
||
251 | * @return string |
||
252 | */ |
||
253 | public function Link($action = '') { |
||
256 | |||
257 | } |
||
258 |
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
.