fromRepository()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 11
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\Git;
6
7
use Symfony\Component\Process\Exception\LogicException;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Exception\RuntimeException;
10
use Symfony\Component\Process\Process;
11
use Version\Exception\InvalidVersionString;
12
use Version\Version;
13
use Version\VersionCollection;
14
use function array_filter;
15
use function array_map;
16
use function explode;
17
18
final class GetVersionCollectionFromGitRepository implements GetVersionCollection
19
{
20
    /**
21
     * {@inheritDoc}
22
     *
23
     * @throws ProcessFailedException
24
     * @throws LogicException
25
     * @throws RuntimeException
26
     */
27
    public function fromRepository(CheckedOutRepository $checkedOutRepository) : VersionCollection
28
    {
29
        $output = (new Process(['git', 'tag', '-l']))
30
            ->setWorkingDirectory($checkedOutRepository->__toString())
31
            ->mustRun()
32
            ->getOutput();
33
34
        return new VersionCollection(...array_filter(
35
            array_map(static function (string $maybeVersion) : ?Version {
36
                try {
37
                    return Version::fromString($maybeVersion);
38
                } catch (InvalidVersionString $e) {
39
                    return null;
40
                }
41
            }, explode("\n", $output))
42
        ));
43
    }
44
}
45