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) |
||
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) |
||
221 | |||
222 | /** |
||
223 | * Get cookie by name |
||
224 | * @param string $name Name of cookie |
||
225 | * @return string Contents |
||
226 | */ |
||
227 | protected function getCookieStr($name) |
||
231 | |||
232 | /** |
||
233 | * Called to check if Request is ready |
||
234 | * @return boolean Ready? |
||
235 | */ |
||
236 | public function checkIfReady() |
||
258 | |||
259 | /** |
||
260 | * Upload maximum file size |
||
261 | * @return integer |
||
262 | */ |
||
263 | public function getUploadMaxSize() |
||
267 | |||
268 | /** |
||
269 | * Parses GET-query string and other request's headers |
||
270 | * @return void |
||
271 | */ |
||
272 | protected function parseParams() |
||
339 | |||
340 | /** |
||
341 | * Prepares the request body |
||
342 | * @return void |
||
343 | */ |
||
344 | public function postPrepare() |
||
402 | |||
403 | /** |
||
404 | * Ensure that headers are sent |
||
405 | * @return boolean Were already sent? |
||
406 | */ |
||
407 | public function ensureSentHeaders() |
||
451 | |||
452 | /** |
||
453 | * Output some data |
||
454 | * @param string $s String to out |
||
455 | * @param boolean $flush ob_flush? |
||
456 | * @return boolean Success |
||
457 | */ |
||
458 | public function out($s, $flush = true) |
||
509 | |||
510 | /** |
||
511 | * Called when request's headers parsed |
||
512 | * @return void |
||
513 | */ |
||
514 | public function onParsedParams() |
||
517 | |||
518 | /** |
||
519 | * Outputs data with headers (split by \r\n\r\n) |
||
520 | * @param string $s Data |
||
521 | * @return boolean Success |
||
522 | */ |
||
523 | public function combinedOut($s) |
||
542 | |||
543 | /** |
||
544 | * Use chunked encoding |
||
545 | * @return void |
||
546 | */ |
||
547 | public function chunked() |
||
552 | |||
553 | /** |
||
554 | * Called when the request wakes up |
||
555 | * @return void |
||
556 | */ |
||
557 | public function onWakeup() |
||
571 | |||
572 | /** |
||
573 | * Called when the request starts sleep |
||
574 | * @return void |
||
575 | */ |
||
576 | public function onSleep() |
||
590 | |||
591 | /** |
||
592 | * Send HTTP-status |
||
593 | * @param integer $code Code |
||
594 | * @throws RequestHeadersAlreadySent |
||
595 | * @return boolean Success |
||
596 | */ |
||
597 | public function status($code = 200) |
||
605 | |||
606 | /** |
||
607 | * Checks if headers have been sent |
||
608 | * @param string &$file File name |
||
609 | * @param integer &$line Line in file |
||
610 | * @return boolean Success |
||
611 | */ |
||
612 | public function headers_sent(&$file, &$line) |
||
618 | |||
619 | /** |
||
620 | * Return current list of headers |
||
621 | * @return array Headers |
||
622 | */ |
||
623 | public function headers_list() |
||
627 | |||
628 | /** |
||
629 | * Set the cookie |
||
630 | * @param string $name Name of cookie |
||
631 | * @param string $value Value |
||
632 | * @param integer $maxage Optional. Max-Age. Default is 0 |
||
633 | * @param string $path Optional. Path. Default is empty string |
||
634 | * @param string $domain Optional. Domain. Default is empty string |
||
635 | * @param boolean $secure Optional. Secure. Default is false |
||
636 | * @param boolean $HTTPOnly Optional. HTTPOnly. Default is false |
||
637 | * @return void |
||
638 | */ |
||
639 | View Code Duplication | public function setcookie($name, $value = '', $maxage = 0, $path = '', $domain = '', $secure = false, $HTTPOnly = false) |
|
649 | |||
650 | /** |
||
651 | * Send the header |
||
652 | * @param string $s Header. Example: 'Location: http://php.net/' |
||
653 | * @param boolean $replace Optional. Replace? |
||
654 | * @param integer $code Optional. HTTP response code |
||
655 | * @throws \PHPDaemon\Request\RequestHeadersAlreadySent |
||
656 | * @return boolean Success |
||
657 | */ |
||
658 | View Code Duplication | public function header($s, $replace = true, $code = false) |
|
715 | |||
716 | /** |
||
717 | * Removes a header |
||
718 | * @param string $s Header name. Example: 'Location' |
||
719 | * @return void |
||
720 | */ |
||
721 | public function removeHeader($s) |
||
725 | |||
726 | /** |
||
727 | * Converts human-readable representation of size to number of bytes |
||
728 | * @param string $value String of size |
||
729 | * @return integer |
||
730 | */ |
||
731 | View Code Duplication | public static function parseSize($value) |
|
765 | |||
766 | /** |
||
767 | * Called when file upload started |
||
768 | * @param Input $in Input buffer |
||
769 | * @return void |
||
770 | */ |
||
771 | public function onUploadFileStart($in) |
||
785 | |||
786 | /** |
||
787 | * Called when chunk of incoming file has arrived |
||
788 | * @param Input $in Input buffer |
||
789 | * @param boolean $last Last? |
||
790 | * @return void |
||
791 | */ |
||
792 | public function onUploadFileChunk($in, $last = false) |
||
816 | |||
817 | /** |
||
818 | * Freeze input |
||
819 | * @return void |
||
820 | */ |
||
821 | protected function freezeInput() |
||
826 | |||
827 | /** |
||
828 | * Unfreeze input |
||
829 | * @return void |
||
830 | */ |
||
831 | protected function unfreezeInput() |
||
838 | |||
839 | /** |
||
840 | * Returns path to directory of temporary upload files |
||
841 | * @return string |
||
842 | */ |
||
843 | public function getUploadTempDir() |
||
850 | |||
851 | /** |
||
852 | * Tells whether the file was uploaded via HTTP POST |
||
853 | * @param string $path The filename being checked |
||
854 | * @return boolean Whether if this is uploaded file |
||
855 | */ |
||
856 | public function isUploadedFile($path) |
||
871 | |||
872 | /** |
||
873 | * Moves an uploaded file to a new location |
||
874 | * @param string $filename The filename of the uploaded file |
||
875 | * @param string $dest The destination of the moved file |
||
876 | * @return boolean Success |
||
877 | */ |
||
878 | public function moveUploadedFile($filename, $dest) |
||
885 | |||
886 | /** |
||
887 | * Read request body from the file given in REQUEST_BODY_FILE parameter |
||
888 | * @return boolean Success |
||
889 | */ |
||
890 | public function readBodyFile() |
||
908 | |||
909 | /** |
||
910 | * Replacement for default parse_str(), it supoorts UCS-2 like this: %uXXXX |
||
911 | * @param string $s String to parse |
||
912 | * @param array &$var Reference to the resulting array |
||
913 | * @param boolean $header Header-style string |
||
914 | * @return void |
||
915 | */ |
||
916 | public static function parse_str($s, &$var, $header = false) |
||
935 | |||
936 | /** |
||
937 | * Called after request finish |
||
938 | * @param callable $cb Callback |
||
939 | * @return void |
||
940 | */ |
||
941 | protected function postFinishHandler($cb = null) |
||
960 | } |
||
961 |