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 |
||
| 14 | class BuzzDriver implements DriverInterface |
||
| 15 | { |
||
| 16 | use DriverHelper; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var \Psr\Http\Message\ResponseInterface |
||
| 20 | */ |
||
| 21 | private $response = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Host used to prepare Request URI. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $host = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Resource path used to prepare Request URI. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $resource = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * HTTP method used to prepare Request. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $method = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Request headers. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $headers = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Request message body. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $body = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get response. |
||
| 60 | * |
||
| 61 | * @return \Psr\Http\Message\ResponseInterface |
||
| 62 | * |
||
| 63 | * @throws \RuntimeException If request hasn't been send already |
||
| 64 | */ |
||
| 65 | protected function getResponse() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Prepare and get request. |
||
| 76 | * |
||
| 77 | * @return \Psr\Http\Message\RequestInterface |
||
| 78 | */ |
||
| 79 | protected function getRequest() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Send the request. |
||
| 95 | */ |
||
| 96 | public function send() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set request host. |
||
| 110 | * |
||
| 111 | * @param string $host |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function setHost($host) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Set request resource url. |
||
| 124 | * |
||
| 125 | * @param string $resource |
||
| 126 | */ |
||
| 127 | public function setResource($resource) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set request method. |
||
| 134 | * |
||
| 135 | * @param string $method Can be GET, POST, PATCH, ... |
||
| 136 | */ |
||
| 137 | public function setMethod($method) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get response status code. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | * |
||
| 152 | * @throws \RuntimeException If request hasn't been send already |
||
| 153 | */ |
||
| 154 | public function getStatusCode() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get response status message. |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | * |
||
| 164 | * @throws \RuntimeException If request hasn't been send already |
||
| 165 | */ |
||
| 166 | public function getStatusMessage() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set request header. |
||
| 173 | * |
||
| 174 | * @param string $header Header to be set |
||
| 175 | * @param mixed $value |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function setHeader($header, $value) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get all response headers. |
||
| 188 | * |
||
| 189 | * @return array Associative array with $header => $value (value can be an array if it hasn't a single value) |
||
| 190 | * |
||
| 191 | * @throws \RuntimeException If request hasn't been send already |
||
| 192 | */ |
||
| 193 | public function getHeaders() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get response body. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | * |
||
| 203 | * @throws \RuntimeException If request hasn't been send already |
||
| 204 | */ |
||
| 205 | public function getBody() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set request body. |
||
| 216 | * |
||
| 217 | * @param string $body |
||
| 218 | */ |
||
| 219 | public function setBody($body) |
||
| 223 | } |
||
| 224 |