Skip to content

Commit 85b73af

Browse files
committed
Refactor ComposerPhpVersionFactory
allows re-use of version-string into PhpVersion parsing
1 parent 38ea1ab commit 85b73af

File tree

2 files changed

+68
-36
lines changed

2 files changed

+68
-36
lines changed

src/Php/ComposerPhpVersionFactory.php

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
namespace PHPStan\Php;
44

5-
use Composer\Semver\VersionParser;
6-
use Nette\Utils\Strings;
75
use PHPStan\DependencyInjection\AutowiredParameter;
86
use PHPStan\DependencyInjection\AutowiredService;
97
use PHPStan\Internal\ComposerHelper;
108
use function count;
119
use function end;
1210
use function is_string;
1311
use function min;
14-
use function sprintf;
1512

1613
#[AutowiredService]
1714
final class ComposerPhpVersionFactory
@@ -46,21 +43,26 @@ private function initializeVersions(): void
4643
return;
4744
}
4845

49-
$parser = new VersionParser();
50-
$constraint = $parser->parseConstraints($composerPhpVersion);
51-
52-
if (!$constraint->getLowerBound()->isZero()) {
53-
$minVersion = $this->buildVersion($constraint->getLowerBound()->getVersion(), false);
54-
55-
if ($minVersion !== null) {
56-
$this->minVersion = new PhpVersion($minVersion->getVersionId());
46+
$parser = new ComposerPhpVersionParser();
47+
[$minVersion, $maxVersion] = $parser->parse($composerPhpVersion, static function (string $version, int $versionId, bool $isMaxVersion): PhpVersion {
48+
if ($isMaxVersion && $version === '6.0.0.0-dev') {
49+
$versionId = min($versionId, PhpVersionFactory::MAX_PHP5_VERSION);
50+
} elseif ($isMaxVersion && $version === '8.0.0.0-dev') {
51+
$versionId = min($versionId, PhpVersionFactory::MAX_PHP7_VERSION);
52+
} else {
53+
$versionId = min($versionId, PhpVersionFactory::MAX_PHP_VERSION);
5754
}
55+
56+
return new PhpVersion($versionId);
57+
});
58+
if ($minVersion !== null) {
59+
$this->minVersion = new PhpVersion($minVersion->getVersionId());
5860
}
59-
if ($constraint->getUpperBound()->isPositiveInfinity()) {
61+
if ($maxVersion === null) {
6062
return;
6163
}
6264

63-
$this->maxVersion = $this->buildVersion($constraint->getUpperBound()->getVersion(), true);
65+
$this->maxVersion = $maxVersion;
6466
}
6567

6668
public function getMinVersion(): ?PhpVersion
@@ -99,27 +101,4 @@ private function getComposerRequireVersion(): ?string
99101
return $composerPhpVersion;
100102
}
101103

102-
private function buildVersion(string $version, bool $isMaxVersion): ?PhpVersion
103-
{
104-
$matches = Strings::match($version, '#^(\d+)\.(\d+)(?:\.(\d+))?#');
105-
if ($matches === null) {
106-
return null;
107-
}
108-
109-
$major = $matches[1];
110-
$minor = $matches[2];
111-
$patch = $matches[3] ?? 0;
112-
$versionId = (int) sprintf('%d%02d%02d', $major, $minor, $patch);
113-
114-
if ($isMaxVersion && $version === '6.0.0.0-dev') {
115-
$versionId = min($versionId, PhpVersionFactory::MAX_PHP5_VERSION);
116-
} elseif ($isMaxVersion && $version === '8.0.0.0-dev') {
117-
$versionId = min($versionId, PhpVersionFactory::MAX_PHP7_VERSION);
118-
} else {
119-
$versionId = min($versionId, PhpVersionFactory::MAX_PHP_VERSION);
120-
}
121-
122-
return new PhpVersion($versionId);
123-
}
124-
125104
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Php;
4+
5+
use Composer\Semver\VersionParser;
6+
use Nette\Utils\Strings;
7+
use function sprintf;
8+
9+
final class ComposerPhpVersionParser
10+
{
11+
12+
/**
13+
* @param callable(string, int, bool):PhpVersion $buildPhpVersion
14+
*
15+
* @return array{PhpVersion|null, PhpVersion|null}
16+
*/
17+
public function parse(string $version, callable $buildPhpVersion): array
18+
{
19+
$minVersion = null;
20+
21+
$parser = new VersionParser();
22+
$constraint = $parser->parseConstraints($version);
23+
24+
if (!$constraint->getLowerBound()->isZero()) {
25+
$minVersion = $this->buildVersion($constraint->getLowerBound()->getVersion(), false, $buildPhpVersion);
26+
}
27+
if ($constraint->getUpperBound()->isPositiveInfinity()) {
28+
return [ $minVersion, null ];
29+
}
30+
31+
$maxVersion = $this->buildVersion($constraint->getUpperBound()->getVersion(), true, $buildPhpVersion);
32+
return [ $minVersion, $maxVersion ];
33+
}
34+
35+
/**
36+
* @param callable(string, int, bool):PhpVersion $buildPhpVersion
37+
*/
38+
private function buildVersion(string $version, bool $isMaxVersion, callable $buildPhpVersion): ?PhpVersion
39+
{
40+
$matches = Strings::match($version, '#^(\d+)\.(\d+)(?:\.(\d+))?#');
41+
if ($matches === null) {
42+
return null;
43+
}
44+
45+
$major = $matches[1];
46+
$minor = $matches[2];
47+
$patch = $matches[3] ?? 0;
48+
$versionId = (int) sprintf('%d%02d%02d', $major, $minor, $patch);
49+
50+
return $buildPhpVersion($version, $versionId, $isMaxVersion);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)