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 |
||
| 20 | abstract class Generic extends \PHPDaemon\Request\Generic |
||
| 21 | { |
||
| 22 | use DeferredEventHandlers; |
||
| 23 | use \PHPDaemon\Traits\Sessions; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array Status codes |
||
| 27 | */ |
||
| 28 | protected static $codes = [ |
||
| 29 | 100 => 'Continue', |
||
| 30 | 101 => 'Switching Protocols', |
||
| 31 | 200 => 'OK', |
||
| 32 | 201 => 'Created', |
||
| 33 | 202 => 'Accepted', |
||
| 34 | 203 => 'Non-Authoritative Information', |
||
| 35 | 204 => 'No Content', |
||
| 36 | 205 => 'Reset Content', |
||
| 37 | 206 => 'Partial Content', |
||
| 38 | 300 => 'Multiple Choices', |
||
| 39 | 301 => 'Moved Permanently', |
||
| 40 | 302 => 'Found', |
||
| 41 | 303 => 'See Other', |
||
| 42 | 304 => 'Not Modified', |
||
| 43 | 305 => 'Use Proxy', |
||
| 44 | 306 => '(Unused)', |
||
| 45 | 307 => 'Temporary Redirect', |
||
| 46 | 400 => 'Bad Request', |
||
| 47 | 401 => 'Unauthorized', |
||
| 48 | 402 => 'Payment Required', |
||
| 49 | 403 => 'Forbidden', |
||
| 50 | 404 => 'Not Found', |
||
| 51 | 405 => 'Method Not Allowed', |
||
| 52 | 406 => 'Not Acceptable', |
||
| 53 | 407 => 'Proxy Authentication Required', |
||
| 54 | 408 => 'Request Timeout', |
||
| 55 | 409 => 'Conflict', |
||
| 56 | 410 => 'Gone', |
||
| 57 | 411 => 'Length Required', |
||
| 58 | 412 => 'Precondition Failed', |
||
| 59 | 413 => 'Request Entity Too Large', |
||
| 60 | 414 => 'Request-URI Too Long', |
||
| 61 | 415 => 'Unsupported Media Type', |
||
| 62 | 416 => 'Requested Range Not Satisfiable', |
||
| 63 | 417 => 'Expectation Failed', |
||
| 64 | 422 => 'Unprocessable Entity', |
||
| 65 | 423 => 'Locked', |
||
| 66 | 500 => 'Internal Server Error', |
||
| 67 | 501 => 'Not Implemented', |
||
| 68 | 502 => 'Bad Gateway', |
||
| 69 | 503 => 'Service Unavailable', |
||
| 70 | 504 => 'Gateway Timeout', |
||
| 71 | 505 => 'HTTP Version Not Supported', |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var boolean Keepalive? |
||
| 76 | */ |
||
| 77 | public $keepalive = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var integer Current response length |
||
| 81 | */ |
||
| 82 | public $responseLength = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var integer Content length from header() method |
||
| 86 | */ |
||
| 87 | protected $contentLength; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var integer Number of outgoing cookie-headers |
||
| 91 | */ |
||
| 92 | protected $cookieNum = 0; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array Replacement pairs for processing some header values in parse_str() |
||
| 96 | */ |
||
| 97 | public static $hvaltr = ['; ' => '&', ';' => '&', ' ' => '%20']; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array State |
||
| 101 | */ |
||
| 102 | public static $htr = ['-' => '_']; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array Outgoing headers |
||
| 106 | */ |
||
| 107 | protected $headers = ['STATUS' => '200 OK']; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var boolean Headers sent? |
||
| 111 | */ |
||
| 112 | protected $headers_sent = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var boolean File name where output started in the file and line variables |
||
| 116 | */ |
||
| 117 | protected $headers_sent_file; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var boolean Line number where output started in the file and line variables |
||
| 121 | */ |
||
| 122 | protected $headers_sent_line; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var File File pointer to send output (X-Sendfile) |
||
| 126 | */ |
||
| 127 | protected $sendfp; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var boolean Frozen input? |
||
| 131 | */ |
||
| 132 | protected $frozenInput = false; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var array Content type parameters |
||
| 136 | */ |
||
| 137 | protected $contype; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Preparing before init |
||
| 141 | * @param object $req Source request |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | protected function preinit($req) |
||
| 145 | { |
||
| 146 | if ($req === null) { |
||
| 147 | $req = new \stdClass; |
||
| 148 | $req->attrs = new \stdClass; |
||
| 149 | $req->attrs->inputDone = true; |
||
| 150 | $req->attrs->paramsDone = true; |
||
| 151 | $req->attrs->chunked = false; |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->attrs = $req->attrs; |
||
| 155 | |||
| 156 | if ($this->upstream->pool->config->expose->value) { |
||
| 157 | $this->header('X-Powered-By: phpDaemon/' . Daemon::$version); |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->attrs->input->setRequest($this); |
||
| 161 | |||
| 162 | $this->parseParams(); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Called when first deferred event used |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public function firstDeferredEventUsed() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Output whole contents of file |
||
| 176 | * @param string $path Path |
||
| 177 | * @param callable $cb Callback |
||
| 178 | * @param integer $pri Priority |
||
| 179 | * @return boolean Success |
||
| 180 | */ |
||
| 181 | public function sendfile($path, $cb, $pri = EIO_PRI_DEFAULT) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get cookie by name |
||
| 232 | * @param string $name Name of cookie |
||
| 233 | * @return string Contents |
||
| 234 | */ |
||
| 235 | protected function getCookieStr($name) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Called to check if Request is ready |
||
| 242 | * @return boolean Ready? |
||
| 243 | */ |
||
| 244 | public function checkIfReady() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Upload maximum file size |
||
| 271 | * @return integer |
||
| 272 | */ |
||
| 273 | public function getUploadMaxSize() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Parses GET-query string and other request's headers |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | protected function parseParams() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Prepares the request body |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | public function postPrepare() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Ensure that headers are sent |
||
| 408 | * @return boolean Were already sent? |
||
| 409 | */ |
||
| 410 | public function ensureSentHeaders() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Output some data |
||
| 458 | * @param string $s String to out |
||
| 459 | * @param boolean $flush ob_flush? |
||
| 460 | * @return boolean Success |
||
| 461 | */ |
||
| 462 | public function out($s, $flush = true) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Called when request's headers parsed |
||
| 516 | * @return void |
||
| 517 | */ |
||
| 518 | public function onParsedParams() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Outputs data with headers (split by \r\n\r\n) |
||
| 524 | * @param string $s Data |
||
| 525 | * @return boolean Success |
||
| 526 | */ |
||
| 527 | public function combinedOut($s) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Use chunked encoding |
||
| 549 | * @return void |
||
| 550 | */ |
||
| 551 | public function chunked() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Called when the request wakes up |
||
| 559 | * @return void |
||
| 560 | */ |
||
| 561 | public function onWakeup() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Called when the request starts sleep |
||
| 578 | * @return void |
||
| 579 | */ |
||
| 580 | public function onSleep() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Send HTTP-status |
||
| 597 | * @param integer $code Code |
||
| 598 | * @throws RequestHeadersAlreadySent |
||
| 599 | * @return boolean Success |
||
| 600 | */ |
||
| 601 | public function status($code = 200) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Checks if headers have been sent |
||
| 612 | * @param string &$file File name |
||
| 613 | * @param integer &$line Line in file |
||
| 614 | * @return boolean Success |
||
| 615 | */ |
||
| 616 | public function headersSent(&$file, &$line) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Return current list of headers |
||
| 625 | * @return array Headers |
||
| 626 | */ |
||
| 627 | public function headersList() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Set the cookie |
||
| 634 | * @param string $name Name of cookie |
||
| 635 | * @param string $value Value |
||
| 636 | * @param integer $maxage Optional. Max-Age. Default is 0 |
||
| 637 | * @param string $path Optional. Path. Default is empty string |
||
| 638 | * @param string $domain Optional. Domain. Default is empty string |
||
| 639 | * @param boolean $secure Optional. Secure. Default is false |
||
| 640 | * @param boolean $HTTPOnly Optional. HTTPOnly. Default is false |
||
| 641 | * @return void |
||
| 642 | */ |
||
| 643 | View Code Duplication | public function setcookie( |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Send the header |
||
| 665 | * @param string $s Header. Example: 'Location: http://php.net/' |
||
| 666 | * @param boolean $replace Optional. Replace? |
||
| 667 | * @param integer $code Optional. HTTP response code |
||
| 668 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
||
| 669 | * @return boolean Success |
||
| 670 | */ |
||
| 671 | View Code Duplication | public function header($s, $replace = true, $code = false) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * Removes a header |
||
| 731 | * @param string $s Header name. Example: 'Location' |
||
| 732 | * @return void |
||
| 733 | */ |
||
| 734 | public function removeHeader($s) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Converts human-readable representation of size to number of bytes |
||
| 741 | * @param string $value String of size |
||
| 742 | * @return integer |
||
| 743 | */ |
||
| 744 | View Code Duplication | public static function parseSize($value) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Called when file upload started |
||
| 781 | * @param Input $in Input buffer |
||
| 782 | * @return void |
||
| 783 | */ |
||
| 784 | public function onUploadFileStart($in) |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Called when chunk of incoming file has arrived |
||
| 801 | * @param Input $in Input buffer |
||
| 802 | * @param boolean $last Last? |
||
| 803 | * @return void |
||
| 804 | */ |
||
| 805 | public function onUploadFileChunk($in, $last = false) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Freeze input |
||
| 832 | * @return void |
||
| 833 | */ |
||
| 834 | protected function freezeInput() |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Unfreeze input |
||
| 842 | * @return void |
||
| 843 | */ |
||
| 844 | protected function unfreezeInput() |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Returns path to directory of temporary upload files |
||
| 854 | * @return string |
||
| 855 | */ |
||
| 856 | public function getUploadTempDir() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Tells whether the file was uploaded via HTTP POST |
||
| 866 | * @param string $path The filename being checked |
||
| 867 | * @return boolean Whether if this is uploaded file |
||
| 868 | */ |
||
| 869 | public function isUploadedFile($path) |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Moves an uploaded file to a new location |
||
| 887 | * @param string $filename The filename of the uploaded file |
||
| 888 | * @param string $dest The destination of the moved file |
||
| 889 | * @return boolean Success |
||
| 890 | */ |
||
| 891 | public function moveUploadedFile($filename, $dest) |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Read request body from the file given in REQUEST_BODY_FILE parameter |
||
| 901 | * @return boolean Success |
||
| 902 | */ |
||
| 903 | public function readBodyFile() |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Replacement for default parse_str(), it supoorts UCS-2 like this: %uXXXX |
||
| 928 | * @param string $s String to parse |
||
| 929 | * @param array &$var Reference to the resulting array |
||
| 930 | * @param boolean $header Header-style string |
||
| 931 | * @return void |
||
| 932 | */ |
||
| 933 | public static function parseStr($s, &$var, $header = false) |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Called after request finish |
||
| 953 | * @param callable $cb Callback |
||
| 954 | * @return void |
||
| 955 | */ |
||
| 956 | protected function postFinishHandler($cb = null) |
||
| 975 | } |
||
| 976 |