| Conditions | 5 |
| Paths | 8 |
| Total Lines | 55 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 50 | public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface |
||
| 51 | { |
||
| 52 | $authenticated = false; |
||
|
|
|||
| 53 | |||
| 54 | // 1. Check for secure scheme |
||
| 55 | |||
| 56 | // 2. Fetch token from server request |
||
| 57 | |||
| 58 | $tokenProvider = new ServerRequestLazyChainProvider($request, [ |
||
| 59 | [ServerRequestAuthBearerProvider::class => [ |
||
| 60 | 'httpHeader' => ServerRequestAuthBearerProvider::DEFAULT_OPTIONS['httpHeader'], |
||
| 61 | 'httpHeaderPrefix' => ServerRequestAuthBearerProvider::DEFAULT_OPTIONS['httpHeaderPrefix'], |
||
| 62 | ]], |
||
| 63 | [ServerRequestCookieProvider::class => [ |
||
| 64 | 'cookieName' => ServerRequestCookieProvider::DEFAULT_OPTIONS['cookieName'] |
||
| 65 | ]] |
||
| 66 | ]); |
||
| 67 | |||
| 68 | $plainToken = $tokenProvider->getPlainToken(); |
||
| 69 | |||
| 70 | // 3. Validate the token |
||
| 71 | if ($plainToken !== null) { |
||
| 72 | try { |
||
| 73 | $token = $this->jwtService->parsePlainToken($plainToken); |
||
| 74 | |||
| 75 | if ($token->verify($this->jwtService->getSigner(), $this->jwtService->getPrivateKey())) { |
||
| 76 | if ($token->isExpired()) { |
||
| 77 | $message = 'Token has expired'; |
||
| 78 | } else { |
||
| 79 | $authenticated = true; |
||
| 80 | // log Something ? |
||
| 81 | $response = $delegate->process($request->withAttribute(self::class, $token)); |
||
| 82 | // do something with the response (writing cookie, refresh token ?) |
||
| 83 | return $response; |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $message = 'Token is invalid'; |
||
| 87 | } |
||
| 88 | } catch (\Throwable $e) { |
||
| 89 | // log something ? |
||
| 90 | $message = 'Token error'; |
||
| 91 | } |
||
| 92 | } else { |
||
| 93 | $message = 'No token provided'; |
||
| 94 | } |
||
| 95 | |||
| 96 | // @todo: ask the correct way with PSR-15 ? |
||
| 97 | $error = new JsonResponse([ |
||
| 98 | 'message' => 'Unauthorized.', |
||
| 99 | 'reason' => $message, |
||
| 100 | 'code' => 401 |
||
| 101 | ], 401, []); |
||
| 102 | |||
| 103 | return $error; |
||
| 104 | } |
||
| 105 | } |
||
| 106 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.