Passed
Push — master ( 1f6501...911244 )
by butschster
33:53 queued 27:07
created

ProcessParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 29
c 1
b 0
f 0
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 3 1
A __construct() 0 3 1
A parseTags() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MonorepoBuilder;
6
7
use Symplify\MonorepoBuilder\Release\Process\ProcessRunner;
8
9
final class ProcessParser implements TagParserInterface
10
{
11
    private const COMMAND = ['git', 'tag', '-l', '--sort=committerdate'];
12
13
    public function __construct(
14
        private readonly ProcessRunner $processRunner
15
    ) {
16
    }
17
18
    /**
19
     * Returns null, when there are no local tags yet
20
     */
21
    public function parse(string $gitDirectory): array
22
    {
23
        return \array_filter($this->parseTags($this->processRunner->run(self::COMMAND, $gitDirectory)));
24
    }
25
26
    /**
27
     * @return string[]
28
     */
29
    private function parseTags(string $commandResult): array
30
    {
31
        $tags = \trim($commandResult);
32
33
        // Remove all "\r" chars in case the CLI env like the Windows OS.
34
        // Otherwise (ConEmu, git bash, mingw cli, e.g.), leave as is.
35
        $normalizedTags = \str_replace("\r", '', $tags);
36
37
        return \explode("\n", $normalizedTags);
38
    }
39
}
40