Passed
Pull Request — master (#50)
by Marco
02:46
created

PickLastMinorVersionFromCollection   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A forVersions() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\Git;
6
7
use Assert\Assert;
8
use Symfony\Component\Process\Exception\LogicException;
9
use Symfony\Component\Process\Exception\RuntimeException;
10
use Version\Version;
11
use Version\VersionsCollection;
12
13
final class PickLastMinorVersionFromCollection implements PickVersionFromVersionCollection
14
{
15
    /**
16
     * {@inheritDoc}
17
     * @throws LogicException
18
     * @throws RuntimeException
19
     */
20
    public function forVersions(VersionsCollection $versions) : Version
21
    {
22
        Assert::that($versions->count())->greaterOrEqualThan(1);
23
24
        $versions->sort(VersionsCollection::SORT_DESC);
25
26
        /** @var Version $lastVersion */
27
        $lastVersion                = $versions->getIterator()->current();
28
        $previousVersionInIteration = $lastVersion;
29
        /** @var Version $version */
30
        foreach ($versions as $version) {
31
            if ($lastVersion->getMinor() !== $version->getMinor()) {
32
                return $previousVersionInIteration;
33
            }
34
            $previousVersionInIteration = $version;
35
        }
36
37
        return $previousVersionInIteration;
38
    }
39
}
40