Conditions | 3 |
Paths | 1 |
Total Lines | 66 |
Code Lines | 38 |
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 |
||
137 | private function listenTo(ServerInterface $server, array $options = []) |
||
138 | { |
||
139 | $server->on( |
||
140 | 'connection', |
||
141 | function (ConnectionInterface $connection) use ($options) { |
||
142 | $request = new Request($connection, $options); |
||
143 | |||
144 | $request->on( |
||
145 | 'received_head', |
||
146 | function ($headers, $httpMethod, $uri, $protocolVersion) use ($request, $connection) { |
||
147 | $this->servedRequests++; |
||
148 | |||
149 | $response = $this->application->processHead($headers, $httpMethod, $uri, $protocolVersion); |
||
150 | |||
151 | if (null !== $response) { |
||
152 | $connection->removeListener('data', [$request, 'feed']); |
||
153 | |||
154 | $response = $this->responseModifiers->modify($response); |
||
155 | |||
156 | $request->sendResponse($response); |
||
157 | } |
||
158 | } |
||
159 | ); |
||
160 | |||
161 | $request->on( |
||
162 | 'request', |
||
163 | function (ServerRequestInterface $serverRequest) use ($request, $connection) { |
||
164 | |||
165 | $connection->removeListener('data', [$request, 'feed']); |
||
166 | |||
167 | $serverRequest = $this->requestModifiers->modify($serverRequest); |
||
168 | |||
169 | $fulfilled = function (ResponseInterface $response) use ($request) { |
||
170 | $response = $this->responseModifiers->modify($response); |
||
171 | $request->sendResponse($response); |
||
172 | }; |
||
173 | |||
174 | $rejected = function (\Throwable $exception) use ($request) { |
||
175 | $this->failedRequests++; |
||
176 | |||
177 | $response = new Response(500); |
||
178 | if (true === $this->debug) { |
||
179 | $error = [ |
||
180 | 'class' => get_class($exception), |
||
181 | 'message' => $exception->getMessage(), |
||
182 | 'code' => $exception->getCode(), |
||
183 | 'file' => $exception->getFile(), |
||
184 | 'line' => $exception->getLine(), |
||
185 | ]; |
||
186 | |||
187 | $response = $response->withHeader('Content-Type', 'application/json'); |
||
188 | $response->getBody()->write(json_encode($error)); |
||
189 | } |
||
190 | |||
191 | $response = $this->responseModifiers->modify($response); |
||
192 | $request->sendResponse($response); |
||
193 | }; |
||
194 | |||
195 | $this->application->processRequest($serverRequest)->done($fulfilled, $rejected); |
||
196 | } |
||
197 | ); |
||
198 | |||
199 | $connection->on('data', [$request, 'feed']); |
||
200 | } |
||
201 | ); |
||
202 | } |
||
203 | } |
||
204 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: