Conditions | 16 |
Paths | 19 |
Total Lines | 71 |
Code Lines | 49 |
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 |
||
38 | public function render($request, Exception $exception) |
||
39 | { |
||
40 | if ($request->is('api/*') || $request->wantsJson()) { |
||
41 | if ('web' !== config('core.api.error_format')) { |
||
42 | $response = []; |
||
43 | $exceptionClass = get_class($exception); |
||
44 | $exception = $this->overwrite($exception); |
||
45 | |||
46 | if ($exception instanceof ModelNotFoundException) { |
||
47 | $response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_NOT_FOUND; |
||
48 | $response['meta'][self::MESSAGE] = $exception->getMessage(); |
||
49 | } elseif ($exception instanceof JWTException) { |
||
50 | switch ($exceptionClass) { |
||
51 | case InvalidClaimException::class: |
||
52 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::INVALID_CLAIM; |
||
53 | break; |
||
54 | |||
55 | case PayloadException::class: |
||
56 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::PAYLOAD; |
||
57 | break; |
||
58 | |||
59 | case TokenBlacklistedException::class: |
||
60 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::TOKEN_BLACKLISTED; |
||
61 | break; |
||
62 | |||
63 | case TokenExpiredException::class: |
||
64 | if ('Token has expired and can no longer be refreshed' === $exception->getMessage()) { |
||
65 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::CAN_NOT_REFRESHED; |
||
66 | } else { |
||
67 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::TOKEN_EXPIRED; |
||
68 | } |
||
69 | break; |
||
70 | |||
71 | case TokenInvalidException::class: |
||
72 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::TOKEN_INVALID; |
||
73 | break; |
||
74 | |||
75 | case UserNotDefinedException::class: |
||
76 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::USER_NOT_DEFINED; |
||
77 | break; |
||
78 | |||
79 | default: |
||
80 | $response['meta'][self::ERROR_CODE] = JWTErrorCode::DEFAULT;; |
||
81 | } |
||
82 | $response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_UNAUTHORIZED; |
||
83 | $response['meta'][self::MESSAGE] = $exception->getMessage(); |
||
84 | } else { |
||
85 | if (method_exists($exception, 'getStatusCode')) { |
||
86 | $response['meta'][self::STATUS_CODE] = $exception->getStatusCode(); |
||
87 | } else { |
||
88 | $response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_INTERNAL_SERVER_ERROR; |
||
89 | } |
||
90 | |||
91 | if (method_exists($exception, 'getCode')) { |
||
92 | $response['meta'][self::ERROR_CODE] = $exception->getCode(); |
||
93 | } |
||
94 | |||
95 | if (null === $exception->getMessage()) { |
||
96 | $response['meta'][self::MESSAGE] = class_basename($exceptionClass); |
||
97 | } else { |
||
98 | $response['meta'][self::MESSAGE] = $exception->getMessage(); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | $response = $this->debug($response, $exception); |
||
103 | |||
104 | return $this->response(collect($response)->toArray()); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return parent::render($request, $exception); |
||
109 | } |
||
136 |