Passed
Push — master ( 351308...878789 )
by Alexander
02:24 queued 10s
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\GitCommandException;
11
12
final class ShortParser implements CommitParserInterface
13
{
14
    public const REG_EXP = "/^[\t *-]*((?<key>[a-z]+)(?:\\((?<scope>[^)]+)\\))?(?<breaking>!)?:\\s+(?<message>.+))/";
15
    private ?Config $config = null;
16
17 24
    public function setConfig(Config $config): void
18
    {
19 24
        $this->config = $config;
20
    }
21
22
    /**
23
     * @throws ChangesNotFoundException
24
     * @throws GitCommandException
25
     */
26 23
    public function process(
27
        ?string $tagsFrom,
28
        string $tagsTo = '',
29
    ): CommitCollection {
30 23
        $vcs = $this->config->getVcsExecutor();
0 ignored issues
show
Bug introduced by
The method getVcsExecutor() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        /** @scrutinizer ignore-call */ 
31
        $vcs = $this->config->getVcsExecutor();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31 23
        $commits = $vcs->getCommitsSinceLastTag($tagsFrom);
32 23
        if (empty($commits)) {
33 1
            throw new ChangesNotFoundException();
34
        }
35 22
        $commitCollection = $this->config->getCommitCollection();
36 22
        $aggregateKey = $this->config->getAggregateSection();
37 22
        $shouldProcessDefaultSquashedCommit = $this->config->shouldProcessDefaultSquashedCommit();
38 22
        $squashedCommitMessage = $this->config->getSquashedCommitMessage();
39 22
        foreach ($commits as $commit) {
40 22
            if (preg_match(
41 22
                '/^(?<hash>[^ ]+) (?<commit>.+)/',
42 22
                $commit,
43 22
                $matches,
44 22
            )) {
45 22
                $hash = $matches['hash'];
46 22
                $commit = $matches['commit'];
47
                if (
48 22
                    $shouldProcessDefaultSquashedCommit
49 22
                    && str_ends_with($commit, $squashedCommitMessage)
50
                ) {
51 2
                    $this->processAggregated($hash, $commitCollection);
52
53 2
                    continue;
54
                }
55 22
                if (!$this->parseCommit($commit, $commitCollection, $aggregateKey, $hash)) {
56 1
                    $commitCollection->addRawMessage($commit);
57
                }
58
            }
59
        }
60
61 22
        return $commitCollection;
62
    }
63
64
    /**
65
     * @throws GitCommandException
66
     */
67 22
    private function parseCommit(
68
        string $line,
69
        CommitCollection $commitCollection,
70
        string $aggregateKey,
71
        string $hash,
72
    ): bool {
73 22
        $matches = [];
74 22
        if (preg_match(self::REG_EXP, $line, $matches)) {
75 21
            $key = trim($matches['key']);
76 21
            if ('' !== $hash && '' !== $aggregateKey && $aggregateKey === $key) {
77 1
                $commitCollection->setMajorMarker('!' === $matches['breaking']);
78 1
                $this->processAggregated($hash, $commitCollection);
79
            } else {
80 21
                $commitCollection->add(
81 21
                    new Commit(
82 21
                        $line,
83 21
                        $key,
84 21
                        $matches['message'],
85 21
                        '!' === $matches['breaking'],
86 21
                        $matches['scope'],
87 21
                        [$matches['breaking']],
88 21
                    ),
89 21
                );
90
            }
91
92 21
            return true;
93
        }
94
95 4
        return false;
96
    }
97
98
    /**
99
     * @throws GitCommandException
100
     */
101 3
    private function processAggregated(string $hash, CommitCollection $commitCollection): void
102
    {
103 3
        $description = $this->config->getVcsExecutor()->getCommitDescription($hash);
104 3
        foreach ($description as $line) {
105 3
            $this->parseCommit($line, $commitCollection, '', '');
106
        }
107
    }
108
}
109