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() |
||
111 | { |
||
112 | $this->requests = new \SplStack; |
||
113 | } |
||
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) |
||
123 | { |
||
124 | if (!is_array($params)) { |
||
125 | $params = ['resultcb' => $params]; |
||
126 | } |
||
127 | if (!isset($params['uri']) || !isset($params['host'])) { |
||
128 | $prepared = Pool::parseUrl($url); |
||
129 | if (!$prepared) { |
||
130 | if (isset($params['resultcb'])) { |
||
131 | $params['resultcb'](false); |
||
132 | } |
||
133 | return; |
||
134 | } |
||
135 | list($params['host'], $params['uri']) = $prepared; |
||
136 | } |
||
137 | if ($params['uri'] === '') { |
||
138 | $params['uri'] = '/'; |
||
139 | } |
||
140 | $this->lastURL = 'http://' . $params['host'] . $params['uri']; |
||
141 | if (!isset($params['version'])) { |
||
142 | $params['version'] = '1.1'; |
||
143 | } |
||
144 | $this->writeln($type . ' ' . $params['uri'] . ' HTTP/' . $params['version']); |
||
145 | if (isset($params['proxy'])) { |
||
146 | if (isset($params['proxy']['auth'])) { |
||
147 | $this->writeln('Proxy-Authorization: basic ' . base64_encode($params['proxy']['auth']['username'] . ':' . $params['proxy']['auth']['password'])); |
||
|
|||
148 | } |
||
149 | } |
||
150 | $this->writeln('Host: ' . $params['host']); |
||
151 | if ($this->pool->config->expose->value && !isset($params['headers']['User-Agent'])) { |
||
152 | $this->writeln('User-Agent: phpDaemon/' . Daemon::$version); |
||
153 | } |
||
154 | if (isset($params['cookie']) && sizeof($params['cookie'])) { |
||
155 | $this->writeln('Cookie: ' . http_build_query($params['cookie'], '', '; ')); |
||
156 | } |
||
157 | if (isset($params['contentType'])) { |
||
158 | if (!isset($params['headers'])) { |
||
159 | $params['headers'] = []; |
||
160 | } |
||
161 | $params['headers']['Content-Type'] = $params['contentType']; |
||
162 | } |
||
163 | if (isset($params['headers'])) { |
||
164 | $this->customRequestHeaders($params['headers']); |
||
165 | } |
||
166 | if (isset($params['rawHeaders']) && $params['rawHeaders']) { |
||
167 | $this->rawHeaders = []; |
||
168 | } |
||
169 | if (isset($params['chunkcb']) && is_callable($params['chunkcb'])) { |
||
170 | $this->chunkcb = $params['chunkcb']; |
||
171 | } |
||
172 | $this->writeln(''); |
||
173 | $this->requests->push($type); |
||
174 | $this->onResponse->push($params['resultcb']); |
||
175 | $this->checkFree(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Perform a HEAD request |
||
180 | * @param string $url |
||
181 | * @param array $params |
||
182 | */ |
||
183 | public function head($url, $params = null) |
||
184 | { |
||
185 | $this->sendRequestHeaders('HEAD', $url, $params); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Perform a GET request |
||
190 | * @param string $url |
||
191 | * @param array $params |
||
192 | */ |
||
193 | public function get($url, $params = null) |
||
194 | { |
||
195 | $this->sendRequestHeaders('GET', $url, $params); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param array $headers |
||
200 | */ |
||
201 | protected function customRequestHeaders($headers) |
||
202 | { |
||
203 | foreach ($headers as $key => $item) { |
||
204 | if (is_numeric($key)) { |
||
205 | if (is_string($item)) { |
||
206 | $this->writeln($item); |
||
207 | } elseif (is_array($item)) { |
||
208 | $this->writeln($item[0] . ': ' . $item[1]); // @TODO: prevent injections? |
||
209 | } |
||
210 | } else { |
||
211 | $this->writeln($key . ': ' . $item); |
||
212 | } |
||
213 | } |
||
214 | } |
||
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) |
||
223 | { |
||
224 | foreach ($data as $val) { |
||
225 | if ($val instanceof UploadFile) { |
||
226 | $params['contentType'] = 'multipart/form-data'; |
||
227 | } |
||
228 | } |
||
229 | if (!isset($params['contentType'])) { |
||
230 | $params['contentType'] = 'application/x-www-form-urlencoded'; |
||
231 | } |
||
232 | if ($params['contentType'] === 'application/x-www-form-urlencoded') { |
||
233 | $body = http_build_query($data, '', '&', PHP_QUERY_RFC3986); |
||
234 | } elseif ($params['contentType'] === 'application/x-json') { |
||
235 | $body = json_encode($data); |
||
236 | } else { |
||
237 | $body = 'Unsupported Content-Type'; |
||
238 | } |
||
239 | if (!isset($params['headers'])) { |
||
240 | $params['headers'] = []; |
||
241 | } |
||
242 | $params['headers']['Content-Length'] = mb_orig_strlen($body); |
||
243 | $this->sendRequestHeaders('POST', $url, $params); |
||
244 | $this->write($body); |
||
245 | $this->writeln(''); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get body |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getBody() |
||
256 | |||
257 | /** |
||
258 | * Get headers |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getHeaders() |
||
265 | |||
266 | /** |
||
267 | * Get header |
||
268 | * @param string $name Header name |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getHeader($name) |
||
276 | |||
277 | /** |
||
278 | * Called when new data received |
||
279 | */ |
||
280 | public function onRead() |
||
281 | { |
||
282 | if ($this->state === self::STATE_BODY) { |
||
426 | |||
427 | /** |
||
428 | * Called when connection finishes |
||
429 | */ |
||
430 | public function onFinish() |
||
447 | |||
448 | /** |
||
449 | * Called when request is finished |
||
450 | */ |
||
451 | protected function requestFinished() |
||
471 | } |
||
472 |
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.