Conditions | 11 |
Paths | 181 |
Total Lines | 65 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 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 |
||
65 | public function onKernelException(GetResponseForExceptionEvent $event) |
||
66 | { |
||
67 | $logRef = uniqid(); |
||
68 | |||
69 | try { |
||
70 | $exception = $event->getException(); |
||
71 | $request = $event->getRequest(); |
||
72 | $code = $exception->getCode(); |
||
73 | |||
74 | if ($exception instanceof InvalidParametersException) { |
||
75 | $severity = LogLevel::NOTICE; |
||
76 | $statusCode = Response::HTTP_BAD_REQUEST; |
||
77 | $vndError = $this->validationErrorFactory->create($request, $exception, $logRef); |
||
78 | } else { |
||
79 | if ($exception instanceof NotFoundHttpException) { |
||
80 | $statusCode = Response::HTTP_NOT_FOUND; |
||
81 | $severity = LogLevel::INFO; |
||
82 | } else { |
||
83 | if ($exception instanceof AuthenticationException) { |
||
84 | $statusCode = Response::HTTP_UNAUTHORIZED; |
||
85 | $severity = LogLevel::WARNING; |
||
86 | } else { |
||
87 | $is3Digits = strlen($code) === 3; |
||
88 | $class = (int)substr($code, 0, 1); |
||
89 | if (!$is3Digits) { |
||
90 | $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
||
91 | $severity = LogLevel::CRITICAL; |
||
92 | } else { |
||
93 | switch ($class) { |
||
94 | case 4: |
||
95 | $severity = LogLevel::NOTICE; |
||
96 | $statusCode = Response::HTTP_BAD_REQUEST; |
||
97 | break; |
||
98 | case 5: |
||
99 | $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
||
100 | $severity = LogLevel::ERROR; |
||
101 | break; |
||
102 | default: |
||
103 | $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
||
104 | $severity = LogLevel::CRITICAL; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | $message = Response::$statusTexts[$statusCode]; |
||
110 | $vndError = new VndError($message, $logRef); |
||
111 | $vndError->addLink('help', $request->get('_resource'), ['title' => 'Error Information']); |
||
112 | $vndError->addLink('about', $request->getUri(), ['title' => 'Error Information']); |
||
113 | } |
||
114 | |||
115 | $reference = $logRef ? " [logref $logRef]" : ''; |
||
116 | $event->setResponse(new VndErrorResponse($vndError, $statusCode)); |
||
117 | $this->logger->log($severity, "{$vndError->getMessage()}{$reference}: $exception"); |
||
118 | } catch (\PHPUnit_Framework_Exception $e) { |
||
119 | throw $e; |
||
120 | } catch (\PHPUnit_Framework_Error $e) { |
||
121 | throw $e; |
||
122 | } catch (\Exception $e) { |
||
123 | // A simpler response where less can go wrong |
||
124 | $message = "Error Handling Failure"; |
||
125 | $vndError = new VndError($message, $logRef); |
||
126 | $this->logger->log(LogLevel::CRITICAL, "$message [logref $logRef]: $e"); |
||
127 | $event->setResponse(new VndErrorResponse($vndError, Response::HTTP_INTERNAL_SERVER_ERROR)); |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 |