Completed
Push — master ( d83206...aa4419 )
by Marco
10s
created

GetVersionCollectionFromGitRepository   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromRepository() 0 12 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare\Git;
5
6
use Symfony\Component\Process\Process;
7
use Version\VersionsCollection;
8
9
final class GetVersionCollectionFromGitRepository implements GetVersionCollection
10
{
11
    /**
12
     * {@inheritDoc}
13
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
14
     * @throws \Symfony\Component\Process\Exception\LogicException
15
     * @throws \Symfony\Component\Process\Exception\RuntimeException
16
     */
17
    public function fromRepository(CheckedOutRepository $checkedOutRepository) : VersionsCollection
18
    {
19
        $output = (new Process(['git', 'tag', '-l']))
20
            ->setWorkingDirectory((string)$checkedOutRepository)
21
            ->mustRun()
22
            ->getOutput();
23
24
        // @todo handle invalid versions more gracefully (drop them)
25
        return VersionsCollection::fromArray(array_filter(
26
            explode("\n", $output),
27
            function (string $maybeVersion) {
28
                return trim($maybeVersion) !== '';
29
            }
30
        ));
31
    }
32
}
33