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:
Complex classes like UrlComposer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UrlComposer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class UrlComposer |
||
8 | { |
||
9 | const SCHEME_HTTP = 'http'; |
||
10 | const PATH_SEPARATOR = '/'; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $scheme = self::SCHEME_HTTP; |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $user = ''; |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $pass = ''; |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $host = ''; |
||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $port; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $path = []; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $query = []; |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $fragment = ''; |
||
44 | |||
45 | /** |
||
46 | * Url constructor. |
||
47 | * |
||
48 | * @param string $url |
||
49 | * |
||
50 | * @throws UrlException |
||
51 | */ |
||
52 | 12 | public function __construct($url = '') |
|
61 | |||
62 | /** |
||
63 | * @param string $url |
||
64 | */ |
||
65 | 10 | protected function parseUrlString($url) |
|
113 | |||
114 | /** |
||
115 | * @param string $string |
||
116 | * |
||
117 | * @return $this |
||
118 | * @throws UrlException |
||
119 | */ |
||
120 | 3 | public function addToPath($string) |
|
142 | |||
143 | /** |
||
144 | * @param string $key |
||
145 | * @param string $value |
||
146 | * |
||
147 | * @return $this |
||
148 | * @throws UrlException |
||
149 | */ |
||
150 | 1 | public function addToQuery($key, $value) |
|
160 | |||
161 | /** |
||
162 | * @param $user |
||
163 | * @param $pass |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function setUserInfo($user, $pass) |
||
171 | |||
172 | /** |
||
173 | * Reset auth, path, query & fragment |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function reset() |
||
185 | |||
186 | /** |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function resetFragment() |
||
195 | |||
196 | /** |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function resetPath() |
||
205 | |||
206 | /** |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function resetQuery() |
||
215 | |||
216 | /** |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function resetUserInfo() |
||
226 | |||
227 | /** |
||
228 | * @return string |
||
229 | */ |
||
230 | 7 | public function getScheme() |
|
234 | |||
235 | /** |
||
236 | * @param string $scheme |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | 1 | public function setScheme($scheme) |
|
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | 7 | public function getUserInfo() |
|
267 | |||
268 | /** |
||
269 | * @return string |
||
270 | */ |
||
271 | 7 | public function getPass() |
|
275 | |||
276 | /** |
||
277 | * @param string $pass |
||
278 | * |
||
279 | * @return $this |
||
280 | */ |
||
281 | 1 | public function setPass($pass) |
|
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | 7 | public function getUser() |
|
295 | |||
296 | /** |
||
297 | * @param string $user |
||
298 | * |
||
299 | * @return $this |
||
300 | */ |
||
301 | 1 | public function setUser($user) |
|
307 | |||
308 | /** |
||
309 | * @return string |
||
310 | */ |
||
311 | 7 | public function getHost() |
|
315 | |||
316 | /** |
||
317 | * @param string $host |
||
318 | * |
||
319 | * @return $this |
||
320 | */ |
||
321 | 1 | public function setHost($host) |
|
327 | |||
328 | /** |
||
329 | * @return int |
||
330 | */ |
||
331 | 7 | public function getPort() |
|
335 | |||
336 | /** |
||
337 | * @param int $port |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | 1 | public function setPort($port) |
|
347 | |||
348 | /** |
||
349 | * @param bool $asString |
||
350 | * |
||
351 | * @return array|string |
||
352 | */ |
||
353 | 7 | public function getPath($asString = false) |
|
363 | |||
364 | /** |
||
365 | * @param array $path |
||
366 | * |
||
367 | * @return $this |
||
368 | */ |
||
369 | 1 | public function setPath(array $path) |
|
375 | |||
376 | /** |
||
377 | * @param bool $asString |
||
378 | * |
||
379 | * @return array|string |
||
380 | */ |
||
381 | 7 | public function getQuery($asString = false) |
|
391 | |||
392 | /** |
||
393 | * @param array $query Key-value array |
||
394 | * |
||
395 | * @return $this |
||
396 | */ |
||
397 | 1 | public function setQuery(array $query) |
|
403 | |||
404 | /** |
||
405 | * @return string |
||
406 | */ |
||
407 | 7 | public function getFragment() |
|
411 | |||
412 | /** |
||
413 | * @param string $fragment |
||
414 | * |
||
415 | * @return $this |
||
416 | */ |
||
417 | 1 | public function setFragment($fragment) |
|
423 | |||
424 | /** |
||
425 | * @return string |
||
426 | */ |
||
427 | 7 | protected function composeAuthority() |
|
444 | |||
445 | /** |
||
446 | * @return string |
||
447 | */ |
||
448 | 2 | public function __toString() |
|
461 | |||
462 | /** |
||
463 | * @return string |
||
464 | * @throws UrlException |
||
465 | */ |
||
466 | 7 | public function compose() |
|
491 | } |
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.