CompositeMatcher::match()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 12
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 8
rs 8.4444
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