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 Generic 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 Generic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class Generic extends \PHPDaemon\HTTPRequest\Generic |
||
13 | { |
||
14 | protected $stage = 0; |
||
15 | protected $sessId; |
||
16 | protected $serverId; |
||
17 | protected $path; |
||
18 | protected $heartbeatTimer; |
||
19 | |||
20 | protected $opts; |
||
21 | |||
22 | protected $stopped = false; |
||
23 | |||
24 | protected $callbackParamEnabled = false; |
||
25 | protected $frames = []; |
||
26 | |||
27 | protected $allowedMethods = 'GET'; |
||
28 | |||
29 | protected $errors = [ |
||
30 | 1002 => 'Connection interrupted', |
||
31 | 2010 => 'Another connection still open', |
||
32 | 3000 => 'Go away!', |
||
33 | ]; |
||
34 | |||
35 | protected $pollMode = ['stream']; |
||
36 | |||
37 | protected $gcEnabled = true; |
||
38 | |||
39 | protected $cacheable = false; |
||
40 | protected $poll = false; |
||
41 | |||
42 | protected $bytesSent = 0; |
||
43 | |||
44 | protected $heartbeatOnFinish = false; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * @return void |
||
50 | */ |
||
51 | public function init() |
||
110 | |||
111 | /** |
||
112 | * CORS |
||
113 | * @return void |
||
114 | */ |
||
115 | protected function CORS() |
||
138 | |||
139 | /** |
||
140 | * contentType |
||
141 | * @param string $type Content-Type |
||
142 | * @return void |
||
143 | */ |
||
144 | protected function contentType($type) |
||
148 | |||
149 | /** |
||
150 | * noncache |
||
151 | * @return void |
||
152 | */ |
||
153 | protected function noncache() |
||
158 | |||
159 | /** |
||
160 | * Output some data |
||
161 | * @param string $s String to out |
||
162 | * @param boolean $flush |
||
163 | * @return boolean Success |
||
164 | */ |
||
165 | public function out($s, $flush = true) |
||
172 | |||
173 | /** |
||
174 | * Stop |
||
175 | * @return void |
||
176 | */ |
||
177 | public function stop() |
||
191 | |||
192 | /** |
||
193 | * Send frame |
||
194 | * @param string $frame |
||
195 | * @return void |
||
196 | */ |
||
197 | protected function sendFrame($frame) |
||
203 | |||
204 | /** |
||
205 | * gcCheck |
||
206 | * @return void |
||
207 | */ |
||
208 | public function gcCheck() |
||
218 | |||
219 | /** |
||
220 | * afterHeaders |
||
221 | * @return void |
||
222 | */ |
||
223 | public function afterHeaders() |
||
226 | |||
227 | /** |
||
228 | * acquire |
||
229 | * @param callable $cb |
||
230 | * @callback $cb ( ) |
||
231 | * @return void |
||
232 | */ |
||
233 | protected function acquire($cb) |
||
285 | |||
286 | /** |
||
287 | * internalServerError |
||
288 | * @return void |
||
289 | */ |
||
290 | public function internalServerError() |
||
296 | |||
297 | /** |
||
298 | * error |
||
299 | * @param integer $code |
||
300 | * @return void |
||
301 | */ |
||
302 | protected function error($code) |
||
306 | |||
307 | /** |
||
308 | * anotherConnectionStillOpen |
||
309 | * @return void |
||
310 | */ |
||
311 | protected function anotherConnectionStillOpen() |
||
320 | |||
321 | /** |
||
322 | * Poll |
||
323 | * @param callable $cb |
||
324 | * @callback $cb ( ) |
||
325 | * @return void |
||
326 | */ |
||
327 | protected function poll($cb = null) |
||
391 | |||
392 | /** |
||
393 | * Output some data |
||
394 | * @param string $s String to out |
||
395 | * @param boolean $flush |
||
396 | * @return boolean Success |
||
397 | */ |
||
398 | public function outputFrame($s, $flush = true) |
||
403 | |||
404 | /** |
||
405 | * Called when request iterated |
||
406 | * @return void |
||
407 | */ |
||
408 | public function run() |
||
412 | |||
413 | /** |
||
414 | * w8in |
||
415 | * @return void |
||
416 | */ |
||
417 | public function w8in() |
||
420 | |||
421 | /** |
||
422 | * s2c |
||
423 | * @param object $redis |
||
424 | * @return void |
||
425 | */ |
||
426 | public function s2c($redis) |
||
448 | |||
449 | /** |
||
450 | * On finish |
||
451 | * @return void |
||
452 | */ |
||
453 | public function onFinish() |
||
463 | } |
||
464 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: