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 | 'gitrefs', |
||
20 | ]; |
||
21 | |||
22 | /** |
||
23 | * @var \DNProject |
||
24 | */ |
||
25 | protected $project = null; |
||
26 | |||
27 | /** |
||
28 | * @var \DNEnvironment |
||
29 | */ |
||
30 | protected $environment = null; |
||
31 | |||
32 | public function init() { |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | public function Link() { |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | * @param \SS_HTTPRequest $request |
||
58 | * |
||
59 | * @return \HTMLText|\SS_HTTPResponse |
||
60 | */ |
||
61 | public function index(\SS_HTTPRequest $request) { |
||
67 | |||
68 | /** |
||
69 | * @param SS_HTTPRequest $request |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function gitrefs(\SS_HTTPRequest $request) { |
||
74 | |||
75 | $refs = []; |
||
76 | $order = 0; |
||
77 | $refs[] = [ |
||
78 | 'id' => ++$order, |
||
79 | 'label' => "Branch version", |
||
80 | "description" => "Deploy the latest version of a branch", |
||
81 | "list" => $this->getGitBranches($this->project) |
||
82 | ]; |
||
83 | |||
84 | $refs[] = [ |
||
85 | 'id' => ++$order, |
||
86 | 'label' => "Tag version", |
||
87 | "description" => "Deploy a tagged release", |
||
88 | "list" => $this->getGitTags($this->project) |
||
89 | ]; |
||
90 | |||
91 | // @todo: the original was a tree that was keyed by environment, the |
||
92 | // front-end dropdown needs to be changed to support that. brrrr. |
||
93 | $prevDeploys = []; |
||
94 | foreach($this->getGitPrevDeploys($this->project) as $env) { |
||
95 | foreach($env as $deploy) { |
||
96 | $prevDeploys[] = $deploy; |
||
97 | } |
||
98 | } |
||
99 | $refs[] = [ |
||
100 | 'id' => ++$order, |
||
101 | 'label' => "Redeploy a release that was previously deployed (to any environment", |
||
102 | "description" => "Deploy a previous release", |
||
103 | "list" => $prevDeploys |
||
104 | ]; |
||
105 | |||
106 | $body = json_encode($refs, JSON_PRETTY_PRINT); |
||
107 | $this->getResponse()->addHeader('Content-Type', 'application/json'); |
||
108 | $this->getResponse()->setBody($body); |
||
109 | return $body; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Generate the data structure used by the frontend component. |
||
114 | * |
||
115 | * @param string $name of the component |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getModel($name) { |
||
124 | |||
125 | /** |
||
126 | * @param $project |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | protected function getGitBranches($project) { |
||
140 | |||
141 | /** |
||
142 | * @param $project |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | protected function getGitTags($project) { |
||
156 | |||
157 | /** |
||
158 | * @param $project |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function getGitPrevDeploys($project) { |
||
189 | } |
||
190 |
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.