| Total Complexity | 54 |
| Total Lines | 257 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RequestHeaderParser 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.
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 RequestHeaderParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class RequestHeaderParser extends EventEmitter |
||
| 24 | { |
||
| 25 | private $maxSize = 8192; |
||
| 26 | |||
| 27 | public function handle(ConnectionInterface $conn) |
||
| 28 | { |
||
| 29 | $buffer = ''; |
||
| 30 | $maxSize = $this->maxSize; |
||
| 31 | $that = $this; |
||
| 32 | $conn->on('data', $fn = function ($data) use (&$buffer, &$fn, $conn, $maxSize, $that) { |
||
| 33 | // append chunk of data to buffer and look for end of request headers |
||
| 34 | $buffer .= $data; |
||
| 35 | $endOfHeader = \strpos($buffer, "\r\n\r\n"); |
||
| 36 | |||
| 37 | // reject request if buffer size is exceeded |
||
| 38 | if ($endOfHeader > $maxSize || ($endOfHeader === false && isset($buffer[$maxSize]))) { |
||
| 39 | $conn->removeListener('data', $fn); |
||
| 40 | $fn = null; |
||
| 41 | |||
| 42 | $that->emit('error', array( |
||
| 43 | new \OverflowException("Maximum header size of {$maxSize} exceeded.", Response::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE), |
||
| 44 | $conn |
||
| 45 | )); |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | // ignore incomplete requests |
||
| 50 | if ($endOfHeader === false) { |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | // request headers received => try to parse request |
||
| 55 | $conn->removeListener('data', $fn); |
||
| 56 | $fn = null; |
||
| 57 | |||
| 58 | try { |
||
| 59 | $request = $that->parseRequest( |
||
| 60 | (string)\substr($buffer, 0, $endOfHeader + 2), |
||
| 61 | $conn->getRemoteAddress(), |
||
| 62 | $conn->getLocalAddress() |
||
| 63 | ); |
||
| 64 | } catch (Exception $exception) { |
||
| 65 | $buffer = ''; |
||
| 66 | $that->emit('error', array( |
||
| 67 | $exception, |
||
| 68 | $conn |
||
| 69 | )); |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | $contentLength = 0; |
||
| 74 | if ($request->hasHeader('Transfer-Encoding')) { |
||
| 75 | $contentLength = null; |
||
| 76 | } elseif ($request->hasHeader('Content-Length')) { |
||
| 77 | $contentLength = (int)$request->getHeaderLine('Content-Length'); |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($contentLength === 0) { |
||
| 81 | // happy path: request body is known to be empty |
||
| 82 | $stream = new EmptyBodyStream(); |
||
| 83 | $request = $request->withBody($stream); |
||
| 84 | } else { |
||
| 85 | // otherwise body is present => delimit using Content-Length or ChunkedDecoder |
||
| 86 | $stream = new CloseProtectionStream($conn); |
||
| 87 | if ($contentLength !== null) { |
||
| 88 | $stream = new LengthLimitedStream($stream, $contentLength); |
||
| 89 | } else { |
||
| 90 | $stream = new ChunkedDecoder($stream); |
||
| 91 | } |
||
| 92 | |||
| 93 | $request = $request->withBody(new HttpBodyStream($stream, $contentLength)); |
||
| 94 | } |
||
| 95 | |||
| 96 | $bodyBuffer = isset($buffer[$endOfHeader + 4]) ? \substr($buffer, $endOfHeader + 4) : ''; |
||
| 97 | $buffer = ''; |
||
| 98 | $that->emit('headers', array($request, $conn)); |
||
| 99 | |||
| 100 | if ($bodyBuffer !== '') { |
||
| 101 | $conn->emit('data', array($bodyBuffer)); |
||
| 102 | } |
||
| 103 | |||
| 104 | // happy path: request body is known to be empty => immediately end stream |
||
| 105 | if ($contentLength === 0) { |
||
| 106 | $stream->emit('end'); |
||
| 107 | $stream->close(); |
||
| 108 | } |
||
| 109 | }); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $headers buffer string containing request headers only |
||
| 114 | * @param ?string $remoteSocketUri |
||
| 115 | * @param ?string $localSocketUri |
||
| 116 | * @return ServerRequestInterface |
||
| 117 | * @throws \InvalidArgumentException |
||
| 118 | * @internal |
||
| 119 | */ |
||
| 120 | public function parseRequest($headers, $remoteSocketUri, $localSocketUri) |
||
| 280 | } |
||
| 281 | } |
||
| 282 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.