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 |
||
17 | class Connection extends \PHPDaemon\Network\Connection implements IRequestUpstream |
||
18 | { |
||
19 | |||
20 | protected $initialLowMark = 1; |
||
21 | |||
22 | /** |
||
23 | * @var integer initial value of the maximum amout of bytes in buffer |
||
24 | */ |
||
25 | protected $initialHighMark = 8192; |
||
26 | |||
27 | protected $timeout = 45; |
||
28 | |||
29 | protected $req; |
||
30 | |||
31 | protected $keepaliveTimer; |
||
32 | |||
33 | protected $freedBeforeProcessing = false; |
||
34 | |||
35 | /** |
||
36 | * @TODO DESCR |
||
|
|||
37 | */ |
||
38 | const STATE_FIRSTLINE = 1; |
||
39 | /** |
||
40 | * @TODO DESCR |
||
41 | */ |
||
42 | const STATE_HEADERS = 2; |
||
43 | /** |
||
44 | * @TODO DESCR |
||
45 | */ |
||
46 | const STATE_CONTENT = 3; |
||
47 | /** |
||
48 | * @TODO DESCR |
||
49 | */ |
||
50 | const STATE_PROCESSING = 4; |
||
51 | |||
52 | protected $EOL = "\r\n"; |
||
53 | protected $currentHeader; |
||
54 | |||
55 | protected $policyReqNotFound = false; |
||
56 | |||
57 | /** |
||
58 | * Check if Sendfile is supported here. |
||
59 | * @return boolean Succes |
||
60 | */ |
||
61 | public function checkSendfileCap() |
||
65 | |||
66 | /** |
||
67 | * Check if Chunked encoding is supported here. |
||
68 | * @return boolean Succes |
||
69 | */ |
||
70 | public function checkChunkedEncCap() |
||
74 | |||
75 | /** |
||
76 | * @TODO |
||
77 | * @return integer |
||
78 | */ |
||
79 | public function getKeepaliveTimeout() |
||
83 | |||
84 | /** |
||
85 | * Read first line of HTTP request |
||
86 | * @return boolean|null Success |
||
87 | */ |
||
88 | protected function httpReadFirstline() |
||
121 | |||
122 | /** |
||
123 | * Read headers line-by-line |
||
124 | * @return boolean|null Success |
||
125 | */ |
||
126 | protected function httpReadHeaders() |
||
147 | |||
148 | /** |
||
149 | * Creates new Request object |
||
150 | * @return \stdClass |
||
151 | */ |
||
152 | protected function newRequest() |
||
171 | |||
172 | /** |
||
173 | * Process HTTP headers |
||
174 | * @return boolean Success |
||
175 | */ |
||
176 | protected function httpProcessHeaders() |
||
209 | |||
210 | /* Used for debugging protocol issues */ |
||
211 | /*public function readline() { |
||
212 | $s = parent::readline(); |
||
213 | Daemon::log(Debug::json($s)); |
||
214 | return $s; |
||
215 | } |
||
216 | |||
217 | public function write($s) { |
||
218 | Daemon::log(Debug::json($s)); |
||
219 | parent::write($s); |
||
220 | }* |
||
221 | |||
222 | /** |
||
223 | * Called when new data received. |
||
224 | * @return void |
||
225 | */ |
||
226 | |||
227 | /** |
||
228 | * onRead |
||
229 | * @return void |
||
230 | */ |
||
231 | protected function onRead() |
||
329 | |||
330 | /** |
||
331 | * Handles the output from downstream requests. |
||
332 | * @param object $req \PHPDaemon\Request\Generic. |
||
333 | * @param string $s The output. |
||
334 | * @return boolean Success |
||
335 | */ |
||
336 | public function requestOut($req, $s) |
||
344 | |||
345 | /** |
||
346 | * End request |
||
347 | * @return void |
||
348 | */ |
||
349 | public function endRequest($req, $appStatus, $protoStatus) |
||
368 | |||
369 | /** |
||
370 | * Frees this request |
||
371 | * @return void |
||
372 | */ |
||
373 | public function freeRequest($req) |
||
384 | |||
385 | /** |
||
386 | * Called when connection is finished |
||
387 | * @return void |
||
388 | */ |
||
389 | public function onFinish() |
||
399 | |||
400 | /** |
||
401 | * Send Bad request |
||
402 | * @return void |
||
403 | */ |
||
404 | public function badRequest($req) |
||
410 | } |
||
411 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.