Passed
Push — master ( 6073e7...d981da )
by Alexander
02:02
created

Config::getSectionRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement;
6
7
use Vasoft\VersionIncrement\SectionRules\DefaultRule;
8
use Vasoft\VersionIncrement\SectionRules\SectionRuleInterface;
9
10
final class Config
11
{
12
    private string $squashedCommitMessage = 'Squashed commit of the following:';
13
    private bool $processDefaultSquashedCommit = false;
14
    private array $minorTypes = [
15
        'feat',
16
    ];
17
    private array $majorTypes = [];
18
    private array $sectionRules = [];
19
    public const DEFAULT_SECTION = 'other';
20
21
    private int $defaultOrder = 500;
22
    private bool $sored = true;
23
    private string $masterBranch = 'master';
24
    private string $releaseSection = 'chore';
25
26
    private string $releaseScope = 'release';
27
28
    private string $aggregateSection = '';
29
30
    private bool $ignoreUntrackedFiles = false;
31
    private array $sections = [
32
        'feat' => [
33
            'title' => 'New features',
34
            'order' => 10,
35
            'hidden' => false,
36
        ],
37
        'fix' => [
38
            'title' => 'Fixes',
39
            'order' => 20,
40
            'hidden' => false,
41
        ],
42
        'chore' => [
43
            'title' => 'Other changes',
44
            'order' => 30,
45
            'hidden' => false,
46
        ],
47
        'docs' => [
48
            'title' => 'Documentation',
49
            'order' => 40,
50
            'hidden' => false,
51
        ],
52
        'style' => [
53
            'title' => 'Styling',
54
            'order' => 50,
55
            'hidden' => false,
56
        ],
57
        'refactor' => [
58
            'title' => 'Refactoring',
59
            'order' => 60,
60
            'hidden' => false,
61
        ],
62
        'test' => [
63
            'title' => 'Tests',
64
            'order' => 70,
65
            'hidden' => false,
66
        ],
67
        'perf' => [
68
            'title' => 'Performance',
69
            'order' => 80,
70
            'hidden' => false,
71
        ],
72
        'ci' => [
73
            'title' => 'Configure CI',
74
            'order' => 90,
75
            'hidden' => false,
76
        ],
77
        'build' => [
78
            'title' => 'Change build system',
79
            'order' => 100,
80
            'hidden' => false,
81
        ],
82
        'other' => [
83
            'title' => 'Other',
84
            'order' => 110,
85
            'hidden' => false,
86
        ],
87
    ];
88
89 1
    public function setSections(array $sections): self
90
    {
91 1
        $this->sored = false;
92 1
        $this->sections = [];
93 1
        foreach ($sections as $key => $value) {
94 1
            $this->sections[$key] = [
95 1
                'title' => $value['title'] ?? $key,
96 1
                'order' => $value['order'] ?? ++$this->defaultOrder,
97 1
                'hidden' => $value['hidden'] ?? false,
98 1
            ];
99
        }
100
101 1
        return $this;
102
    }
103
104 2
    public function setSection(
105
        string $key,
106
        string $title,
107
        int $order = -1,
108
        ?bool $hidden = null,
109
    ): self {
110 2
        $this->sored = false;
111 2
        $exists = $this->sections[$key] ?? null;
112 2
        $this->sections[$key] = [
113 2
            'title' => $title,
114 2
            'order' => -1 === $order ? ($exists ? $exists['order'] : ++$this->defaultOrder) : $order,
115 2
            'hidden' => null !== $hidden ? $hidden : ($exists ? $exists['hidden'] : false),
116 2
        ];
117
118 2
        return $this;
119
    }
120
121 16
    public function getSectionIndex(): array
122
    {
123 16
        $this->sortSections();
124
125 16
        return array_fill_keys(array_keys($this->sections), []);
126
    }
127
128 18
    private function sortSections(): void
129
    {
130 18
        if (!$this->sored) {
131 3
            $this->checkDefaultSection();
132 3
            uasort($this->sections, static fn($a, $b) => $a['order'] <=> $b['order']);
133 3
            $this->sored = true;
134
        }
135
    }
136
137 2
    public function getSectionDescriptions(): array
138
    {
139 2
        $this->sortSections();
140 2
        $result = [];
141 2
        foreach ($this->sections as $key => $section) {
142 2
            $result[] = sprintf('%s - %s', $key, $section['title']);
143
        }
144
145 2
        return $result;
146
    }
147
148 15
    public function getSectionTitle(string $key): string
149
    {
150 15
        return $this->sections[$key]['title'] ?? $key;
151
    }
152
153 2
    public function isSectionHidden(string $key): bool
154
    {
155 2
        return $this->sections[$key]['hidden'] ?? false;
156
    }
157
158 1
    public function setReleaseSection(string $section): self
159
    {
160 1
        $this->releaseSection = $section;
161
162 1
        return $this;
163
    }
164
165 14
    public function getReleaseSection(): string
166
    {
167 14
        return isset($this->sections[$this->releaseSection]) ? $this->releaseSection : self::DEFAULT_SECTION;
168
    }
169
170 3
    private function checkDefaultSection(): void
171
    {
172 3
        if (!isset($this->sections[self::DEFAULT_SECTION])) {
173 1
            $maxOrder = 0;
174 1
            foreach ($this->sections as $section) {
175 1
                $maxOrder = max($maxOrder, $section['order']);
176
            }
177 1
            $this->sections[self::DEFAULT_SECTION] = [
178 1
                'title' => 'Other',
179 1
                'order' => $maxOrder + 1000,
180 1
                'hidden' => false,
181 1
            ];
182
        }
183
    }
184
185 17
    public function getMasterBranch(): string
186
    {
187 17
        return $this->masterBranch;
188
    }
189
190 3
    public function setMasterBranch(string $masterBranch): self
191
    {
192 3
        $this->masterBranch = $masterBranch;
193
194 3
        return $this;
195
    }
196
197 1
    public function setMinorTypes(array $minorTypes): self
198
    {
199 1
        $this->minorTypes = $minorTypes;
200
201 1
        return $this;
202
    }
203
204 1
    public function setMajorTypes(array $majorTypes): self
205
    {
206 1
        $this->majorTypes = $majorTypes;
207
208 1
        return $this;
209
    }
210
211 11
    public function getMajorTypes(): array
212
    {
213 11
        return $this->majorTypes;
214
    }
215
216 10
    public function getMinorTypes(): array
217
    {
218 10
        return $this->minorTypes;
219
    }
220
221 2
    public function setReleaseScope(string $releaseScope): self
222
    {
223 2
        $this->releaseScope = $releaseScope;
224
225 2
        return $this;
226
    }
227
228 13
    public function getReleaseScope(): string
229
    {
230 13
        return $this->releaseScope;
231
    }
232
233 2
    public function setIgnoreUntrackedFiles(bool $ignoreUntrackedFiles): self
234
    {
235 2
        $this->ignoreUntrackedFiles = $ignoreUntrackedFiles;
236
237 2
        return $this;
238
    }
239
240 16
    public function mastIgnoreUntrackedFiles(): bool
241
    {
242 16
        return $this->ignoreUntrackedFiles;
243
    }
244
245 2
    public function addSectionRule(string $key, SectionRuleInterface $rule): self
246
    {
247 2
        $this->sectionRules[$key][] = $rule;
248
249 2
        return $this;
250
    }
251
252 12
    public function getSectionRules(string $key): array
253
    {
254 12
        $this->sectionRules[$key]['default'] = new DefaultRule($key);
255
256 12
        return $this->sectionRules[$key];
257
    }
258
259 1
    public function setAggregateSection(string $aggregateSection): self
260
    {
261 1
        $this->aggregateSection = $aggregateSection;
262
263 1
        return $this;
264
    }
265
266 13
    public function getAggregateSection(): string
267
    {
268 13
        return $this->aggregateSection;
269
    }
270
271 1
    public function setSquashedCommitMessage(string $squashedCommitMessage): self
272
    {
273 1
        $this->squashedCommitMessage = $squashedCommitMessage;
274
275 1
        return $this;
276
    }
277
278 13
    public function getSquashedCommitMessage(): string
279
    {
280 13
        return $this->squashedCommitMessage;
281
    }
282
283 2
    public function setProcessDefaultSquashedCommit(bool $processDefaultSquashedCommit): self
284
    {
285 2
        $this->processDefaultSquashedCommit = $processDefaultSquashedCommit;
286
287 2
        return $this;
288
    }
289
290 13
    public function shouldProcessDefaultSquashedCommit(): bool
291
    {
292 13
        return $this->processDefaultSquashedCommit;
293
    }
294
}
295