CompositeMatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 57
ccs 18
cts 18
cp 1
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
B match() 0 23 8
A any() 0 3 1
A all() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Files\PathMatcher;
6
7
/**
8
 * Composite matcher allows combining several matchers.
9
 */
10
final class CompositeMatcher implements PathMatcherInterface
11
{
12
    /**
13
     * @var PathMatcherInterface[]
14
     */
15
    private array $matchers;
16
17 14
    private function __construct(
18
        private bool $matchAny,
19
        PathMatcherInterface ...$matchers
20
    ) {
21 14
        $this->matchers = $matchers;
22
    }
23
24
    /**
25
     * Get an instance of composite matcher that gives a match if any of sub-matchers match.
26
     *
27
     * @param PathMatcherInterface ...$matchers Matchers to check.
28
     */
29 7
    public static function any(PathMatcherInterface ...$matchers): self
30
    {
31 7
        return new self(true, ...$matchers);
32
    }
33
34
    /**
35
     * Get an instance of composite matcher that gives a match only if all of sub-matchers match.
36
     *
37
     * @param PathMatcherInterface ...$matchers Matchers to check.
38
     */
39 7
    public static function all(PathMatcherInterface ...$matchers): self
40
    {
41 7
        return new self(false, ...$matchers);
42
    }
43
44 14
    public function match(string $path): ?bool
45
    {
46 14
        $allNulls = true;
47
48 14
        foreach ($this->matchers as $matcher) {
49 14
            $match = $matcher->match($path);
50
51 14
            if ($match === null) {
52 6
                continue;
53
            }
54
55 12
            $allNulls = false;
56
57 12
            if ($this->matchAny && $match) {
58 4
                return true;
59
            }
60
61 9
            if (!$this->matchAny && !$match) {
62 4
                return false;
63
            }
64
        }
65
66 6
        return $allNulls ? null : !$this->matchAny;
67
    }
68
}
69