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 |
||
14 | class Request |
||
15 | { |
||
16 | /** |
||
17 | * Request constructor. |
||
18 | * |
||
19 | * @param array $languageList |
||
20 | * @param array $packageList |
||
21 | * @param null $url |
||
22 | */ |
||
23 | public function __construct(array $languageList = [], array $packageList = [], $url = null) |
||
88 | |||
89 | /** |
||
90 | * Path without an optional query. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function url() |
||
98 | |||
99 | /** |
||
100 | * Create absolute url path or full uri from route. |
||
101 | * |
||
102 | * @param string $route |
||
103 | * @param bool $domain |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function makeUrl($route = '', $domain = false) |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Request method. |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function method() |
||
172 | |||
173 | /** |
||
174 | * A default language code. |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function languageDefault() |
||
182 | |||
183 | /** |
||
184 | * A language code. |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function languageCode() |
||
192 | |||
193 | /** |
||
194 | * A language name associated with code. |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function languageName() |
||
202 | |||
203 | /** |
||
204 | * Name of an app package. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function package() |
||
212 | |||
213 | /** |
||
214 | * Custom routing class or false. |
||
215 | * |
||
216 | * @return string|bool |
||
217 | */ |
||
218 | public function packageRoute() |
||
222 | |||
223 | /** |
||
224 | * URL path without front controller, language code, package name and an optional query. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function route() |
||
232 | |||
233 | private $method; |
||
234 | |||
235 | private $url; |
||
236 | private $languageDefault; |
||
237 | private $languageCode; |
||
238 | private $languageName; |
||
239 | private $package; |
||
240 | private $packageRoute; |
||
241 | private $route; |
||
242 | } |
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.