forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposerPhpVersionParser.php
More file actions
53 lines (42 loc) · 1.4 KB
/
ComposerPhpVersionParser.php
File metadata and controls
53 lines (42 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php declare(strict_types = 1);
namespace PHPStan\Php;
use Composer\Semver\VersionParser;
use Nette\Utils\Strings;
use function sprintf;
final class ComposerPhpVersionParser
{
/**
* @param callable(string, int, bool):PhpVersion $buildPhpVersion
*
* @return array{PhpVersion|null, PhpVersion|null}
*/
public function parse(string $version, callable $buildPhpVersion): array
{
$minVersion = null;
$parser = new VersionParser();
$constraint = $parser->parseConstraints($version);
if (!$constraint->getLowerBound()->isZero()) {
$minVersion = $this->buildVersion($constraint->getLowerBound()->getVersion(), false, $buildPhpVersion);
}
if ($constraint->getUpperBound()->isPositiveInfinity()) {
return [ $minVersion, null ];
}
$maxVersion = $this->buildVersion($constraint->getUpperBound()->getVersion(), true, $buildPhpVersion);
return [ $minVersion, $maxVersion ];
}
/**
* @param callable(string, int, bool):PhpVersion $buildPhpVersion
*/
private function buildVersion(string $version, bool $isMaxVersion, callable $buildPhpVersion): ?PhpVersion
{
$matches = Strings::match($version, '#^(\d+)\.(\d+)(?:\.(\d+))?#');
if ($matches === null) {
return null;
}
$major = $matches[1];
$minor = $matches[2];
$patch = $matches[3] ?? 0;
$versionId = (int) sprintf('%d%02d%02d', $major, $minor, $patch);
return $buildPhpVersion($version, $versionId, $isMaxVersion);
}
}