Passed
Push — master ( e86da3...7acf89 )
by BruceScrutinizer
09:02
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace NamespaceProtector\Config;
4
5
use NamespaceProtector\Common\PathInterface;
6
use Webmozart\Assert\Assert;
7
8
final class Config
9
{
10
    public const MODE_MAKE_VENDOR_PRIVATE = 'MODE_MAKE_VENDOR_PRIVATE';
11
    public const MODE_PUBLIC = 'PUBLIC';
12
13
    public const PLOTTER_TERMINAL = 'plotter-terminal';
14
    public const PLOTTER_PNG = 'plotter-png';
15
16
    /** @var PathInterface */
17
    private $pathStart;
18
19
    /** @var PathInterface  */
20
    private $pathComposerJson;
21
22
    /** @var array<string> */
23
    private $privateEntries;
24
25
    /** @var array<string> */
26
    private $publicEntries;
27
28
    /** @var string */
29
    private $mode;
30
31
    /** @var string */
32
    private $version;
33
34
    /** @var bool */
35
    private $enabledCache;
36
37
    /** @var string */
38
    private $plotter;
39
40
    /**
41
     * @param array<string> $privateEntries
42
     * @param array<string> $publicEntries
43
     */
44
    public function __construct(
45
        string $version,
46
        PathInterface $pathStart,
47
        PathInterface $pathComposerJson,
48
        array $privateEntries,
49
        array $publicEntries,
50
        string $mode = self::MODE_PUBLIC,
51
        bool $enabledCache = false,
52
        string $plotter = self::PLOTTER_TERMINAL
53
    ) {
54
        $this->version = $version;
55
        $this->pathStart = $pathStart;
56
        $this->pathComposerJson = $pathComposerJson;
57
        $this->privateEntries = $privateEntries;
58
        $this->publicEntries = $publicEntries;
59
        $this->mode = $mode;
60
        $this->enabledCache = $enabledCache;
61
        $this->plotter = $plotter;
62
    }
63
64
    public function getStartPath(): PathInterface
65
    {
66
        return $this->pathStart;
67
    }
68
69
    /**
70
     * @return array<string>
71
     */
72
    public function getPrivateEntries(): array
73
    {
74
        return $this->privateEntries;
75
    }
76
77
    /**
78
     * @return array<string>
79
     */
80
    public function getPublicEntries(): array
81
    {
82
        return $this->publicEntries;
83
    }
84
85
    public function getMode(): string
86
    {
87
        return $this->mode;
88
    }
89
90
    public function getPlotter(): string
91
    {
92
        return $this->plotter;
93
    }
94
95
    public function getPathComposerJson(): PathInterface
96
    {
97
        return $this->pathComposerJson;
98
    }
99
100
    /** @param array<string,string> $parameters */
101
    public function cloneWithWithOverride(self $config, array $parameters): self
102
    {
103
        $self = new self(
104
            $config->getVersion(),
105
            $config->getStartPath(),
106
            $config->getPathComposerJson(),
107
            $config->getPrivateEntries(),
108
            $config->getPublicEntries(),
109
            $config->getMode(),
110
            $config->enabledCache(),
111
            $parameters['plotter'] ?? $config->getPlotter(),
112
        );
113
114
        $self->validateLoadedConfig(); //todo: validation called multiple times
115
116
        return $self;
117
    }
118
119
    public function validateLoadedConfig(): void
120
    {
121
        Assert::inArray($this->getMode(), [self::MODE_PUBLIC, self::MODE_MAKE_VENDOR_PRIVATE], 'Mode not valid');
122
        Assert::eq('0.1.0', $this->getVersion(), 'Version not valid');
123
        Assert::directory($this->getStartPath()->get(), 'Start directory not valid');
124
        Assert::directory($this->getPathComposerJson()->get(), 'Composer json directory not valid');
125
        Assert::boolean($this->enabledCache(), 'Cache flag must be boolean');
126
        Assert::inArray($this->getPlotter(), [self::PLOTTER_TERMINAL, self::PLOTTER_PNG], 'Plotter not valid');
127
    }
128
129
    public function getVersion(): string
130
    {
131
        //todo: use https://github.com/nikolaposa/version
132
        return $this->version;
133
    }
134
135
    public function enabledCache(): bool
136
    {
137
        return $this->enabledCache;
138
    }
139
}
140