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 |
||
11 | View Code Duplication | trait UrlParser |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * Convert relative to full URL |
||
15 | * |
||
16 | * @param string $url |
||
17 | * @param string $base |
||
18 | * @return string |
||
19 | * @throws ClientException |
||
20 | */ |
||
21 | protected function urlConvertToFull($url, $base) |
||
31 | |||
32 | /** |
||
33 | * URL encoder according to RFC 3986 |
||
34 | * Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings. |
||
35 | * @link http://publicmind.in/blog/url-encoding/ |
||
36 | * |
||
37 | * @param string $url |
||
38 | * @return string |
||
39 | */ |
||
40 | protected function urlEncode($url) |
||
65 | |||
66 | /** |
||
67 | * Validate URL |
||
68 | * |
||
69 | * @param string $url |
||
70 | * @return bool |
||
71 | */ |
||
72 | protected function urlValidate($url) |
||
81 | |||
82 | /** |
||
83 | * Validate host name |
||
84 | * |
||
85 | * @link http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php |
||
86 | * |
||
87 | * @param string $host |
||
88 | * @return bool |
||
89 | */ |
||
90 | protected static function urlValidateHost($host) |
||
99 | |||
100 | /** |
||
101 | * Validate URL scheme |
||
102 | * |
||
103 | * @param string $scheme |
||
104 | * @return bool |
||
105 | */ |
||
106 | protected static function urlValidateScheme($scheme) |
||
116 | |||
117 | /** |
||
118 | * Base URL |
||
119 | * |
||
120 | * @param string $url |
||
121 | * @return string |
||
122 | * @throws ClientException |
||
123 | */ |
||
124 | protected function urlBase($url) |
||
136 | } |
||
137 |
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.