1 | <?php declare(strict_types=1); |
||
13 | class I18nHandler |
||
14 | { |
||
15 | const REGEX_LOCALE = '#^/(?P<locale>[a-z]{2}[-_][a-zA-Z]{2})(?:/|$)#'; |
||
16 | |||
17 | /** @var Translator$translator */ |
||
18 | private $translator; |
||
19 | |||
20 | /** @var array $supportedLocales */ |
||
21 | private $supportedLocales; |
||
22 | |||
23 | /** @var string $defaultLocale */ |
||
24 | private $defaultLocale; |
||
25 | |||
26 | /** |
||
27 | * InternationalisationMiddleware constructor. |
||
28 | * @param $helper |
||
29 | * @param string|null $defaultLocale |
||
30 | */ |
||
31 | 4 | public function __construct(Translator $translator, array $supportedLocales, string $defaultLocale) |
|
37 | |||
38 | /** |
||
39 | * @param ServerRequestInterface $request |
||
40 | * @return ServerRequestInterface |
||
41 | * @throws NotFoundException |
||
42 | */ |
||
43 | 3 | public function handleI18n(ServerRequestInterface $request): ServerRequestInterface |
|
44 | { |
||
45 | 3 | $uri = $request->getUri(); |
|
46 | 3 | $path = $uri->getPath(); |
|
47 | |||
48 | 3 | if (! preg_match(self::REGEX_LOCALE, $path, $matches)) { |
|
49 | 1 | $locale = Locale::canonicalize($this->defaultLocale); |
|
|
|||
50 | 1 | Locale::setDefault($this->defaultLocale); |
|
51 | |||
52 | 1 | return $request; |
|
53 | } |
||
54 | |||
55 | 2 | $locale = $matches['locale']; |
|
56 | |||
57 | 2 | if (in_array($locale, $this->supportedLocales)) { |
|
58 | 1 | $locale = Locale::canonicalize($locale); |
|
59 | 1 | Locale::setDefault($locale); |
|
60 | 1 | $this->translator->setLocale($locale); |
|
61 | 1 | $path = substr($path, strlen($locale) + 1); |
|
62 | 1 | $uri = $uri->withPath($path); |
|
63 | 1 | $request = $request->withUri($uri); |
|
64 | } |
||
65 | |||
66 | 2 | return $request; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param ServerRequestInterface $request |
||
71 | * @return ServerRequestInterface |
||
72 | */ |
||
73 | 1 | public function removeI18n(ServerRequestInterface $request): ServerRequestInterface |
|
89 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.