Passed
Push — master ( 351308...878789 )
by Alexander
02:24 queued 10s
created

GitExecutor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 16
c 2
b 0
f 0
dl 0
loc 67
ccs 24
cts 24
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setVersionTag() 0 3 1
A getLastTag() 0 5 1
A runCommand() 0 8 2
A addFile() 0 3 1
A status() 0 3 1
A commit() 0 3 1
A getCurrentBranch() 0 5 1
A getCommitDescription() 0 3 1
A getCommitsSinceLastTag() 0 5 2
A setConfig() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement;
6
7
use Vasoft\VersionIncrement\Contract\VcsExecutorInterface;
8
use Vasoft\VersionIncrement\Exceptions\GitCommandException;
9
10
class GitExecutor implements VcsExecutorInterface
11
{
12
    /**
13
     * @codeCoverageIgnore
14
     */
15
    public function setConfig(Config $config): void
16
    {
17
        // Do nothing
18
    }
19
20 1
    public function status(): array
21
    {
22 1
        return $this->runCommand('status --porcelain');
23
    }
24
25 1
    public function getCommitDescription(string $commitId): array
26
    {
27 1
        return $this->runCommand('log -1 --pretty=format:%b ' . $commitId);
28
    }
29
30 1
    public function addFile(string $file): void
31
    {
32 1
        $this->runCommand('add ' . $file);
33
    }
34
35 1
    public function setVersionTag(string $version): void
36
    {
37 1
        $this->runCommand('tag v' . $version);
38
    }
39
40 1
    public function commit(string $message): void
41
    {
42 1
        $this->runCommand("commit -am '" . $message . "'");
43
    }
44
45 2
    public function getCurrentBranch(): string
46
    {
47 2
        $branch = $this->runCommand('rev-parse --abbrev-ref HEAD');
48
49 1
        return trim($branch[0] ?? '');
50
    }
51
52 2
    public function getLastTag(): ?string
53
    {
54 2
        $tags = $this->runCommand('tag --sort=-creatordate');
55
56 2
        return $tags[0] ?? null;
57
    }
58
59 2
    public function getCommitsSinceLastTag(?string $lastTag): array
60
    {
61 2
        $command = $lastTag ? "log {$lastTag}..HEAD --pretty=format:\"%H %s\"" : 'log --pretty=format:"%H %s"';
62
63 2
        return $this->runCommand($command);
64
    }
65
66
    /**
67
     * @throws GitCommandException
68
     */
69 11
    private function runCommand(string $command): array
70
    {
71 11
        exec("git {$command} 2>&1", $output, $returnCode);
72 11
        if (0 !== $returnCode) {
73 1
            throw new GitCommandException($command, $output);
74
        }
75
76 10
        return $output;
77
    }
78
}
79