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 |
||
| 12 | class Connection extends \PHPDaemon\Network\Connection implements IRequestUpstream |
||
| 13 | { |
||
| 14 | const FCGI_BEGIN_REQUEST = 1; |
||
| 15 | const FCGI_ABORT_REQUEST = 2; |
||
| 16 | const FCGI_END_REQUEST = 3; |
||
| 17 | const FCGI_PARAMS = 4; |
||
| 18 | const FCGI_STDIN = 5; |
||
| 19 | const FCGI_STDOUT = 6; |
||
| 20 | const FCGI_STDERR = 7; |
||
| 21 | const FCGI_DATA = 8; |
||
| 22 | const FCGI_GET_VALUES = 9; |
||
| 23 | const FCGI_GET_VALUES_RESULT = 10; |
||
| 24 | const FCGI_UNKNOWN_TYPE = 11; |
||
| 25 | const FCGI_RESPONDER = 1; |
||
| 26 | const FCGI_AUTHORIZER = 2; |
||
| 27 | const FCGI_FILTER = 3; |
||
| 28 | const STATE_CONTENT = 1; |
||
| 29 | const STATE_PADDING = 2; |
||
| 30 | protected static $roles = [ |
||
| 31 | self::FCGI_RESPONDER => 'FCGI_RESPONDER', |
||
| 32 | self::FCGI_AUTHORIZER => 'FCGI_AUTHORIZER', |
||
| 33 | self::FCGI_FILTER => 'FCGI_FILTER', |
||
| 34 | ]; |
||
| 35 | protected static $requestTypes = [ |
||
| 36 | self::FCGI_BEGIN_REQUEST => 'FCGI_BEGIN_REQUEST', |
||
| 37 | self::FCGI_ABORT_REQUEST => 'FCGI_ABORT_REQUEST', |
||
| 38 | self::FCGI_END_REQUEST => 'FCGI_END_REQUEST', |
||
| 39 | self::FCGI_PARAMS => 'FCGI_PARAMS', |
||
| 40 | self::FCGI_STDIN => 'FCGI_STDIN', |
||
| 41 | self::FCGI_STDOUT => 'FCGI_STDOUT', |
||
| 42 | self::FCGI_STDERR => 'FCGI_STDERR', |
||
| 43 | self::FCGI_DATA => 'FCGI_DATA', |
||
| 44 | self::FCGI_GET_VALUES => 'FCGI_GET_VALUES', |
||
| 45 | self::FCGI_GET_VALUES_RESULT => 'FCGI_GET_VALUES_RESULT', |
||
| 46 | self::FCGI_UNKNOWN_TYPE => 'FCGI_UNKNOWN_TYPE', |
||
| 47 | ]; |
||
| 48 | public $timeout = 180; |
||
| 49 | /** |
||
| 50 | * @var integer initial value of the minimal amout of bytes in buffer |
||
| 51 | */ |
||
| 52 | protected $lowMark = 8; |
||
| 53 | /** |
||
| 54 | * @var integer initial value of the maximum amout of bytes in buffer |
||
| 55 | */ |
||
| 56 | protected $highMark = 0xFFFFFF; |
||
| 57 | protected $requests = []; |
||
| 58 | protected $header; |
||
| 59 | protected $content; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Is this upstream suitable for sendfile()? |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | public function checkSendfileCap() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Is this upstream suitable for chunked encoding? |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function checkChunkedEncCap() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @TODO |
||
| 83 | * @return integer |
||
| 84 | */ |
||
| 85 | public function getKeepaliveTimeout() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Called when new data received |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function onRead() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Handles the output from downstream requests |
||
| 279 | * @param object $req |
||
| 280 | * @param string $appStatus |
||
| 281 | * @param string $protoStatus |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function endRequest($req, $appStatus, $protoStatus) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Handles the output from downstream requests |
||
| 307 | * @param object $req Request |
||
| 308 | * @param string $out The output |
||
| 309 | * @return boolean Success |
||
| 310 | */ |
||
| 311 | public function requestOut($req, $out) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sends a chunk |
||
| 332 | * @param object $req Request |
||
| 333 | * @param string $chunk Data |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function sendChunk($req, $chunk) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Frees request |
||
| 353 | * @param object $req |
||
| 354 | */ |
||
| 355 | public function freeRequest($req) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Send Bad request |
||
| 363 | * @param object $req |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | public function badRequest($req) |
||
| 370 | } |
||
| 371 |