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 |
||
16 | class Connection extends ClientConnection |
||
17 | { |
||
18 | /** |
||
19 | * State: headers |
||
20 | */ |
||
21 | const STATE_HEADERS = 1; |
||
22 | |||
23 | /** |
||
24 | * State: body |
||
25 | */ |
||
26 | const STATE_BODY = 2; |
||
27 | |||
28 | /** |
||
29 | * @var array Associative array of headers |
||
30 | */ |
||
31 | public $headers = []; |
||
32 | |||
33 | /** |
||
34 | * @var integer Content length |
||
35 | */ |
||
36 | public $contentLength = -1; |
||
37 | |||
38 | /** |
||
39 | * @var string Contains response body |
||
40 | */ |
||
41 | public $body = ''; |
||
42 | |||
43 | /** |
||
44 | * @var string End of line |
||
45 | */ |
||
46 | protected $EOL = "\r\n"; |
||
47 | |||
48 | /** |
||
49 | * @var array Associative array of Cookies |
||
50 | */ |
||
51 | public $cookie = []; |
||
52 | |||
53 | /** |
||
54 | * @var integer Size of current chunk |
||
55 | */ |
||
56 | protected $curChunkSize; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $curChunk; |
||
62 | |||
63 | /** |
||
64 | * @var boolean |
||
65 | */ |
||
66 | public $chunked = false; |
||
67 | |||
68 | /** |
||
69 | * @var callback |
||
70 | */ |
||
71 | public $chunkcb; |
||
72 | |||
73 | /** |
||
74 | * @var integer |
||
75 | */ |
||
76 | public $protocolError; |
||
77 | |||
78 | /** |
||
79 | * @var integer |
||
80 | */ |
||
81 | public $responseCode = 0; |
||
82 | |||
83 | /** |
||
84 | * @var string Last requested URL |
||
85 | */ |
||
86 | public $lastURL; |
||
87 | |||
88 | /** |
||
89 | * @var array Raw headers array |
||
90 | */ |
||
91 | public $rawHeaders = null; |
||
92 | |||
93 | public $contentType; |
||
94 | |||
95 | public $charset; |
||
96 | |||
97 | public $eofTerminated = false; |
||
98 | |||
99 | /** |
||
100 | * @var \SplStack |
||
101 | */ |
||
102 | protected $requests; |
||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | public $reqType; |
||
108 | |||
109 | /** |
||
110 | * Constructor |
||
111 | */ |
||
112 | protected function init() { |
||
115 | |||
116 | /** |
||
117 | * Send request headers |
||
118 | * @param $type |
||
119 | * @param $url |
||
120 | * @param &$params |
||
121 | * @return void |
||
122 | */ |
||
123 | protected function sendRequestHeaders($type, $url, &$params) { |
||
171 | |||
172 | /** |
||
173 | * Perform a HEAD request |
||
174 | * @param string $url |
||
175 | * @param array $params |
||
176 | */ |
||
177 | public function head($url, $params = null) |
||
181 | |||
182 | /** |
||
183 | * Perform a GET request |
||
184 | * @param string $url |
||
185 | * @param array $params |
||
186 | */ |
||
187 | public function get($url, $params = null) |
||
191 | |||
192 | /** |
||
193 | * @param array $headers |
||
194 | */ |
||
195 | protected function customRequestHeaders($headers) |
||
209 | |||
210 | /** |
||
211 | * Perform a POST request |
||
212 | * @param string $url |
||
213 | * @param array $data |
||
214 | * @param array $params |
||
215 | */ |
||
216 | public function post($url, $data = [], $params = null) |
||
241 | |||
242 | /** |
||
243 | * Get body |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getBody() |
||
250 | |||
251 | /** |
||
252 | * Get headers |
||
253 | * @return array |
||
254 | */ |
||
255 | public function getHeaders() |
||
259 | |||
260 | /** |
||
261 | * Get header |
||
262 | * @param string $name Header name |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getHeader($name) |
||
270 | |||
271 | /** |
||
272 | * Called when new data received |
||
273 | */ |
||
274 | public function onRead() |
||
420 | |||
421 | /** |
||
422 | * Called when connection finishes |
||
423 | */ |
||
424 | public function onFinish() |
||
441 | |||
442 | /** |
||
443 | * Called when request is finished |
||
444 | */ |
||
445 | protected function requestFinished() |
||
465 | } |
||
466 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.