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 |
||
7 | class DeployDispatcher extends Dispatcher { |
||
8 | |||
9 | const ACTION_DEPLOY = 'deploys'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $action_types = [ |
||
15 | self::ACTION_DEPLOY |
||
16 | ]; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | public static $allowed_actions = [ |
||
22 | 'history', |
||
23 | 'currentbuild', |
||
24 | 'deployment', |
||
25 | 'log', |
||
26 | 'start' |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @var \DNProject |
||
31 | */ |
||
32 | protected $project = null; |
||
33 | |||
34 | /** |
||
35 | * @var \DNEnvironment |
||
36 | */ |
||
37 | protected $environment = null; |
||
38 | |||
39 | View Code Duplication | public function init() { |
|
|
|||
40 | parent::init(); |
||
41 | |||
42 | $this->project = $this->getCurrentProject(); |
||
43 | |||
44 | if (!$this->project) { |
||
45 | return $this->project404Response(); |
||
46 | } |
||
47 | |||
48 | // Performs canView permission check by limiting visible projects |
||
49 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
50 | if (!$this->environment) { |
||
51 | return $this->environment404Response(); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param \SS_HTTPRequest $request |
||
57 | * @return \HTMLText|\SS_HTTPResponse |
||
58 | */ |
||
59 | public function index(\SS_HTTPRequest $request) { |
||
62 | |||
63 | /** |
||
64 | * @param \SS_HTTPRequest $request |
||
65 | * @return \SS_HTTPResponse |
||
66 | */ |
||
67 | public function history(SS_HTTPRequest $request) { |
||
94 | |||
95 | /** |
||
96 | * @param \SS_HTTPRequest $request |
||
97 | * @return \SS_HTTPResponse |
||
98 | */ |
||
99 | public function currentbuild(SS_HTTPRequest $request) { |
||
106 | |||
107 | /** |
||
108 | * @param \SS_HTTPRequest $request |
||
109 | * @return \SS_HTTPResponse |
||
110 | */ |
||
111 | public function deployment(SS_HTTPRequest $request) { |
||
121 | |||
122 | /** |
||
123 | * @param \SS_HTTPRequest $request |
||
124 | * @return \SS_HTTPResponse |
||
125 | */ |
||
126 | public function log(SS_HTTPRequest $request) { |
||
141 | |||
142 | /** |
||
143 | * @param \SS_HTTPRequest $request |
||
144 | * @return \SS_HTTPResponse |
||
145 | */ |
||
146 | public function start(SS_HTTPRequest $request) { |
||
178 | |||
179 | /** |
||
180 | * @param string $action |
||
181 | * @return string |
||
182 | */ |
||
183 | public function Link($action = '') { |
||
186 | |||
187 | /** |
||
188 | * @param string $name |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getModel($name = '') { |
||
194 | |||
195 | /** |
||
196 | * Return data about a single deployment. |
||
197 | * @param DNDeployment $deployment |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function getDeploymentData(DNDeployment $deployment) { |
||
220 | |||
221 | } |
||
222 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.