CompositeParser::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
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
Bug Best Practice introduced by
The expression $parsers of type array<integer,RazonYang\...\LocaleParserInterface> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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