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 Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Connection extends ClientConnection |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * State: headers |
||
| 18 | */ |
||
| 19 | const STATE_HEADERS = 1; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * State: body |
||
| 23 | */ |
||
| 24 | const STATE_BODY = 2; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array Associative array of headers |
||
| 28 | */ |
||
| 29 | public $headers = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var integer Content length |
||
| 33 | */ |
||
| 34 | public $contentLength = -1; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string Contains response body |
||
| 38 | */ |
||
| 39 | public $body = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string End of line |
||
| 43 | */ |
||
| 44 | protected $EOL = "\r\n"; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array Associative array of Cookies |
||
| 48 | */ |
||
| 49 | public $cookie = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var integer Size of current chunk |
||
| 53 | */ |
||
| 54 | protected $curChunkSize; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $curChunk; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean |
||
| 63 | */ |
||
| 64 | public $chunked = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var callback |
||
| 68 | */ |
||
| 69 | public $chunkcb; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | public $protocolError; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var integer |
||
| 78 | */ |
||
| 79 | public $responseCode = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string Last requested URL |
||
| 83 | */ |
||
| 84 | public $lastURL; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array Raw headers array |
||
| 88 | */ |
||
| 89 | public $rawHeaders = null; |
||
| 90 | |||
| 91 | public $contentType; |
||
| 92 | |||
| 93 | public $charset; |
||
| 94 | |||
| 95 | public $eofTerminated = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var \SplStack |
||
| 99 | */ |
||
| 100 | protected $requests; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | public $reqType; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Constructor |
||
| 109 | */ |
||
| 110 | protected function init() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Send request headers |
||
| 117 | * @param $type |
||
| 118 | * @param $url |
||
| 119 | * @param &$params |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | protected function sendRequestHeaders($type, $url, &$params) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Perform a HEAD request |
||
| 180 | * @param string $url |
||
| 181 | * @param array $params |
||
|
|
|||
| 182 | */ |
||
| 183 | public function head($url, $params = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Perform a GET request |
||
| 190 | * @param string $url |
||
| 191 | * @param array $params |
||
| 192 | */ |
||
| 193 | public function get($url, $params = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $headers |
||
| 200 | */ |
||
| 201 | protected function customRequestHeaders($headers) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Perform a POST request |
||
| 218 | * @param string $url |
||
| 219 | * @param array $data |
||
| 220 | * @param array $params |
||
| 221 | */ |
||
| 222 | public function post($url, $data = [], $params = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get body |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getBody() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get headers |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function getHeaders() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get header |
||
| 272 | * @param string $name Header name |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function getHeader($name) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Called when new data received |
||
| 283 | */ |
||
| 284 | public function onRead() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Called when connection finishes |
||
| 433 | */ |
||
| 434 | public function onFinish() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Called when request is finished |
||
| 454 | */ |
||
| 455 | protected function requestFinished() |
||
| 475 | } |
||
| 476 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.