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

PickLastMinorVersionFromCollection::forVersions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
nc 3
nop 1
dl 0
loc 18
c 0
b 0
f 0
cc 3
rs 9.4285
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare\Git;
5
6
use Assert\Assert;
7
use Version\Version;
8
use Version\VersionsCollection;
9
10
final class PickLastMinorVersionFromCollection implements PickVersionFromVersionCollection
11
{
12
    /**
13
     * {@inheritDoc}
14
     * @throws \Symfony\Component\Process\Exception\LogicException
15
     * @throws \Symfony\Component\Process\Exception\RuntimeException
16
     */
17
    public function forVersions(VersionsCollection $versions) : Version
18
    {
19
        Assert::that($versions->count())->greaterOrEqualThan(1);
20
21
        $versions->sort(VersionsCollection::SORT_DESC);
22
23
        /** @var Version $lastVersion */
24
        $lastVersion = $versions->getIterator()->current();
25
        $previousVersionInIteration = $lastVersion;
26
        /** @var Version $version */
27
        foreach ($versions as $version) {
28
            if ($lastVersion->getMinor() !== $version->getMinor()) {
29
                return $previousVersionInIteration;
30
            }
31
            $previousVersionInIteration = $version;
32
        }
33
34
        return $version;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $version seems to be defined by a foreach iteration on line 27. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
35
    }
36
}
37