| Conditions | 10 |
| Paths | 11 |
| Total Lines | 70 |
| 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 |
||
| 154 | public function prepareExceptionViewModel(MvcEvent $e) |
||
| 155 | { |
||
| 156 | // Do nothing if no error in the event |
||
| 157 | $error = $e->getError(); |
||
| 158 | if (empty($error)) { |
||
| 159 | return; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Do nothing if the result is a response object |
||
| 163 | $result = $e->getResult(); |
||
| 164 | if ($result instanceof ResponseInterface) { |
||
| 165 | return; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Proceed to showing an error page with or without exception |
||
| 169 | switch ($error) { |
||
| 170 | case Application::ERROR_CONTROLLER_NOT_FOUND: |
||
| 171 | case Application::ERROR_CONTROLLER_INVALID: |
||
| 172 | case Application::ERROR_ROUTER_NO_MATCH: |
||
| 173 | // Specifically not handling these |
||
| 174 | return; |
||
| 175 | |||
| 176 | case Application::ERROR_EXCEPTION: |
||
| 177 | default: |
||
| 178 | // Prepare error message |
||
| 179 | $exception = $e->getParam('exception'); |
||
| 180 | |||
| 181 | // Log exception to sentry by triggering an exception event |
||
| 182 | $e->getApplication()->getEventManager()->trigger('logException', $this, ['exception' => $exception]); |
||
| 183 | |||
| 184 | if (\is_callable($this->message)) { |
||
| 185 | $callback = $this->message; |
||
| 186 | $message = (string)$callback($exception, $this->displayExceptions); |
||
| 187 | } elseif ($this->displayExceptions && $exception instanceof \Exception) { |
||
| 188 | /* @var $exception \Exception */ |
||
| 189 | $message = str_replace( |
||
| 190 | [ |
||
| 191 | ':className', |
||
| 192 | ':message', |
||
| 193 | ':code', |
||
| 194 | ':file', |
||
| 195 | ':line', |
||
| 196 | ':stack', |
||
| 197 | ':previous', |
||
| 198 | ], [ |
||
| 199 | \get_class($exception), |
||
| 200 | $exception->getMessage(), |
||
| 201 | $exception->getCode(), |
||
| 202 | $exception->getFile(), |
||
| 203 | $exception->getLine(), |
||
| 204 | $exception->getTraceAsString(), |
||
| 205 | $exception->getPrevious(), |
||
| 206 | ], |
||
| 207 | $this->message |
||
| 208 | ); |
||
| 209 | } else { |
||
| 210 | $message = $this->defaultExceptionMessage; |
||
| 211 | } |
||
| 212 | |||
| 213 | // Prepare view model |
||
| 214 | $model = new ConsoleModel(); |
||
| 215 | $model->setResult($message); |
||
| 216 | $model->setErrorLevel(1); |
||
| 217 | |||
| 218 | // Inject it into MvcEvent |
||
| 219 | $e->setResult($model); |
||
| 220 | |||
| 221 | break; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |