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 |
||
17 | class Uri implements UriInterface |
||
18 | { |
||
19 | /** |
||
20 | * Unreserved characters used in paths, query strings, and fragments. |
||
21 | * |
||
22 | * @const string |
||
23 | */ |
||
24 | const CHAR_UNRESERVED = 'A-Za-z0-9\-\._~'; |
||
25 | |||
26 | /** |
||
27 | * Sub-delimiters used in query strings and fragments. |
||
28 | * |
||
29 | * @const string |
||
30 | */ |
||
31 | const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
||
32 | |||
33 | /** |
||
34 | * Pattern for path filtering. |
||
35 | * |
||
36 | * @const string |
||
37 | */ |
||
38 | const PATH_FILTER_PATTERN = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS |
||
39 | . '%:@\/]+|%(?![A-Fa-f0-9]{2}))/'; |
||
40 | |||
41 | /** |
||
42 | * Pattern for query or fragment filtering. |
||
43 | * |
||
44 | * @const string |
||
45 | */ |
||
46 | const QUERY_OR_FRAGMENT_FILTER_PATTERN = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS |
||
47 | . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/'; |
||
48 | |||
49 | /** |
||
50 | * The scheme. |
||
51 | * |
||
52 | * @var null|string |
||
53 | */ |
||
54 | protected $scheme; |
||
55 | |||
56 | /** |
||
57 | * The user info. |
||
58 | * |
||
59 | * @var null|string |
||
60 | */ |
||
61 | protected $userInfo; |
||
62 | |||
63 | /** |
||
64 | * The host. |
||
65 | * |
||
66 | * @var null|string |
||
67 | */ |
||
68 | protected $host; |
||
69 | |||
70 | /** |
||
71 | * The port. |
||
72 | * |
||
73 | * @var null|int |
||
74 | */ |
||
75 | protected $port; |
||
76 | |||
77 | /** |
||
78 | * The path. |
||
79 | * |
||
80 | * @var null|string |
||
81 | */ |
||
82 | protected $path; |
||
83 | |||
84 | /** |
||
85 | * The query. |
||
86 | * |
||
87 | * @var null|string |
||
88 | */ |
||
89 | protected $query; |
||
90 | |||
91 | /** |
||
92 | * The fragment. |
||
93 | * |
||
94 | * @var null|string |
||
95 | */ |
||
96 | protected $fragment; |
||
97 | |||
98 | /** |
||
99 | * Create URI object from array. |
||
100 | * |
||
101 | * @param array $array |
||
102 | * |
||
103 | * @return static |
||
104 | * @throws \InvalidArgumentException |
||
105 | */ |
||
106 | 8 | public static function fromArray(array $array = []) |
|
145 | |||
146 | /** |
||
147 | * Create URI object from string. |
||
148 | * |
||
149 | * @param string $string |
||
150 | * |
||
151 | * @return static |
||
152 | * @throws \InvalidArgumentException |
||
153 | */ |
||
154 | 10 | public static function fromString($string) |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 8 | public function getScheme() |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 8 | public function getAuthority() |
|
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | 8 | public function getUserInfo() |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 8 | public function getHost() |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | 8 | public function getPort() |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 8 | public function getPath() |
|
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 8 | public function getQuery() |
|
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | 8 | public function getFragment() |
|
275 | |||
276 | /** |
||
277 | * Filter path value. |
||
278 | * |
||
279 | * @param string $value |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 6 | protected function filterPathValue($value) |
|
287 | |||
288 | /** |
||
289 | * Filter query or fragment value. |
||
290 | * |
||
291 | * @param string $value |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | 2 | protected function filterQueryOrFragmentValue($value) |
|
299 | |||
300 | /** |
||
301 | * URL encode a the first match returned by a regex. |
||
302 | * |
||
303 | * @param array $matches |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 2 | protected function urlEncodeFirstMatch(array $matches) |
|
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | 6 | public function withScheme($scheme) |
|
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | 2 | public function withUserInfo($user, $password = null) |
|
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | 4 | View Code Duplication | public function withHost($host) |
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | 4 | public function withPort($port) |
|
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | 6 | View Code Duplication | public function withPath($path) |
399 | |||
400 | /** |
||
401 | * {@inheritdoc} |
||
402 | */ |
||
403 | 2 | public function withQuery($query) |
|
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | 2 | View Code Duplication | public function withFragment($fragment) |
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | 8 | public function __toString() |
|
437 | |||
438 | /** |
||
439 | * Convert URI into a string representation. |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 8 | public function toString() |
|
474 | } |
||
475 |
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.