Passed
Pull Request — master (#44)
by Alexander
02:11
created

CompositeMatcher   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 19
c 1
b 0
f 0
dl 0
loc 63
ccs 20
cts 20
cp 1
rs 10

4 Methods

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