Conditions | 10 |
Paths | 13 |
Total Lines | 59 |
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 |
||
119 | public function prepareExceptionViewModel(MvcEvent $e): void |
||
120 | { |
||
121 | // Do nothing if no error in the event |
||
122 | $error = $e->getError(); |
||
123 | if (empty($error)) { |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | // Do nothing if the result is a response object |
||
128 | $result = $e->getResult(); |
||
129 | if ($result instanceof Response) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | // Proceed to showing an error page with or without exception |
||
134 | switch ($error) { |
||
135 | case Application::ERROR_CONTROLLER_NOT_FOUND: |
||
136 | case Application::ERROR_CONTROLLER_INVALID: |
||
137 | case Application::ERROR_ROUTER_NO_MATCH: |
||
138 | // Specifically not handling these |
||
139 | return; |
||
140 | |||
141 | case Application::ERROR_EXCEPTION: |
||
142 | default: |
||
143 | // check if there really is an exception |
||
144 | // ZF also throws normal errors, for example: error-route-unauthorized |
||
145 | // if there is no exception we have nothing to log |
||
146 | if ($e->getParam('exception') == null) { |
||
147 | return; |
||
148 | } |
||
149 | |||
150 | // Log exception to sentry by triggering an exception event |
||
151 | $eventID = $e->getApplication()->getEventManager()->trigger('logException', $this, ['exception' => $e->getParam('exception')]); |
||
152 | |||
153 | $model = new ViewModel( |
||
154 | [ |
||
155 | 'message' => sprintf($this->defaultExceptionMessage, $eventID->last()), |
||
156 | 'exception' => $e->getParam('exception'), |
||
157 | 'display_exceptions' => $this->displayExceptions(), |
||
158 | ] |
||
159 | ); |
||
160 | $model->setTemplate($this->getExceptionTemplate()); |
||
161 | $e->setResult($model); |
||
162 | |||
163 | $response = $e->getResponse(); |
||
164 | if (!$response) { |
||
165 | $response = new HttpResponse(); |
||
166 | $response->setStatusCode(500); |
||
167 | $e->setResponse($response); |
||
168 | } else { |
||
169 | $statusCode = $response->getStatusCode(); |
||
170 | if ($statusCode === 200) { |
||
171 | $response->setStatusCode(500); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | break; |
||
176 | } |
||
177 | } |
||
178 | } |