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 |
||
8 | class Site extends ApiResource |
||
9 | { |
||
10 | /** |
||
11 | * Resource type. |
||
12 | * |
||
13 | * @return string |
||
14 | */ |
||
15 | public static function resourceType() |
||
19 | |||
20 | /** |
||
21 | * Resource path (relative to Server URL). |
||
22 | * |
||
23 | * @return string |
||
24 | */ |
||
25 | public function resourcePath() |
||
29 | |||
30 | /** |
||
31 | * Site domain. |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function domain() |
||
39 | |||
40 | /** |
||
41 | * Site username. |
||
42 | * |
||
43 | * @return string|null |
||
44 | */ |
||
45 | public function username() |
||
49 | |||
50 | /** |
||
51 | * Project type. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function projectType() |
||
59 | |||
60 | /** |
||
61 | * Site directory. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function directory() |
||
69 | |||
70 | /** |
||
71 | * Site repository. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function repository() |
||
79 | |||
80 | /** |
||
81 | * Site repository provider. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function repositoryProvider() |
||
89 | |||
90 | /** |
||
91 | * Site repository branch. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function repositoryBranch() |
||
99 | |||
100 | /** |
||
101 | * Site repository status. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function repositoryStatus() |
||
109 | |||
110 | /** |
||
111 | * Site deployment status. |
||
112 | * |
||
113 | * @return string | null |
||
114 | */ |
||
115 | public function deploymentStatus() |
||
119 | |||
120 | /** |
||
121 | * Site allows wildcard URIs. |
||
122 | * |
||
123 | * @return boolean |
||
124 | */ |
||
125 | public function wildcards() |
||
129 | |||
130 | /** |
||
131 | * Site quick deploy enabled. |
||
132 | * |
||
133 | * @return boolean |
||
134 | */ |
||
135 | public function quickDeploy() |
||
139 | |||
140 | /** |
||
141 | * Site hipchat room. |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function hipchatRoom() |
||
149 | |||
150 | /** |
||
151 | * Site slack channel. |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function slackChannel() |
||
159 | |||
160 | /** |
||
161 | * Site app. |
||
162 | * |
||
163 | * @return string | null |
||
164 | */ |
||
165 | public function app() |
||
169 | |||
170 | /** |
||
171 | * Site app status. |
||
172 | * |
||
173 | * @return string | null |
||
174 | */ |
||
175 | public function appStatus() |
||
179 | |||
180 | /** |
||
181 | * Install new application on site. |
||
182 | * |
||
183 | * @param \Laravel\Forge\Contracts\ApplicationContract $application |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | View Code Duplication | public function install(ApplicationContract $application) |
|
195 | |||
196 | /** |
||
197 | * Install new application on site. |
||
198 | * |
||
199 | * @param \Laravel\Forge\Contracts\ApplicationContract $application |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | View Code Duplication | public function updateApplication(ApplicationContract $application) |
|
211 | |||
212 | /** |
||
213 | * Uninstall application from site. |
||
214 | * |
||
215 | * @param \Laravel\Forge\Contracts\ApplicationContract $application |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function uninstall(ApplicationContract $application) |
||
225 | |||
226 | /** |
||
227 | * Connect load balancer. |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function balance(array $serverIds) |
||
239 | } |
||
240 |
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.