Passed
Push — master ( a6226b...f00568 )
by Sergei
02:59
created

Options   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 77
ccs 28
cts 28
cp 1
rs 10
c 1
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A containsWildcard() 0 3 1
A isOptional() 0 3 1
A normalizePath() 0 3 1
B __construct() 0 22 7
A isVariable() 0 3 1
A mergePlanFile() 0 3 1
A buildMergePlan() 0 3 1
A vendorOverrideLayerPackages() 0 3 1
A sourceDirectory() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Config;
6
7
use function is_array;
8
use function str_replace;
9
use function trim;
10
11
/**
12
 * @internal
13
 */
14
final class Options
15
{
16
    public const DEFAULT_MERGE_PLAN_FILE = '.merge-plan.php';
17
    public const DEFAULT_CONFIG_DIRECTORY = '';
18
    public const DEFAULT_VENDOR_DIRECTORY = 'vendor';
19
    public const DEFAULT_ENVIRONMENT = '/';
20
    public const ROOT_PACKAGE_NAME = '/';
21
    public const VENDOR_OVERRIDE_PACKAGE_NAME = '//';
22
23
    private string $mergePlanFile = self::DEFAULT_MERGE_PLAN_FILE;
24
    private bool $buildMergePlan = true;
25
    private array $vendorOverrideLayerPackages = [];
26
    private string $sourceDirectory = self::DEFAULT_CONFIG_DIRECTORY;
27
28 48
    public function __construct(array $extra)
29
    {
30 48
        if (!isset($extra['config-plugin-options']) || !is_array($extra['config-plugin-options'])) {
31 26
            return;
32
        }
33
34 46
        $options = $extra['config-plugin-options'];
35
36 46
        if (!empty($options['merge-plan-file'])) {
37 3
            $this->mergePlanFile = (string) $options['merge-plan-file'];
38
        }
39
40 46
        if (isset($options['build-merge-plan'])) {
41 34
            $this->buildMergePlan = (bool) $options['build-merge-plan'];
42
        }
43
44 46
        if (isset($options['vendor-override-layer'])) {
45 27
            $this->vendorOverrideLayerPackages = (array) $options['vendor-override-layer'];
46
        }
47
48 46
        if (isset($options['source-directory'])) {
49 34
            $this->sourceDirectory = $this->normalizePath((string) $options['source-directory']);
50
        }
51
    }
52
53 69
    public static function containsWildcard(string $file): bool
54
    {
55 69
        return str_contains($file, '*');
56
    }
57
58 69
    public static function isOptional(string $file): bool
59
    {
60 69
        return str_starts_with($file, '?');
61
    }
62
63 69
    public static function isVariable(string $file): bool
64
    {
65 69
        return str_starts_with($file, '$');
66
    }
67
68 17
    public function mergePlanFile(): string
69
    {
70 17
        return $this->mergePlanFile;
71
    }
72
73 30
    public function buildMergePlan(): bool
74
    {
75 30
        return $this->buildMergePlan;
76
    }
77
78 21
    public function vendorOverrideLayerPackages(): array
79
    {
80 21
        return $this->vendorOverrideLayerPackages;
81
    }
82
83 36
    public function sourceDirectory(): string
84
    {
85 36
        return $this->sourceDirectory;
86
    }
87
88 34
    private function normalizePath(string $value): string
89
    {
90 34
        return trim(str_replace('\\', '/', $value), '/');
91
    }
92
}
93