1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement\Commits; |
6
|
|
|
|
7
|
|
|
final class CommitCollection |
8
|
|
|
{ |
9
|
|
|
private bool $majorMarker = false; |
10
|
|
|
private bool $minorMarker = false; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param array<string,Section> $sections |
14
|
|
|
*/ |
15
|
16 |
|
public function __construct( |
16
|
|
|
private readonly array $sections, |
17
|
|
|
private readonly Section $defaultSection, |
18
|
16 |
|
) {} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Adds a commit to the collection. |
22
|
|
|
* |
23
|
|
|
* If the commit type exists in the collection, it is added to the corresponding section. |
24
|
|
|
* Otherwise, it is added to the default section. |
25
|
|
|
*/ |
26
|
14 |
|
public function add(Commit $commit): void |
27
|
|
|
{ |
28
|
14 |
|
$this->detectMarkers($commit); |
29
|
14 |
|
$rawMessage = false; |
30
|
14 |
|
$detectedKey = $this->detectionSection($commit, $rawMessage); |
31
|
14 |
|
if ($rawMessage) { |
32
|
12 |
|
$this->addRawMessage($commit->rawMessage); |
33
|
|
|
|
34
|
12 |
|
return; |
35
|
|
|
} |
36
|
14 |
|
if ($detectedKey !== $commit->type) { |
37
|
2 |
|
$commit = $commit->withType($detectedKey); |
38
|
2 |
|
$this->detectMarkers($commit); |
39
|
|
|
} |
40
|
14 |
|
$section = $this->sections[$commit->type] ?? $this->defaultSection; |
41
|
14 |
|
if (!$section->hidden) { |
42
|
14 |
|
$section->addCommit($commit); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
14 |
|
private function detectMarkers(Commit $commit): void |
47
|
|
|
{ |
48
|
|
|
|
49
|
14 |
|
if ($commit->breakingChange) { |
50
|
2 |
|
$this->majorMarker = true; |
51
|
|
|
|
52
|
2 |
|
return; |
53
|
|
|
} |
54
|
14 |
|
$section = $this->sections[$commit->type] ?? null; |
55
|
14 |
|
if ($section) { |
56
|
14 |
|
if ($section->isMajorMarker) { |
57
|
1 |
|
$this->majorMarker = true; |
58
|
14 |
|
} elseif ($section->isMinorMarker) { |
59
|
13 |
|
$this->minorMarker = true; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds a commit to the default section. |
66
|
|
|
* |
67
|
|
|
* This method performs the following steps: |
68
|
|
|
* 1. Checks if the default section is not hidden. |
69
|
|
|
* 2. Creates a new Commit object with the processed message and adds it to the default section. |
70
|
|
|
*/ |
71
|
13 |
|
public function addRawMessage(string $message): void |
72
|
|
|
{ |
73
|
13 |
|
if (!$this->defaultSection->hidden) { |
74
|
13 |
|
$this->defaultSection->addCommit( |
75
|
13 |
|
new Commit( |
76
|
13 |
|
$message, |
77
|
13 |
|
$this->defaultSection->type, |
78
|
13 |
|
trim($message), |
79
|
13 |
|
false, |
80
|
13 |
|
), |
81
|
13 |
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns non-empty sections of the commit collection. |
87
|
|
|
* |
88
|
|
|
* @return array<string, Section> |
89
|
|
|
*/ |
90
|
15 |
|
public function getVisibleSections(): array |
91
|
|
|
{ |
92
|
15 |
|
return array_filter( |
93
|
15 |
|
$this->sections, |
94
|
15 |
|
static fn(Section $section): bool => !$section->isEmpty() && !$section->hidden, |
95
|
15 |
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
14 |
|
private function detectionSection( |
99
|
|
|
Commit $commit, |
100
|
|
|
bool &$rawMessage, |
101
|
|
|
): string { |
102
|
14 |
|
foreach ($this->sections as $index => $section) { |
103
|
14 |
|
foreach ($section->rules as $rule) { |
104
|
14 |
|
if ($rule($commit->type, $commit->scope, $commit->flags, $commit->comment)) { |
105
|
14 |
|
return $index; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
12 |
|
$rawMessage = true; |
110
|
|
|
|
111
|
12 |
|
return $this->defaultSection->type; |
112
|
|
|
} |
113
|
|
|
|
114
|
16 |
|
public function hasMajorMarker(): bool |
115
|
|
|
{ |
116
|
|
|
|
117
|
16 |
|
return $this->majorMarker; |
118
|
|
|
} |
119
|
|
|
|
120
|
12 |
|
public function hasMinorMarker(): bool |
121
|
|
|
{ |
122
|
12 |
|
return $this->minorMarker; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function setMajorMarker(bool $majorMarker): void |
126
|
|
|
{ |
127
|
2 |
|
if ($majorMarker) { |
128
|
1 |
|
$this->majorMarker = true; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|