php$0 ➔ forVersions()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 24
rs 9.536

1 Method

Rating   Name   Duplication   Size   Complexity  
A PickLastMinorVersionFromCollection.php$0 ➔ assert() 0 3 1
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\Comparison\Constraint\CompositeConstraint;
11
use Version\Comparison\Constraint\Constraint;
12
use Version\Comparison\Constraint\OperationConstraint;
13
use Version\Version;
14
use Version\VersionCollection;
15
16
final class PickLastMinorVersionFromCollection implements PickVersionFromVersionCollection
17
{
18
    /**
19
     * {@inheritDoc}
20
     *
21
     * @throws LogicException
22
     * @throws RuntimeException
23
     */
24
    public function forVersions(VersionCollection $versions) : Version
25
    {
26
        Assert::that($versions->count())
27
            ->greaterThan(0, 'Cannot determine latest minor version from an empty collection');
28
29
        $stableVersions = $versions->matching(new class implements Constraint {
30
            public function assert(Version $version) : bool
31
            {
32
                return ! $version->isPreRelease();
33
            }
34
        });
35
36
        $versionsSortedDescending = $stableVersions->sortedDescending();
37
38
        $lastVersion = $versionsSortedDescending->first();
39
40
        $matchingMinorVersions = $stableVersions
41
            ->matching(CompositeConstraint::and(
42
                OperationConstraint::lessOrEqualTo($lastVersion),
43
                OperationConstraint::greaterOrEqualTo(Version::fromString($lastVersion->getMajor() . '.' . $lastVersion->getMinor() . '.0'))
44
            ))
45
            ->sortedAscending();
46
47
        return $matchingMinorVersions->first();
48
    }
49
}
50