| Conditions | 12 |
| Paths | 74 |
| Total Lines | 46 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 declare(strict_types=1); |
||
| 28 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 29 | { |
||
| 30 | $uri = $request->getUri(); |
||
| 31 | $path = $uri->getPath(); |
||
| 32 | $prefix = $this->prefix; |
||
| 33 | $auto = $prefix === null; |
||
| 34 | $length = $auto ? 0 : strlen($prefix); |
||
| 35 | |||
| 36 | if ($auto) { |
||
| 37 | // automatically checks that the project is in a subfolder |
||
| 38 | // and URI contains a prefix |
||
| 39 | $scriptName = $request->getServerParams()['SCRIPT_NAME']; |
||
| 40 | if (strpos($scriptName, '/', 1) !== false) { |
||
| 41 | $tmpPrefix = substr($scriptName, 0, strrpos($scriptName, '/')); |
||
| 42 | if (strpos($path, $tmpPrefix) === 0) { |
||
| 43 | $prefix = $tmpPrefix; |
||
| 44 | $length = strlen($prefix); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } elseif ($length > 0) { |
||
| 48 | if ($prefix[-1] === '/') { |
||
| 49 | throw new BadUriPrefixException('Wrong URI prefix value'); |
||
| 50 | } |
||
| 51 | if (strpos($path, $prefix) !== 0) { |
||
| 52 | throw new BadUriPrefixException('URI prefix does not match'); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($length > 0) { |
||
| 57 | $newPath = substr($path, $length); |
||
| 58 | if ($newPath === '') { |
||
| 59 | $newPath = '/'; |
||
| 60 | } |
||
| 61 | if ($newPath[0] !== '/') { |
||
| 62 | if (!$auto) { |
||
| 63 | throw new BadUriPrefixException('URI prefix does not match completely'); |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | $request = $request->withUri($uri->withPath($newPath)); |
||
| 67 | $this->uriGenerator->setUriPrefix($prefix); |
||
|
|
|||
| 68 | // rewrite alias |
||
| 69 | $this->aliases->set('@web', $prefix . '/'); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | return $handler->handle($request); |
||
| 74 | } |
||
| 76 |