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 |
||
8 | class Uri implements UriInterface |
||
9 | { |
||
10 | |||
11 | |||
12 | /** |
||
13 | * Uri scheme (without "://" suffix) |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $scheme = ''; |
||
18 | |||
19 | /** |
||
20 | * Uri user |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $user = ''; |
||
25 | |||
26 | /** |
||
27 | * Uri password |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $password = ''; |
||
32 | |||
33 | /** |
||
34 | * Uri host |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $host = ''; |
||
39 | |||
40 | /** |
||
41 | * Uri port number |
||
42 | * |
||
43 | * @var null|int |
||
44 | */ |
||
45 | protected $port; |
||
46 | |||
47 | /** |
||
48 | * Uri base path |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $basePath = ''; |
||
53 | |||
54 | /** |
||
55 | * Uri path |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $path = ''; |
||
60 | |||
61 | /** |
||
62 | * Uri query string (without "?" prefix) |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $query = ''; |
||
67 | |||
68 | /** |
||
69 | * Uri fragment string (without "#" prefix) |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $fragment = ''; |
||
74 | |||
75 | /** |
||
76 | * Instance new Uri. |
||
77 | * |
||
78 | * @param string $scheme Uri scheme. |
||
79 | * @param string $host Uri host. |
||
80 | * @param int $port Uri port number. |
||
81 | * @param string $path Uri path. |
||
82 | * @param string $query Uri query string. |
||
83 | * @param string $fragment Uri fragment. |
||
84 | * @param string $user Uri user. |
||
85 | * @param string $password Uri password. |
||
86 | */ |
||
87 | public function __construct( |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function getScheme() |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getAuthority() |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function getUserInfo() |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function getHost() |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getPort() |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getPath() |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function getQuery() |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function getFragment() |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function withScheme($scheme) |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function withUserInfo($user, $password = null) |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function withHost($host) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function withPort($port) |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function withPath($path) |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | View Code Duplication | public function withQuery($query) |
|
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | View Code Duplication | public function withFragment($fragment) |
|
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function __toString() |
||
288 | |||
289 | /* |
||
290 | END OF UriInterface Implementation |
||
291 | */ |
||
292 | |||
293 | /** |
||
294 | * filter scheme given to only allow certain scheme, no file:// or ftp:// or other scheme because its http message uri interface |
||
295 | * |
||
296 | * @access protected |
||
297 | * @param string $scheme |
||
298 | * @return string $scheme |
||
299 | * @throws InvalidArgumentException if not corret scheme is present |
||
300 | */ |
||
301 | protected function filterScheme(string $scheme) |
||
316 | |||
317 | |||
318 | /** |
||
319 | * Filter allowable port to minimize risk |
||
320 | * |
||
321 | * @access protected |
||
322 | * @param mixed $port |
||
323 | * @return mixed $port |
||
324 | * @throws InvalidArgumentException for incorrect port assigned |
||
325 | */ |
||
326 | protected function filterPort($port) |
||
334 | |||
335 | /** |
||
336 | * Path allowed chars filter, no weird path on uri yes?. |
||
337 | * |
||
338 | * @access protected |
||
339 | * @param string $path |
||
340 | * @return rawurlencode of cleared path |
||
341 | */ |
||
342 | protected function filterPath($path) |
||
352 | |||
353 | /** |
||
354 | * replace query to clear not allowed chars |
||
355 | * |
||
356 | * @access protected |
||
357 | * @param string $query |
||
358 | * @return rawurlencode of replaced query |
||
359 | */ |
||
360 | protected function filterQuery($query) |
||
370 | |||
371 | /** |
||
372 | * cek if current uri scheme use standard port |
||
373 | * |
||
374 | * @access protected |
||
375 | * @return boolean |
||
376 | */ |
||
377 | protected function hasStandardPort() |
||
381 | |||
382 | |||
383 | /** |
||
384 | * get BasePath property. |
||
385 | * |
||
386 | * @access public |
||
387 | * @return string basePath |
||
388 | */ |
||
389 | public function getBasePath() |
||
393 | |||
394 | /** |
||
395 | * Set BasePath Function to rewrite request. |
||
396 | * |
||
397 | * @access public |
||
398 | * @param string $basePath |
||
399 | * @return void |
||
400 | */ |
||
401 | public function withBasePath(string $basePath) |
||
405 | |||
406 | |||
407 | /** |
||
408 | * get Base Url |
||
409 | * |
||
410 | * @access public |
||
411 | * @return void |
||
412 | */ |
||
413 | public function getBaseUrl() |
||
427 | |||
428 | /** |
||
429 | * Create uri Instance from header $_SERVER. |
||
430 | * |
||
431 | * @access public |
||
432 | * @static |
||
433 | * @return Resilient\Http\Uri |
||
434 | */ |
||
435 | public static function createFromServer($serv) |
||
481 | |||
482 | |||
483 | /** |
||
484 | * Create Uri Instance from string http://www.example.com/url/path.html |
||
485 | * |
||
486 | * @access public |
||
487 | * @static |
||
488 | * @param string $uri |
||
489 | * @return Resilient\Http\Uri |
||
490 | */ |
||
491 | public static function createFromString(string $uri) |
||
505 | } |
||
506 |
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.