| Conditions | 7 |
| Paths | 46 |
| Total Lines | 66 |
| Code Lines | 39 |
| 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 |
||
| 111 | public function dispatch(Controller $controller, string $methodName): array { |
||
| 112 | $out = [null, [], null]; |
||
| 113 | |||
| 114 | try { |
||
| 115 | // prefill reflector with everything thats needed for the |
||
| 116 | // middlewares |
||
| 117 | $this->reflector->reflect($controller, $methodName); |
||
| 118 | |||
| 119 | $this->middlewareDispatcher->beforeController($controller, |
||
| 120 | $methodName); |
||
| 121 | |||
| 122 | $databaseStatsBefore = []; |
||
| 123 | if ($this->config->getSystemValueBool('debug', false)) { |
||
| 124 | $databaseStatsBefore = $this->connection->getStats(); |
||
|
|
|||
| 125 | } |
||
| 126 | |||
| 127 | $response = $this->executeController($controller, $methodName); |
||
| 128 | |||
| 129 | if (!empty($databaseStatsBefore)) { |
||
| 130 | $databaseStatsAfter = $this->connection->getStats(); |
||
| 131 | $numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built']; |
||
| 132 | $numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed']; |
||
| 133 | |||
| 134 | if ($numBuilt > 50) { |
||
| 135 | $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.' , [ |
||
| 136 | 'class' => (string) get_class($controller), |
||
| 137 | 'method' => $methodName, |
||
| 138 | 'count' => $numBuilt, |
||
| 139 | ]); |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($numExecuted > 100) { |
||
| 143 | $this->logger->warning('Controller {class}::{method} executed {count} queries.' , [ |
||
| 144 | 'class' => (string) get_class($controller), |
||
| 145 | 'method' => $methodName, |
||
| 146 | 'count' => $numExecuted, |
||
| 147 | ]); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | // if an exception appears, the middleware checks if it can handle the |
||
| 152 | // exception and creates a response. If no response is created, it is |
||
| 153 | // assumed that theres no middleware who can handle it and the error is |
||
| 154 | // thrown again |
||
| 155 | } catch (\Exception $exception) { |
||
| 156 | $response = $this->middlewareDispatcher->afterException( |
||
| 157 | $controller, $methodName, $exception); |
||
| 158 | } catch (\Throwable $throwable) { |
||
| 159 | $exception = new \Exception($throwable->getMessage(), $throwable->getCode(), $throwable); |
||
| 160 | $response = $this->middlewareDispatcher->afterException( |
||
| 161 | $controller, $methodName, $exception); |
||
| 162 | } |
||
| 163 | |||
| 164 | $response = $this->middlewareDispatcher->afterController( |
||
| 165 | $controller, $methodName, $response); |
||
| 166 | |||
| 167 | // depending on the cache object the headers need to be changed |
||
| 168 | $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
||
| 169 | $out[1] = array_merge($response->getHeaders()); |
||
| 170 | $out[2] = $response->getCookies(); |
||
| 171 | $out[3] = $this->middlewareDispatcher->beforeOutput( |
||
| 172 | $controller, $methodName, $response->render() |
||
| 173 | ); |
||
| 174 | $out[4] = $response; |
||
| 175 | |||
| 176 | return $out; |
||
| 177 | } |
||
| 242 |