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 Google_Http_Request 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 Google_Http_Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Google_Http_Request |
||
| 31 | { |
||
| 32 | const GZIP_UA = " (gzip)"; |
||
| 33 | |||
| 34 | private $batchHeaders = array( |
||
| 35 | 'Content-Type' => 'application/http', |
||
| 36 | 'Content-Transfer-Encoding' => 'binary', |
||
| 37 | 'MIME-Version' => '1.0', |
||
| 38 | ); |
||
| 39 | |||
| 40 | protected $queryParams; |
||
| 41 | protected $requestMethod; |
||
| 42 | protected $requestHeaders; |
||
| 43 | protected $baseComponent = null; |
||
| 44 | protected $path; |
||
| 45 | protected $postBody; |
||
| 46 | protected $userAgent; |
||
| 47 | protected $canGzip = null; |
||
| 48 | |||
| 49 | protected $responseHttpCode; |
||
| 50 | protected $responseHeaders; |
||
| 51 | protected $responseBody; |
||
| 52 | |||
| 53 | protected $expectedClass; |
||
| 54 | protected $expectedRaw = false; |
||
| 55 | |||
| 56 | public $accessKey; |
||
| 57 | |||
| 58 | public function __construct( |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Misc function that returns the base url component of the $url |
||
| 72 | * used by the OAuth signing class to calculate the base string |
||
| 73 | * @return string The base url component of the $url. |
||
| 74 | */ |
||
| 75 | public function getBaseComponent() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the base URL that path and query parameters will be added to. |
||
| 82 | * @param $baseComponent string |
||
| 83 | */ |
||
| 84 | public function setBaseComponent($baseComponent) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Enable support for gzipped responses with this request. |
||
| 91 | */ |
||
| 92 | public function enableGzip() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Disable support for gzip responses with this request. |
||
| 101 | */ |
||
| 102 | public function disableGzip() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Can this request accept a gzip response? |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function canGzip() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Misc function that returns an array of the query parameters of the current |
||
| 125 | * url used by the OAuth signing class to calculate the signature |
||
| 126 | * @return array Query parameters in the query string. |
||
| 127 | */ |
||
| 128 | public function getQueryParams() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set a new query parameter. |
||
| 135 | * @param $key - string to set, does not need to be URL encoded |
||
| 136 | * @param $value - string to set, does not need to be URL encoded |
||
| 137 | */ |
||
| 138 | public function setQueryParam($key, $value) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string HTTP Response Code. |
||
| 145 | */ |
||
| 146 | public function getResponseHttpCode() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param int $responseHttpCode HTTP Response Code. |
||
| 153 | */ |
||
| 154 | public function setResponseHttpCode($responseHttpCode) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return $responseHeaders (array) HTTP Response Headers. |
||
|
|
|||
| 161 | */ |
||
| 162 | public function getResponseHeaders() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string HTTP Response Body |
||
| 169 | */ |
||
| 170 | public function getResponseBody() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set the class the response to this request should expect. |
||
| 177 | * |
||
| 178 | * @param $class string the class name |
||
| 179 | */ |
||
| 180 | public function setExpectedClass($class) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Retrieve the expected class the response should expect. |
||
| 187 | * @return string class name |
||
| 188 | */ |
||
| 189 | public function getExpectedClass() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Enable expected raw response |
||
| 196 | */ |
||
| 197 | public function enableExpectedRaw() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Disable expected raw response |
||
| 204 | */ |
||
| 205 | public function disableExpectedRaw() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Expected raw response or not. |
||
| 212 | * @return boolean expected raw response |
||
| 213 | */ |
||
| 214 | public function getExpectedRaw() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param array $headers The HTTP response headers |
||
| 221 | * to be normalized. |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function setResponseHeaders($headers) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $key |
||
| 235 | * @return array|boolean Returns the requested HTTP header or |
||
| 236 | * false if unavailable. |
||
| 237 | */ |
||
| 238 | public function getResponseHeader($key) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $responseBody The HTTP response body. |
||
| 247 | */ |
||
| 248 | public function setResponseBody($responseBody) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string $url The request URL. |
||
| 255 | */ |
||
| 256 | public function getUrl() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string $method HTTP Request Method. |
||
| 266 | */ |
||
| 267 | public function getRequestMethod() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return array $headers HTTP Request Headers. |
||
| 274 | */ |
||
| 275 | public function getRequestHeaders() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $key |
||
| 282 | * @return array|boolean Returns the requested HTTP header or |
||
| 283 | * false if unavailable. |
||
| 284 | */ |
||
| 285 | public function getRequestHeader($key) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return string $postBody HTTP Request Body. |
||
| 294 | */ |
||
| 295 | public function getPostBody() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $url the url to set |
||
| 302 | */ |
||
| 303 | public function setUrl($url) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $method Set he HTTP Method and normalize |
||
| 329 | * it to upper-case, as required by HTTP. |
||
| 330 | * |
||
| 331 | */ |
||
| 332 | public function setRequestMethod($method) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param array $headers The HTTP request headers |
||
| 339 | * to be set and normalized. |
||
| 340 | */ |
||
| 341 | View Code Duplication | public function setRequestHeaders($headers) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $postBody the postBody to set |
||
| 352 | */ |
||
| 353 | public function setPostBody($postBody) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set the User-Agent Header. |
||
| 360 | * @param string $userAgent The User-Agent. |
||
| 361 | */ |
||
| 362 | public function setUserAgent($userAgent) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return string The User-Agent. |
||
| 372 | */ |
||
| 373 | public function getUserAgent() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns a cache key depending on if this was an OAuth signed request |
||
| 380 | * in which case it will use the non-signed url and access key to make this |
||
| 381 | * cache key unique per authenticated user, else use the plain request url |
||
| 382 | * @return string The md5 hash of the request cache key. |
||
| 383 | */ |
||
| 384 | public function getCacheKey() |
||
| 398 | |||
| 399 | public function getParsedCacheControl() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $id |
||
| 413 | * @return string A string representation of the HTTP Request. |
||
| 414 | */ |
||
| 415 | public function toBatchString($id) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Our own version of parse_str that allows for multiple variables |
||
| 444 | * with the same name. |
||
| 445 | * @param $string - the query string to parse |
||
| 446 | */ |
||
| 447 | private function parseQuery($string) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * A version of build query that allows for multiple |
||
| 468 | * duplicate keys. |
||
| 469 | * @param $parts array of key value pairs |
||
| 470 | */ |
||
| 471 | private function buildQuery($parts) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * If we're POSTing and have no body to send, we can send the query |
||
| 488 | * parameters in there, which avoids length issues with longer query |
||
| 489 | * params. |
||
| 490 | */ |
||
| 491 | public function maybeMoveParametersToBody() |
||
| 504 | } |
||
| 505 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.