1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace RazonYang\Yii\TranslatorMiddleware\LocaleParser; |
||
6 | |||
7 | use InvalidArgumentException; |
||
8 | use RazonYang\Yii\TranslatorMiddleware\LocaleParserInterface; |
||
9 | use Psr\Http\Message\ServerRequestInterface; |
||
10 | |||
11 | /** |
||
12 | * CompositeParser implements the LocaleParserInterface that supports multiple parsers. |
||
13 | */ |
||
14 | final class CompositeParser implements LocaleParserInterface |
||
15 | { |
||
16 | private array $parsers = []; |
||
17 | 10 | public function __construct( |
|
18 | LocaleParserInterface ...$parsers, |
||
19 | ) { |
||
20 | 10 | if (!$parsers) { |
|
0 ignored issues
–
show
|
|||
21 | 1 | throw new InvalidArgumentException('No parser specified.'); |
|
22 | } |
||
23 | |||
24 | 9 | $this->parsers = $parsers; |
|
25 | } |
||
26 | |||
27 | 9 | public function parse(ServerRequestInterface $request): ?string |
|
28 | { |
||
29 | 9 | foreach ($this->parsers as $parser) { |
|
30 | 9 | $locale = $parser->parse($request); |
|
31 | |||
32 | 9 | if ($locale) { |
|
33 | 7 | return $locale; |
|
34 | } |
||
35 | } |
||
36 | |||
37 | 2 | return null; |
|
38 | } |
||
39 | } |
||
40 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.