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