| Conditions | 10 |
| Paths | 13 |
| Total Lines | 60 |
| 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 |
||
| 94 | public function prepareExceptionViewModel(MvcEvent $e) |
||
| 95 | { |
||
| 96 | // Do nothing if no error in the event |
||
| 97 | $error = $e->getError(); |
||
| 98 | if (empty($error)) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Do nothing if the result is a response object |
||
| 103 | $result = $e->getResult(); |
||
| 104 | if ($result instanceof Response) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | // Proceed to showing an error page with or without exception |
||
| 109 | switch ($error) { |
||
| 110 | case Application::ERROR_CONTROLLER_NOT_FOUND: |
||
| 111 | case Application::ERROR_CONTROLLER_INVALID: |
||
| 112 | case Application::ERROR_ROUTER_NO_MATCH: |
||
| 113 | // Specifically not handling these |
||
| 114 | return; |
||
| 115 | |||
| 116 | case Application::ERROR_EXCEPTION: |
||
| 117 | default: |
||
| 118 | // check if there really is an exception |
||
| 119 | // ZF also throws normal errors, for example: error-route-unauthorized |
||
| 120 | // if there is no exception we have nothing to log |
||
| 121 | if ($e->getParam('exception') == null) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Log exception to sentry by triggering an exception event |
||
| 126 | $eventID = $e->getApplication()->getEventManager()->trigger('logException', $this, ['exception' => $e->getParam('exception')]); |
||
| 127 | |||
| 128 | $model = new ViewModel( |
||
| 129 | [ |
||
| 130 | 'message' => sprintf($this->defaultExceptionMessage, $eventID->last()), |
||
| 131 | 'exception' => $e->getParam('exception'), |
||
| 132 | 'display_exceptions' => $this->displayExceptions(), |
||
| 133 | ] |
||
| 134 | ); |
||
| 135 | $model->setTemplate($this->getExceptionTemplate()); |
||
| 136 | $e->setResult($model); |
||
| 137 | |||
| 138 | /** @var HttpResponse $response */ |
||
| 139 | $response = $e->getResponse(); |
||
| 140 | if (!$response) { |
||
| 141 | $response = new HttpResponse(); |
||
| 142 | $response->setStatusCode(500); |
||
| 143 | $e->setResponse($response); |
||
| 144 | } else { |
||
| 145 | $statusCode = $response->getStatusCode(); |
||
| 146 | if ($statusCode === 200) { |
||
| 147 | $response->setStatusCode(500); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | break; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 187 | } |