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 | 'show', |
||
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 show(SS_HTTPRequest $request) { |
||
112 | $deployment = DNDeployment::get()->byId($request->param('ID')); |
||
113 | if (!$deployment || !$deployment->exists()) { |
||
114 | return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
||
115 | } |
||
116 | if (!$deployment->canView()) { |
||
117 | return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
||
118 | } |
||
119 | return $this->getAPIResponse(['deployment' => $this->getDeploymentData($deployment)], 200); |
||
120 | } |
||
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 for use in API response. |
||
197 | * @param DNDeployment $deployment |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function getDeploymentData(DNDeployment $deployment) { |
||
231 | |||
232 | /** |
||
233 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
234 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
235 | * user who can create the deployment but is not part of the stack roles. |
||
236 | * |
||
237 | * @param Member $member |
||
238 | * @return array |
||
239 | */ |
||
240 | protected function getStackMemberData(Member $member) { |
||
259 | |||
260 | } |
||
261 |
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.