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 |
||
| 17 | class RestClient |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * httpClient |
||
| 21 | * |
||
| 22 | * @var ClientInterface |
||
| 23 | * @access private |
||
| 24 | */ |
||
| 25 | private $httpClient; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * baseUrl |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | * @access private |
||
| 32 | */ |
||
| 33 | private $baseUrl; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * logHistory |
||
| 37 | * |
||
| 38 | * @var boolean |
||
| 39 | * @access private |
||
| 40 | */ |
||
| 41 | private $logHistory; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * requestHistory |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | * @access private |
||
| 48 | */ |
||
| 49 | private $requestHistory; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * currentRequest |
||
| 53 | * |
||
| 54 | * @var Request |
||
| 55 | * @access private |
||
| 56 | */ |
||
| 57 | private $currentRequest; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param ClientInterface $httpClient |
||
| 61 | * @param string|null $baseUrl |
||
| 62 | */ |
||
| 63 | public function __construct(ClientInterface $httpClient, $baseUrl = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function isHistoryLogged() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * setCurrentRequest |
||
| 81 | * |
||
| 82 | * @param Request $currentRequest |
||
| 83 | * @access public |
||
| 84 | * @return RestClient |
||
| 85 | */ |
||
| 86 | public function setCurrentRequest(Request $currentRequest) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * setLogHistory |
||
| 95 | * |
||
| 96 | * @param boolean $logHistory |
||
| 97 | * @access public |
||
| 98 | * @return RestClient |
||
| 99 | */ |
||
| 100 | public function setLogHistory($logHistory) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function getRequestHistory() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * get a path |
||
| 117 | * |
||
| 118 | * @param string $path |
||
| 119 | * @param array $parameters |
||
| 120 | * @return array|ResponseInterface|null |
||
| 121 | * @throws RestException |
||
| 122 | */ |
||
| 123 | public function get($path, $parameters = []) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * delete |
||
| 140 | * |
||
| 141 | * @param string $path |
||
| 142 | * @access public |
||
| 143 | * @return void |
||
| 144 | * @throws RestException |
||
| 145 | */ |
||
| 146 | public function delete($path) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $path |
||
| 159 | * @param mixed $data |
||
| 160 | * @param array $parameters |
||
| 161 | * @return array|ResponseInterface |
||
| 162 | * @throws RestClientException |
||
| 163 | * @throws RestException |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function post($path, $data, $parameters = []) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $path |
||
| 179 | * @param mixed $data |
||
| 180 | * @param array $parameters |
||
| 181 | * @return array|ResponseInterface |
||
| 182 | * @throws RestClientException |
||
| 183 | * @throws RestException |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function put($path, $data, $parameters = []) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Merge default parameters. |
||
| 200 | * |
||
| 201 | * @param array $parameters |
||
| 202 | * @access protected |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function mergeDefaultParameters(array $parameters) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * getCurrentRequest |
||
| 224 | * |
||
| 225 | * @access private |
||
| 226 | * @return Request |
||
| 227 | */ |
||
| 228 | protected function getCurrentRequest() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Executes request. |
||
| 239 | * |
||
| 240 | * @param string $method |
||
| 241 | * @param string $url |
||
| 242 | * @param array $parameters |
||
| 243 | * @access private |
||
| 244 | * @return ResponseInterface|array |
||
| 245 | * @throws TransferException |
||
| 246 | */ |
||
| 247 | private function executeRequest($method, $url, $parameters = []) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Logs request. |
||
| 287 | * |
||
| 288 | * @param float|null $startTime |
||
| 289 | * @param string $method |
||
| 290 | * @param string $url |
||
| 291 | * @param array $parameters |
||
| 292 | * @param ResponseInterface|null $response |
||
| 293 | * @access private |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | private function logRequest($startTime, $method, $url, $parameters, ResponseInterface $response = null) |
||
| 312 | } |
||
| 313 |