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