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 Http 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 Http, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Http { |
||
16 | |||
17 | const POST = "POST"; |
||
18 | const GET = "GET"; |
||
19 | |||
20 | /** |
||
21 | * Redirect to the given url. |
||
22 | * |
||
23 | * @param string $url The URL to redirect to. |
||
24 | * @param array<mixed> $params Associative array of query parameters. |
||
25 | * @param bool $session Whether to append session information. |
||
26 | */ |
||
27 | View Code Duplication | public static function http_redirect($url, $params = [], $session = false) { |
|
42 | |||
43 | /** |
||
44 | * Perform HTTP redirect with saving POST params in session. |
||
45 | * |
||
46 | * @param string $url URL redirect to. |
||
47 | * @param array<mixed> $postData List of post params to save. |
||
48 | * @param bool $serialize Serialize transmitted POST data values or not. |
||
49 | */ |
||
50 | public static function httpRedirect($url = "", $postData = [], $serialize = true) { |
||
78 | |||
79 | /** |
||
80 | * Perform HTTP redirect with saving POST params in session. |
||
81 | * |
||
82 | * @param string $url URL redirect to. |
||
83 | * @param array<mixed> $postData List of post params to save. |
||
84 | * @param bool $serialize Serialize transmitted POST data values or not. |
||
85 | */ |
||
86 | public static function redirect($url = "", $postData = [], $serialize = true) { |
||
89 | |||
90 | /** |
||
91 | * Returns clients IP-address. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public static function getIP() { |
||
104 | |||
105 | /** |
||
106 | * Returns HTTP referrer. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public static function getReferrer() { |
||
113 | |||
114 | /** |
||
115 | * Gets the address that the provided URL redirects to, |
||
116 | * or FALSE if there's no redirect. |
||
117 | * |
||
118 | * @param string $url URL. |
||
119 | * |
||
120 | * @return mixed String with redirect URL or FALSE if no redirect. |
||
121 | * @throws HttpException |
||
122 | */ |
||
123 | public static function getRedirectUrl($url) { |
||
161 | |||
162 | /** |
||
163 | * Follows and collects all redirects, in order, for the given URL. |
||
164 | * |
||
165 | * @param string $url |
||
166 | * @return array |
||
167 | */ |
||
168 | public static function getAllRedirects($url) { |
||
180 | |||
181 | /** |
||
182 | * Gets the address that the URL ultimately leads to. |
||
183 | * Returns $url itself if it isn't a redirect. |
||
184 | * |
||
185 | * @param string $url |
||
186 | * @return string |
||
187 | */ |
||
188 | public static function getFinalUrl($url) { |
||
196 | |||
197 | /** |
||
198 | * Executes CURL async request. |
||
199 | * |
||
200 | * @param string $url URL. |
||
201 | * @param array $params List of request params. |
||
202 | * @param string $type Type of the request (GET, POST, ...). |
||
203 | * @param int $timeout Timeout in seconds. |
||
204 | * |
||
205 | * @return type |
||
206 | */ |
||
207 | public static function curlRequestAsync($url, $params, $type = self::POST, $timeout = 30) { |
||
271 | |||
272 | /** |
||
273 | * Validates URL existence with cURL request. |
||
274 | * |
||
275 | * @param string $url URL. |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public static function urlExists($url) { |
||
290 | |||
291 | /** |
||
292 | * Force HTTP status code. |
||
293 | * |
||
294 | * @param int $code Status code. |
||
295 | * @param string $path Include path if needed. |
||
296 | * |
||
297 | * @throws HttpException If invalid HTTP status provided. |
||
298 | */ |
||
299 | public static function forceHttpStatus($code, $path = null) { |
||
313 | |||
314 | /** |
||
315 | * Magic methods: |
||
316 | * force404($path = null) - force HTTP status codes. |
||
317 | * |
||
318 | * @param string $name The name of the method being called. |
||
319 | * @param type $arguments Enumerated array containing the parameters passed |
||
320 | * to the method. |
||
321 | * @return mixed |
||
322 | * |
||
323 | * @throws HttpException If invalid method name provided. |
||
324 | */ |
||
325 | public static function __callStatic($name, $arguments) { |
||
334 | |||
335 | } |
||
336 | |||
341 |
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.