Passed
Pull Request — master (#44)
by Alexander
02:19
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
eloc 18
c 1
b 0
f 0
dl 0
loc 63
ccs 20
cts 20
cp 1
rs 10
wmc 11

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