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 |
||
11 | class Descriptor |
||
12 | { |
||
13 | const SCOPE_NONE = "none"; |
||
14 | const SCOPE_READ = "read"; |
||
15 | const SCOPE_WRITE = "write"; |
||
16 | const SCOPE_DELETE = "delete"; |
||
17 | const SCOPE_ADMIN = "admin"; |
||
18 | const SCOPE_ACT_AS_USER = "act_a_user"; |
||
19 | |||
20 | const SCOPE_JIRA_PROJECT_ADMIN = "project_admin"; |
||
21 | const SCOPE_CONFLUENCE_SPACE_ADMIN = "space_admin"; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $descriptor = [ |
||
27 | 'authentication' => [ |
||
28 | 'type' => 'jwt' |
||
29 | ], |
||
30 | 'modules' => [], |
||
31 | 'scopes' => [] |
||
32 | ]; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Descriptor constructor. |
||
37 | * |
||
38 | * @param string $baseUrl |
||
39 | * @param string $key |
||
40 | */ |
||
41 | public function __construct($baseUrl, $key) |
||
46 | |||
47 | /** |
||
48 | * @param string $name |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setName($name) |
||
57 | |||
58 | /** |
||
59 | * @param string $description |
||
60 | * |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function setDescription($description) |
||
68 | |||
69 | /** |
||
70 | * @param string $scope |
||
71 | * |
||
72 | * @return $this |
||
73 | * @throws \Exception |
||
74 | */ |
||
75 | View Code Duplication | public function addScope($scope) |
|
86 | |||
87 | /** |
||
88 | * @param string $scope |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | private function validateScopeValue($scope) |
||
107 | |||
108 | /** |
||
109 | * @param $scope |
||
110 | * |
||
111 | * @return mixed |
||
112 | */ |
||
113 | private function getScopeKey($scope) |
||
118 | |||
119 | /** |
||
120 | * @param string $scope |
||
121 | * |
||
122 | * @return $this |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | View Code Duplication | public function removeScope($scope) |
|
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getArray() |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getJson() |
||
153 | |||
154 | /** |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function enableLicensing() |
||
162 | |||
163 | /** |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function disableLicensing() |
||
171 | |||
172 | /** |
||
173 | * @param int $version |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setApiVersion($version) |
||
182 | |||
183 | /** |
||
184 | * @param string $name |
||
185 | * @param string $url |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function addLink($name, $url) |
||
194 | |||
195 | /** |
||
196 | * @param string $name |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function removeLink($name) |
||
205 | |||
206 | /** |
||
207 | * @param string $name |
||
208 | * @param string $url |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function setVendor($name, $url = '') |
||
220 | |||
221 | /** |
||
222 | * @param string $installed |
||
223 | * @param string $enabled |
||
224 | * @param string $disabled |
||
225 | * @param string $uninstalled |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setLifecycleWebhooks($installed, $enabled, $disabled = '', $uninstalled = '') |
||
239 | |||
240 | /** |
||
241 | * @param string $name |
||
242 | * @param array $description |
||
243 | * |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function addModule($name, array $description) |
||
252 | |||
253 | /** |
||
254 | * @param string $name |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function removeModule($name) |
||
263 | } |
||
264 |
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.