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 |
||
| 20 | class Client extends Base |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * HTTP connection timeout in seconds. |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | const HTTP_TIMEOUT = 5; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Number of maximum redirections for the HTTP client. |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | const HTTP_MAX_REDIRECTS = 2; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * HTTP client user agent. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const HTTP_USER_AGENT = 'Jitamin'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Send a GET HTTP request. |
||
| 45 | * |
||
| 46 | * @param string $url |
||
| 47 | * @param string[] $headers |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public function get($url, array $headers = []) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Send a GET HTTP request and parse JSON response. |
||
| 58 | * |
||
| 59 | * @param string $url |
||
| 60 | * @param string[] $headers |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | public function getJson($url, array $headers = []) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Send a POST HTTP request encoded in JSON. |
||
| 73 | * |
||
| 74 | * @param string $url |
||
| 75 | * @param array $data |
||
| 76 | * @param string[] $headers |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function postJson($url, array $data, array $headers = []) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Send a POST HTTP request encoded in JSON (Fire and forget). |
||
| 92 | * |
||
| 93 | * @param string $url |
||
| 94 | * @param array $data |
||
| 95 | * @param string[] $headers |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function postJsonAsync($url, array $data, array $headers = []) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Send a POST HTTP request encoded in www-form-urlencoded. |
||
| 109 | * |
||
| 110 | * @param string $url |
||
| 111 | * @param array $data |
||
| 112 | * @param string[] $headers |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function postForm($url, array $data, array $headers = []) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Send a POST HTTP request encoded in www-form-urlencoded (fire and forget). |
||
| 128 | * |
||
| 129 | * @param string $url |
||
| 130 | * @param array $data |
||
| 131 | * @param string[] $headers |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function postFormAsync($url, array $data, array $headers = []) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Make the HTTP request. |
||
| 145 | * |
||
| 146 | * @param string $method |
||
| 147 | * @param string $url |
||
| 148 | * @param string $content |
||
| 149 | * @param string[] $headers |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function doRequest($method, $url, $content, array $headers) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get stream context. |
||
| 183 | * |
||
| 184 | * @param string $method |
||
| 185 | * @param string $content |
||
| 186 | * @param string[] $headers |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | private function getContext($method, $content, array $headers) |
||
| 229 | } |
||
| 230 |
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.