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 Uri 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 Uri, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | final class Uri implements UriInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var string|null |
||
11 | */ |
||
12 | private $scheme; |
||
13 | |||
14 | /** |
||
15 | * @var string|null |
||
16 | */ |
||
17 | private $host; |
||
18 | |||
19 | /** |
||
20 | * @var int|null |
||
21 | */ |
||
22 | private $port; |
||
23 | |||
24 | const PORT_HTTP = 80; |
||
25 | const PORT_HTTPS = 443; |
||
26 | |||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | private $user; |
||
31 | |||
32 | /** |
||
33 | * @var string|null |
||
34 | */ |
||
35 | private $password; |
||
36 | |||
37 | /** |
||
38 | * @var string|null |
||
39 | */ |
||
40 | private $path; |
||
41 | |||
42 | /** |
||
43 | * @var string|null |
||
44 | */ |
||
45 | private $query; |
||
46 | |||
47 | /** |
||
48 | * @var string|null |
||
49 | */ |
||
50 | private $fragment; |
||
51 | |||
52 | /** |
||
53 | * @var UriInterface|null |
||
54 | */ |
||
55 | private $previous; |
||
56 | |||
57 | /** |
||
58 | * @param string|null $scheme |
||
59 | * @param string|null $host |
||
60 | * @param int|null $port |
||
61 | * @param string|null $user |
||
62 | * @param string|null $password |
||
63 | * @param string|null $path |
||
64 | * @param string|null $query |
||
65 | * @param string|null $fragment |
||
66 | * @param UriInterface|null $previous |
||
67 | */ |
||
68 | public function __construct( |
||
89 | |||
90 | /** |
||
91 | * @param string $uri |
||
92 | * @param UriInterface|null $previous |
||
93 | * |
||
94 | * @return UriInterface |
||
95 | * |
||
96 | * @throws \InvalidArgumentException |
||
97 | */ |
||
98 | public static function create(string $uri, UriInterface $previous = null) |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function getScheme(): string |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function getAuthority(): string |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function getUserInfo(): string |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function getHost(): string |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function getPort() |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function getPath(): string |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function getQuery(): string |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function getFragment(): string |
||
233 | |||
234 | /** |
||
235 | * @return UriInterface|null |
||
236 | */ |
||
237 | public function getPrevious() |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | View Code Duplication | public function withScheme($scheme): self |
|
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function withUserInfo($user, $password = null): self |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | View Code Duplication | public function withHost($host): self |
|
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | View Code Duplication | public function withPort($port): self |
|
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | View Code Duplication | public function withPath($path): self |
|
331 | |||
332 | /** |
||
333 | * {@inheritdoc} |
||
334 | */ |
||
335 | View Code Duplication | public function withQuery($query): self |
|
349 | |||
350 | /** |
||
351 | * {@inheritdoc} |
||
352 | */ |
||
353 | View Code Duplication | public function withFragment($fragment): self |
|
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | public function __toString(): string |
||
397 | |||
398 | /** |
||
399 | * @param string $scheme |
||
400 | * |
||
401 | * @return int|null |
||
402 | */ |
||
403 | private function getPortForScheme(string $scheme) |
||
413 | |||
414 | /** |
||
415 | * @param string $authority |
||
416 | * @param string $path |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | private function getPathForUri(string $authority, string $path): string |
||
428 | |||
429 | /** |
||
430 | * @param string $path |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | private function getPathForUriWithAuthority(string $path) |
||
438 | |||
439 | /** |
||
440 | * @param string $path |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | private function getPathForUriWithoutAuthority(string $path) |
||
455 | } |
||
456 |
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.