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\Constraint\ComparisonConstraint; |
11
|
|
|
use Version\Constraint\CompositeConstraint; |
12
|
|
|
use Version\Constraint\ConstraintInterface; |
13
|
|
|
use Version\Version; |
14
|
|
|
use Version\VersionsCollection; |
15
|
|
|
use function array_values; |
16
|
|
|
use function iterator_to_array; |
17
|
|
|
|
18
|
|
|
final class PickLastMinorVersionFromCollection implements PickVersionFromVersionCollection |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
* @throws LogicException |
23
|
|
|
* @throws RuntimeException |
24
|
|
|
*/ |
25
|
|
|
public function forVersions(VersionsCollection $versions) : Version |
26
|
|
|
{ |
27
|
|
|
Assert::that($versions->count()) |
28
|
|
|
->greaterThan(0, 'Cannot determine latest minor version from an empty collection'); |
29
|
|
|
|
30
|
|
|
$stableVersions = $versions->matching(new class implements ConstraintInterface { |
31
|
|
|
public function assert(Version $version) : bool |
32
|
|
|
{ |
33
|
|
|
return ! $version->isPreRelease(); |
34
|
|
|
} |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$versionsSortedDescending = $stableVersions->sortedDescending(); |
38
|
|
|
|
39
|
|
|
/** @var Version $lastVersion */ |
40
|
|
|
$lastVersion = array_values(iterator_to_array($versionsSortedDescending))[0]; |
41
|
|
|
|
42
|
|
|
$matchingMinorVersions = $stableVersions |
43
|
|
|
->matching(new CompositeConstraint( |
44
|
|
|
CompositeConstraint::OPERATOR_AND, |
45
|
|
|
new ComparisonConstraint(ComparisonConstraint::OPERATOR_LTE, $lastVersion), |
46
|
|
|
new ComparisonConstraint( |
47
|
|
|
ComparisonConstraint::OPERATOR_GTE, |
48
|
|
|
Version::fromString($lastVersion->getMajor() . '.' . $lastVersion->getMinor() . '.0') |
49
|
|
|
) |
50
|
|
|
)) |
51
|
|
|
->sortedAscending(); |
52
|
|
|
|
53
|
|
|
/** @var Version[] $matchingMinorVersionsAsArray */ |
54
|
|
|
$matchingMinorVersionsAsArray = array_values(iterator_to_array($matchingMinorVersions)); |
55
|
|
|
|
56
|
|
|
return $matchingMinorVersionsAsArray[0]; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|