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 |
||
4 | class DeployPlanDispatcher extends Dispatcher { |
||
5 | |||
6 | const ACTION_PLAN = 'plan'; |
||
7 | |||
8 | /** |
||
9 | * @var array |
||
10 | */ |
||
11 | private static $action_types = [ |
||
12 | self::ACTION_PLAN |
||
13 | ]; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | public static $allowed_actions = [ |
||
19 | 'gitupdate', |
||
20 | 'gitrefs', |
||
21 | 'deploysummary', |
||
22 | 'deployhistorydata' |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var \DNProject |
||
27 | */ |
||
28 | protected $project = null; |
||
29 | |||
30 | /** |
||
31 | * @var \DNEnvironment |
||
32 | */ |
||
33 | protected $environment = null; |
||
34 | |||
35 | public function init() { |
||
36 | parent::init(); |
||
37 | |||
38 | $this->project = $this->getCurrentProject(); |
||
39 | |||
40 | if(!$this->project) { |
||
41 | return $this->project404Response(); |
||
42 | } |
||
43 | |||
44 | // Performs canView permission check by limiting visible projects |
||
45 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
46 | if(!$this->environment) { |
||
47 | return $this->environment404Response(); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | */ |
||
54 | public function Link() { |
||
55 | return \Controller::join_links($this->environment->Link(), self::ACTION_PLAN); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * |
||
60 | * @param \SS_HTTPRequest $request |
||
61 | * |
||
62 | * @return \HTMLText|\SS_HTTPResponse |
||
63 | */ |
||
64 | public function index(\SS_HTTPRequest $request) { |
||
65 | $this->setCurrentActionType(self::ACTION_PLAN); |
||
66 | return $this->customise([ |
||
67 | 'Environment' => $this->environment |
||
68 | ])->renderWith(['Plan', 'DNRoot']); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param SS_HTTPRequest $request |
||
73 | * @return SS_HTTPResponse |
||
74 | */ |
||
75 | public function gitupdate(SS_HTTPRequest $request) { |
||
76 | switch($request->httpMethod()) { |
||
77 | case 'POST': |
||
78 | return $this->createFetch(); |
||
79 | case 'GET': |
||
80 | return $this->getFetch($this->getRequest()->param('ID')); |
||
81 | default: |
||
82 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST or GET/{id}'], 405); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param SS_HTTPRequest $request |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function gitrefs(\SS_HTTPRequest $request) { |
||
126 | |||
127 | /** |
||
128 | * Generate the data structure used by the frontend component. |
||
129 | * |
||
130 | * @param string $name of the component |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function getModel($name) { |
||
139 | |||
140 | /** |
||
141 | * @param int $ID |
||
142 | * @return SS_HTTPResponse |
||
143 | */ |
||
144 | protected function getFetch($ID) { |
||
157 | |||
158 | /** |
||
159 | * @return SS_HTTPResponse |
||
160 | */ |
||
161 | View Code Duplication | protected function createFetch() { |
|
178 | |||
179 | /** |
||
180 | * @return SS_HTTPResponse |
||
181 | */ |
||
182 | public function deployhistorydata(SS_HTTPRequest $request) { |
||
198 | |||
199 | /** |
||
200 | * @param SS_HTTPRequest $request |
||
201 | * |
||
202 | * @return SS_HTTPResponse |
||
203 | */ |
||
204 | public function deploysummary(SS_HTTPRequest $request) { |
||
240 | |||
241 | /** |
||
242 | * @param $project |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | protected function getGitBranches($project) { |
||
256 | |||
257 | /** |
||
258 | * @param $project |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | protected function getGitTags($project) { |
||
272 | |||
273 | /** |
||
274 | * @param $project |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function getGitPrevDeploys($project) { |
||
305 | |||
306 | /** |
||
307 | * Return a simple response with a message |
||
308 | * |
||
309 | * @param array $output |
||
310 | * @param int $statusCode |
||
311 | * @return SS_HTTPResponse |
||
312 | */ |
||
313 | protected function getAPIResponse($output, $statusCode) { |
||
322 | |||
323 | /** |
||
324 | * @param ArrayData $interface |
||
325 | * @param $changes |
||
326 | * |
||
327 | * @return bool |
||
328 | * |
||
329 | */ |
||
330 | protected function canCompareCodeVersions(\ArrayData $interface, $changes) { |
||
349 | } |
||
350 |
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.