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 | class UriParser |
||
12 | { |
||
13 | /** |
||
14 | * Scheme white-list |
||
15 | * @var string[] |
||
16 | */ |
||
17 | protected $schemes = [ |
||
18 | 'http', |
||
19 | 'https', |
||
20 | 'ftp', |
||
21 | 'ftps', |
||
22 | 'sftp', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * URI |
||
27 | * @var string |
||
28 | */ |
||
29 | private $uri; |
||
30 | |||
31 | /** |
||
32 | * UriParser constructor. |
||
33 | * |
||
34 | * @param $uri |
||
35 | */ |
||
36 | public function __construct($uri) |
||
40 | |||
41 | /** |
||
42 | * Convert relative to full |
||
43 | * |
||
44 | * @param string $fallbackBase |
||
45 | * @return string |
||
46 | */ |
||
47 | public function convertToFull($fallbackBase) |
||
59 | |||
60 | /** |
||
61 | * URI encoder according to RFC 3986 |
||
62 | * Returns a string containing the encoded URI with disallowed characters converted to their percentage encodings. |
||
63 | * @link http://publicmind.in/blog/url-encoding/ |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | public function encode() |
||
95 | |||
96 | /** |
||
97 | * Base uri to lowercase |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | private function baseToLowercase() |
||
109 | |||
110 | /** |
||
111 | * Validate |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function validate() |
||
131 | |||
132 | /** |
||
133 | * Validate IPv4 or IPv6 |
||
134 | * |
||
135 | * @param string|null $ipAddress |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function validateIP($ipAddress = null) |
||
149 | |||
150 | /** |
||
151 | * Validate host name |
||
152 | * |
||
153 | * @link http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php |
||
154 | * |
||
155 | * @param string|null $host |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function validateHost($host = null) |
||
171 | |||
172 | /** |
||
173 | * Validate scheme |
||
174 | * |
||
175 | * @param string|null $scheme |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function validateScheme($scheme = null) |
||
186 | |||
187 | /** |
||
188 | * Base |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | public function base() |
||
204 | |||
205 | /** |
||
206 | * Strip fragment |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function stripFragment() |
||
214 | } |
||
215 |
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.