SymbolFilterFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 2
rs 9.9
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\DependencyGuard\Php\Filter;
8
9
use Composer\Composer;
10
11
class SymbolFilterFactory implements SymbolFilterFactoryInterface
12
{
13
    /**
14
     * Create a symbol filter for the given Composer instance.
15
     *
16
     * @param Composer $composer
17
     *
18
     * @return SymbolFilterInterface
19
     */
20 3
    public function create(Composer $composer): SymbolFilterInterface
21
    {
22
        /** @var SymbolFilterInterface[] $filters */
23 3
        $filters = array_map(
24
            function (string $exclusion) : SymbolFilterInterface {
25
                $filters = [
26 1
                    new ExactSymbolFilter($exclusion),
27 1
                    new PatternSymbolFilter($exclusion)
28
                ];
29
30 1
                if (preg_match('#\\\\$#', $exclusion)) {
31 1
                    $filters[] = new NamespaceSymbolFilter($exclusion);
32
                }
33
34 1
                return new SymbolFilterChain(...$filters);
35 3
            },
36 3
            $this->getExclusions($composer)
37
        );
38
39 3
        $filters[] = new UserDefinedSymbolFilter();
40
41 3
        return new SymbolFilterChain(...$filters);
42
    }
43
44
    /**
45
     * Get the exclusions from the Composer root package.
46
     *
47
     * @param Composer $composer
48
     *
49
     * @return string[]
50
     */
51 3
    private function getExclusions(Composer $composer): array
52
    {
53 3
        $extra = $composer->getPackage()->getExtra();
54
55 3
        return $extra['dependency-guard']['exclude'] ?? [];
56
    }
57
}
58