| Conditions | 11 |
| Paths | 1 |
| Total Lines | 125 |
| Code Lines | 52 |
| 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 setupHandlers( |
||
| 95 | RequestInterface $request, |
||
| 96 | ResponseInterface $response, |
||
| 97 | callable $next |
||
| 98 | ) { |
||
| 99 | $container = $this->app()->getContainer(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * HTTP 404 (Not Found) handler. |
||
| 103 | * |
||
| 104 | * @param object|HandlerInterface $handler An error handler instance. |
||
| 105 | * @return HandlerInterface |
||
| 106 | */ |
||
| 107 | $container->extend('notFoundHandler', function ($handler, $container) { |
||
| 108 | $appConfig = $container['config']; |
||
| 109 | $adminConfig = $container['admin/config']; |
||
| 110 | if ($handler instanceof HandlerInterface) { |
||
| 111 | $config = $handler->createConfig($appConfig['handlers.defaults']); |
||
| 112 | $config->merge($adminConfig['handlers.defaults']); |
||
| 113 | |||
| 114 | if (!empty($adminConfig['handlers.notFound'])) { |
||
| 115 | $config->merge($adminConfig['handlers.notFound']); |
||
| 116 | } |
||
| 117 | |||
| 118 | $handler->setConfig($config)->init(); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $handler; |
||
| 122 | }); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * HTTP 405 (Not Allowed) handler. |
||
| 126 | * |
||
| 127 | * @param object|HandlerInterface $handler An error handler instance. |
||
| 128 | * @return HandlerInterface |
||
| 129 | */ |
||
| 130 | $container->extend('notAllowedHandler', function ($handler, $container) { |
||
| 131 | $appConfig = $container['config']; |
||
| 132 | $adminConfig = $container['admin/config']; |
||
| 133 | if ($handler instanceof HandlerInterface) { |
||
| 134 | $config = $handler->createConfig($appConfig['handlers.defaults']); |
||
| 135 | $config->merge($adminConfig['handlers.defaults']); |
||
| 136 | |||
| 137 | if (!empty($adminConfig['handlers.notAllowed'])) { |
||
| 138 | $config->merge($adminConfig['handlers.notAllowed']); |
||
| 139 | } |
||
| 140 | |||
| 141 | $handler->setConfig($config)->init(); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $handler; |
||
| 145 | }); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * HTTP 500 (Error) handler for PHP 7+ Throwables. |
||
| 149 | * |
||
| 150 | * @param object|HandlerInterface $handler An error handler instance. |
||
| 151 | * @return HandlerInterface |
||
| 152 | */ |
||
| 153 | $container->extend('phpErrorHandler', function ($handler, $container) { |
||
| 154 | $appConfig = $container['config']; |
||
| 155 | $adminConfig = $container['admin/config']; |
||
| 156 | if ($handler instanceof HandlerInterface) { |
||
| 157 | $config = $handler->createConfig($appConfig['handlers.defaults']); |
||
| 158 | $config->merge($adminConfig['handlers.defaults']); |
||
| 159 | |||
| 160 | if (!empty($adminConfig['handlers.phpError'])) { |
||
| 161 | $config->merge($adminConfig['handlers.phpError']); |
||
| 162 | } |
||
| 163 | |||
| 164 | $handler->setConfig($config)->init(); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $handler; |
||
| 168 | }); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * HTTP 500 (Error) handler. |
||
| 172 | * |
||
| 173 | * @param object|HandlerInterface $handler An error handler instance. |
||
| 174 | * @return HandlerInterface |
||
| 175 | */ |
||
| 176 | $container->extend('errorHandler', function ($handler, $container) { |
||
| 177 | $appConfig = $container['config']; |
||
| 178 | $adminConfig = $container['admin/config']; |
||
| 179 | if ($handler instanceof HandlerInterface) { |
||
| 180 | $config = $handler->createConfig($appConfig['handlers.defaults']); |
||
| 181 | $config->merge($adminConfig['handlers.defaults']); |
||
| 182 | |||
| 183 | if (!empty($adminConfig['handlers.error'])) { |
||
| 184 | $config->merge($adminConfig['handlers.error']); |
||
| 185 | } |
||
| 186 | |||
| 187 | $handler->setConfig($config)->init(); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $handler; |
||
| 191 | }); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * HTTP 503 (Service Unavailable) handler. |
||
| 195 | * |
||
| 196 | * This handler is not part of Slim. |
||
| 197 | * |
||
| 198 | * @param object|HandlerInterface $handler An error handler instance. |
||
| 199 | * @return HandlerInterface |
||
| 200 | */ |
||
| 201 | $container->extend('maintenanceHandler', function ($handler, $container) { |
||
| 202 | $appConfig = $container['config']; |
||
| 203 | $adminConfig = $container['admin/config']; |
||
| 204 | if ($handler instanceof HandlerInterface) { |
||
| 205 | $config = $handler->createConfig($appConfig['handlers.defaults']); |
||
| 206 | $config->merge($adminConfig['handlers.defaults']); |
||
| 207 | |||
| 208 | if (!empty($adminConfig['handlers.maintenance'])) { |
||
| 209 | $config->merge($adminConfig['handlers.maintenance']); |
||
| 210 | } |
||
| 211 | |||
| 212 | $handler->setConfig($config)->init(); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $handler; |
||
| 216 | }); |
||
| 217 | |||
| 218 | return $next($request, $response); |
||
| 219 | } |
||
| 221 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: