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