Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions src/Php/ComposerPhpVersionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace PHPStan\Php;

use Composer\Semver\VersionParser;
use Nette\Utils\Strings;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Internal\ComposerHelper;
use function count;
use function end;
use function is_string;
use function min;
use function sprintf;

#[AutowiredService]
final class ComposerPhpVersionFactory
Expand Down Expand Up @@ -46,21 +43,26 @@ private function initializeVersions(): void
return;
}

$parser = new VersionParser();
$constraint = $parser->parseConstraints($composerPhpVersion);

if (!$constraint->getLowerBound()->isZero()) {
$minVersion = $this->buildVersion($constraint->getLowerBound()->getVersion(), false);

if ($minVersion !== null) {
$this->minVersion = new PhpVersion($minVersion->getVersionId());
$parser = new ComposerPhpVersionParser();
[$minVersion, $maxVersion] = $parser->parse($composerPhpVersion, static function (string $version, int $versionId, bool $isMaxVersion): PhpVersion {
if ($isMaxVersion && $version === '6.0.0.0-dev') {
$versionId = min($versionId, PhpVersionFactory::MAX_PHP5_VERSION);
} elseif ($isMaxVersion && $version === '8.0.0.0-dev') {
$versionId = min($versionId, PhpVersionFactory::MAX_PHP7_VERSION);
} else {
$versionId = min($versionId, PhpVersionFactory::MAX_PHP_VERSION);
}

return new PhpVersion($versionId);
});
if ($minVersion !== null) {
$this->minVersion = new PhpVersion($minVersion->getVersionId());
}
if ($constraint->getUpperBound()->isPositiveInfinity()) {
if ($maxVersion === null) {
return;
}

$this->maxVersion = $this->buildVersion($constraint->getUpperBound()->getVersion(), true);
$this->maxVersion = $maxVersion;
}

public function getMinVersion(): ?PhpVersion
Expand Down Expand Up @@ -99,27 +101,4 @@ private function getComposerRequireVersion(): ?string
return $composerPhpVersion;
}

private function buildVersion(string $version, bool $isMaxVersion): ?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);

if ($isMaxVersion && $version === '6.0.0.0-dev') {
$versionId = min($versionId, PhpVersionFactory::MAX_PHP5_VERSION);
} elseif ($isMaxVersion && $version === '8.0.0.0-dev') {
$versionId = min($versionId, PhpVersionFactory::MAX_PHP7_VERSION);
} else {
$versionId = min($versionId, PhpVersionFactory::MAX_PHP_VERSION);
}

return new PhpVersion($versionId);
}

}
53 changes: 53 additions & 0 deletions src/Php/ComposerPhpVersionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}

}
Loading