| Conditions | 3 |
| Paths | 1 |
| Total Lines | 64 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 12 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 121 | private function listenTo(ServerInterface $server, array $options = []) |
||
| 122 | { |
||
| 123 | $server->on( |
||
| 124 | 'connection', |
||
| 125 | function (ConnectionInterface $connection) use ($options) { |
||
| 126 | $request = new Request($connection, $options); |
||
| 127 | |||
| 128 | $request->on( |
||
| 129 | 'received_head', |
||
| 130 | function ($headers, $httpMethod, $uri, $protocolVersion) use ($request, $connection) { |
||
| 131 | $this->servedRequests++; |
||
| 132 | |||
| 133 | $response = $this->application->processHead($headers, $httpMethod, $uri, $protocolVersion); |
||
| 134 | |||
| 135 | if (null !== $response) { |
||
| 136 | $connection->removeListener('data', [$request, 'feed']); |
||
| 137 | |||
| 138 | $response = $this->modifyResponse($response); |
||
| 139 | |||
| 140 | $request->sendResponse($response); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | ); |
||
| 144 | |||
| 145 | $request->on( |
||
| 146 | 'request', |
||
| 147 | function (ServerRequestInterface $serverRequest) use ($request, $connection) { |
||
| 148 | |||
| 149 | $connection->removeListener('data', [$request, 'feed']); |
||
| 150 | |||
| 151 | $fulfilled = function (ResponseInterface $response) use ($request) { |
||
| 152 | $response = $this->modifyResponse($response); |
||
| 153 | $request->sendResponse($response); |
||
| 154 | }; |
||
| 155 | |||
| 156 | $rejected = function (\Throwable $exception) use ($request) { |
||
| 157 | $this->failedRequests++; |
||
| 158 | |||
| 159 | $response = new Response(500); |
||
| 160 | if (true === $this->debug) { |
||
| 161 | $error = [ |
||
| 162 | 'class' => get_class($exception), |
||
| 163 | 'message' => $exception->getMessage(), |
||
| 164 | 'code' => $exception->getCode(), |
||
| 165 | 'file' => $exception->getFile(), |
||
| 166 | 'line' => $exception->getLine() |
||
| 167 | ]; |
||
| 168 | |||
| 169 | $response = $response->withHeader('Content-Type', 'application/json'); |
||
| 170 | $response->getBody()->write(json_encode($error)); |
||
| 171 | } |
||
| 172 | |||
| 173 | $response = $this->modifyResponse($response); |
||
| 174 | $request->sendResponse($response); |
||
| 175 | }; |
||
| 176 | |||
| 177 | $this->application->processRequest($serverRequest)->done($fulfilled, $rejected); |
||
| 178 | } |
||
| 179 | ); |
||
| 180 | |||
| 181 | $connection->on('data', [$request, 'feed']); |
||
| 182 | } |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 195 |