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 |
||
13 | class HttpHelper |
||
14 | { |
||
15 | private $curlHandle; |
||
16 | |||
17 | /** |
||
18 | * HttpHelper constructor. |
||
19 | * |
||
20 | * @param string $userAgent |
||
21 | * @param boolean $disableVerifyPeer |
||
22 | */ |
||
23 | 1 | public function __construct($userAgent, $disableVerifyPeer) |
|
35 | |||
36 | public function __destruct() |
||
40 | |||
41 | /** |
||
42 | * Fetches the content of a URL, with an optional parameter set. |
||
43 | * |
||
44 | * @param string $url The URL to fetch. |
||
45 | * @param null|array $parameters Key/value pair of GET parameters to add to the request. |
||
46 | * Null lets you handle it yourself. |
||
47 | * |
||
48 | * @param array $headers |
||
49 | * |
||
50 | * @return string |
||
51 | * @throws CurlException |
||
52 | */ |
||
53 | 1 | public function get($url, $parameters = null, $headers = array()) |
|
76 | |||
77 | /** |
||
78 | * Posts data to a URL |
||
79 | * |
||
80 | * @param string $url The URL to fetch. |
||
81 | * @param array $parameters Key/value pair of POST parameters to add to the request. |
||
82 | * @param array $headers |
||
83 | * |
||
84 | * @return string |
||
85 | * @throws CurlException |
||
86 | */ |
||
87 | public function post($url, $parameters, $headers = array()) |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getError() |
||
114 | } |
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.