| Conditions | 10 |
| Paths | 33 |
| Total Lines | 46 |
| Code Lines | 23 |
| 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 |
||
| 76 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 77 | { |
||
| 78 | $language = null; |
||
| 79 | $uri = $request->getUri(); |
||
| 80 | |||
| 81 | //Use path |
||
| 82 | if ($this->usePath) { |
||
| 83 | $path = ltrim($uri->getPath(), '/'); |
||
| 84 | $dirs = explode('/', $path, 2); |
||
| 85 | $first = strtolower(array_shift($dirs)); |
||
| 86 | |||
| 87 | if (!empty($first) && in_array($first, $this->languages, true)) { |
||
| 88 | $language = $first; |
||
| 89 | |||
| 90 | //remove the language in the path |
||
| 91 | $request = $request->withUri($uri->withPath('/'.array_shift($dirs))); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | //Use http headers |
||
| 96 | if ($language === null) { |
||
| 97 | $language = $this->negotiateHeader($request->getHeaderLine('Accept-Language'), new Negotiator(), $this->languages); |
||
| 98 | |||
| 99 | if (empty($language)) { |
||
| 100 | $language = isset($this->languages[0]) ? $this->languages[0] : ''; |
||
| 101 | } |
||
| 102 | |||
| 103 | //Redirect to a path with the language |
||
| 104 | if ($this->redirectStatus !== false && $this->usePath) { |
||
| 105 | $path = Utils\Helpers::joinPath($language, $uri->getPath()); |
||
| 106 | |||
| 107 | return $this->getRedirectResponse($request, $uri->withPath($path), $response); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $response = $next( |
||
| 112 | self::setAttribute($request, self::KEY, $language), |
||
| 113 | $response->withHeader('Content-Language', $language) |
||
| 114 | ); |
||
| 115 | |||
| 116 | if (!$response->hasHeader('Content-Language')) { |
||
| 117 | $response = $response->withHeader('Content-Language', $language); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $response; |
||
| 121 | } |
||
| 122 | } |
||
| 123 |