Passed
Push — master ( d69420...736410 )
by Alexander
02:23
created

ShortParser::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement\Commits;
6
7
use Vasoft\VersionIncrement\Config;
8
use Vasoft\VersionIncrement\Contract\CommitParserInterface;
9
use Vasoft\VersionIncrement\Exceptions\ChangesNotFoundException;
10
use Vasoft\VersionIncrement\Exceptions\ConfigNotSetException;
11
use Vasoft\VersionIncrement\Exceptions\GitCommandException;
12
13
final class ShortParser implements CommitParserInterface
14
{
15
    public const REG_EXP = "/^[\t *-]*((?<key>[a-z]+)(?:\\((?<scope>[^)]+)\\))?(?<breaking>!)?:\\s+(?<message>.+))/";
16
    private ?Config $config = null;
17
18 26
    public function setConfig(Config $config): void
19
    {
20 26
        $this->config = $config;
21
    }
22
23
    /**
24
     * @throws ChangesNotFoundException
25
     * @throws ConfigNotSetException
26
     * @throws GitCommandException
27
     */
28 26
    public function process(
29
        ?string $tagsFrom,
30
        string $tagsTo = '',
31
    ): CommitCollection {
32 26
        if (null === $this->config) {
33 1
            throw new ConfigNotSetException();
34
        }
35 25
        $vcs = $this->config->getVcsExecutor();
36 25
        $commits = $vcs->getCommitsSinceLastTag($tagsFrom);
37 25
        if (empty($commits)) {
38 1
            throw new ChangesNotFoundException();
39
        }
40 24
        $commitCollection = $this->config->getCommitCollection();
41 24
        $aggregateKey = $this->config->getAggregateSection();
42 24
        $shouldProcessDefaultSquashedCommit = $this->config->shouldProcessDefaultSquashedCommit();
43 24
        $squashedCommitMessage = $this->config->getSquashedCommitMessage();
44 24
        foreach ($commits as $commit) {
45 24
            if (preg_match(
46 24
                '/^(?<hash>[^ ]+) (?<commit>.+)/',
47 24
                $commit,
48 24
                $matches,
49 24
            )) {
50 24
                $hash = $matches['hash'];
51 24
                $commit = $matches['commit'];
52
                if (
53 24
                    $shouldProcessDefaultSquashedCommit
54 24
                    && str_ends_with($commit, $squashedCommitMessage)
55
                ) {
56 2
                    $this->processAggregated($hash, $commitCollection);
57
58 2
                    continue;
59
                }
60 24
                if (!$this->parseCommit($commit, $commitCollection, $aggregateKey, $hash)) {
61 1
                    $commitCollection->addRawMessage($commit);
62
                }
63
            }
64
        }
65
66 24
        return $commitCollection;
67
    }
68
69
    /**
70
     * @throws GitCommandException
71
     */
72 24
    private function parseCommit(
73
        string $line,
74
        CommitCollection $commitCollection,
75
        string $aggregateKey,
76
        string $hash,
77
    ): bool {
78 24
        $matches = [];
79 24
        if (preg_match(self::REG_EXP, $line, $matches)) {
80 23
            $key = trim($matches['key']);
81 23
            if ('' !== $hash && '' !== $aggregateKey && $aggregateKey === $key) {
82 1
                $commitCollection->setMajorMarker('!' === $matches['breaking']);
83 1
                $this->processAggregated($hash, $commitCollection);
84
            } else {
85 23
                $commitCollection->add(
86 23
                    new Commit(
87 23
                        $line,
88 23
                        $key,
89 23
                        $matches['message'],
90 23
                        '!' === $matches['breaking'],
91 23
                        $matches['scope'],
92 23
                        [$matches['breaking']],
93 23
                    ),
94 23
                );
95
            }
96
97 23
            return true;
98
        }
99
100 4
        return false;
101
    }
102
103
    /**
104
     * @param string $hash
105
     * @param CommitCollection $commitCollection
106
     * @throws ConfigNotSetException
107
     * @throws GitCommandException
108
     */
109 3
    private function processAggregated(string $hash, CommitCollection $commitCollection): void
110
    {
111 3
        if (null === $this->config) {
112
            throw new ConfigNotSetException();
113
        }
114 3
        $description = $this->config->getVcsExecutor()->getCommitDescription($hash);
115 3
        foreach ($description as $line) {
116 3
            $this->parseCommit($line, $commitCollection, '', '');
117
        }
118
    }
119
}
120