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() |
||
113 | { |
||
114 | $this->requests = new \SplStack; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Send request headers |
||
119 | * @param $type |
||
120 | * @param $url |
||
121 | * @param &$params |
||
122 | * @return void |
||
123 | */ |
||
124 | protected function sendRequestHeaders($type, $url, &$params) |
||
125 | { |
||
126 | if (!is_array($params)) { |
||
127 | $params = ['resultcb' => $params]; |
||
128 | } |
||
129 | if (!isset($params['uri']) || !isset($params['host'])) { |
||
130 | $prepared = Pool::parseUrl($url); |
||
131 | if (!$prepared) { |
||
132 | if (isset($params['resultcb'])) { |
||
133 | $params['resultcb'](false); |
||
134 | } |
||
135 | return; |
||
136 | } |
||
137 | list($params['host'], $params['uri']) = $prepared; |
||
138 | } |
||
139 | if ($params['uri'] === '') { |
||
140 | $params['uri'] = '/'; |
||
141 | } |
||
142 | $this->lastURL = 'http://' . $params['host'] . $params['uri']; |
||
143 | if (!isset($params['version'])) { |
||
144 | $params['version'] = '1.1'; |
||
145 | } |
||
146 | $this->writeln($type . ' ' . $params['uri'] . ' HTTP/' . $params['version']); |
||
147 | if (isset($params['proxy'])) { |
||
148 | if (isset($params['proxy']['auth'])) { |
||
149 | $this->writeln('Proxy-Authorization: basic ' . base64_encode($params['proxy']['auth']['username'] . ':' . $params['proxy']['auth']['password'])); |
||
|
|||
150 | } |
||
151 | } |
||
152 | $this->writeln('Host: ' . $params['host']); |
||
153 | if ($this->pool->config->expose->value && !isset($params['headers']['User-Agent'])) { |
||
154 | $this->writeln('User-Agent: phpDaemon/' . Daemon::$version); |
||
155 | } |
||
156 | if (isset($params['cookie']) && sizeof($params['cookie'])) { |
||
157 | $this->writeln('Cookie: ' . http_build_query($params['cookie'], '', '; ')); |
||
158 | } |
||
159 | if (isset($params['headers'])) { |
||
160 | $this->customRequestHeaders($params['headers']); |
||
161 | } |
||
162 | if (isset($params['rawHeaders']) && $params['rawHeaders']) { |
||
163 | $this->rawHeaders = []; |
||
164 | } |
||
165 | if (isset($params['chunkcb']) && is_callable($params['chunkcb'])) { |
||
166 | $this->chunkcb = $params['chunkcb']; |
||
167 | } |
||
168 | $this->writeln(''); |
||
169 | $this->requests->push($type); |
||
170 | $this->onResponse($params['resultcb']); |
||
171 | $this->checkFree(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Perform a HEAD request |
||
176 | * @param string $url |
||
177 | * @param array $params |
||
178 | */ |
||
179 | public function head($url, $params = null) |
||
183 | |||
184 | /** |
||
185 | * Perform a GET request |
||
186 | * @param string $url |
||
187 | * @param array $params |
||
188 | */ |
||
189 | public function get($url, $params = null) |
||
193 | |||
194 | /** |
||
195 | * @param array $headers |
||
196 | */ |
||
197 | protected function customRequestHeaders($headers) |
||
211 | |||
212 | /** |
||
213 | * Perform a POST request |
||
214 | * @param string $url |
||
215 | * @param array $data |
||
216 | * @param array $params |
||
217 | */ |
||
218 | public function post($url, $data = [], $params = null) |
||
243 | |||
244 | /** |
||
245 | * Get body |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getBody() |
||
252 | |||
253 | /** |
||
254 | * Get headers |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getHeaders() |
||
261 | |||
262 | /** |
||
263 | * Get header |
||
264 | * @param string $name Header name |
||
265 | * @return string |
||
266 | */ |
||
267 | public function getHeader($name) |
||
272 | |||
273 | /** |
||
274 | * Called when new data received |
||
275 | */ |
||
276 | public function onRead() |
||
422 | |||
423 | /** |
||
424 | * Called when connection finishes |
||
425 | */ |
||
426 | public function onFinish() |
||
443 | |||
444 | /** |
||
445 | * Called when request is finished |
||
446 | */ |
||
447 | protected function requestFinished() |
||
467 | } |
||
468 |
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.