Conditions | 9 |
Paths | 24 |
Total Lines | 52 |
Code Lines | 30 |
Lines | 20 |
Ratio | 38.46 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
164 | public function handleRequest(Request $request, Response $response) |
||
165 | { |
||
166 | $requestPath = $request->getPath(); |
||
167 | $filePath = $this->resolvePath($requestPath); |
||
168 | |||
169 | if ($this->logger !== null) { |
||
170 | $this->logger->debug('Got HTTP request (request path: {requestPath}, resolved path: {resolvedPath})', [ |
||
171 | 'requestPath' => $requestPath, |
||
172 | 'resolvedPath' => $filePath, |
||
173 | ]); |
||
174 | } |
||
175 | |||
176 | if ($this->authenticationHandler instanceof HandlerInterface) { |
||
177 | if (!$this->authenticationHandler->handle($request)) { |
||
178 | if ($this->logger !== null) { |
||
179 | $this->logger->warning('Client failed authentication'); |
||
180 | } |
||
181 | |||
182 | $this->authenticationHandler->requireAuthentication($response); |
||
183 | |||
184 | return; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | if (file_exists($filePath)) { |
||
189 | if (is_readable($filePath)) { |
||
190 | $response->writeHead(200, [ |
||
191 | 'Content-Type' => $this->getContentType($filePath), |
||
|
|||
192 | ]); |
||
193 | |||
194 | $response->end(file_get_contents($filePath)); |
||
195 | View Code Duplication | } else { |
|
196 | if ($this->logger !== null) { |
||
197 | $this->logger->error('HTTP request failed, file unreadable ({filePath})', [ |
||
198 | 'filePath' => $filePath, |
||
199 | ]); |
||
200 | } |
||
201 | |||
202 | $response->writeHead(403, ['Content-Type' => 'text/plain']); |
||
203 | $response->end("Forbidden\n"); |
||
204 | } |
||
205 | View Code Duplication | } else { |
|
206 | if ($this->logger !== null) { |
||
207 | $this->logger->error('HTTP request failed, file not found ({filePath})', [ |
||
208 | 'filePath' => $filePath, |
||
209 | ]); |
||
210 | } |
||
211 | |||
212 | $response->writeHead(404, ['Content-Type' => 'text/plain']); |
||
213 | $response->end("Not found\n"); |
||
214 | } |
||
215 | } |
||
216 | |||
269 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.