1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Merge pull request #10129 from Nitamet/10084-incorrect-version-checking

This commit is contained in:
Bruce Weirdan 2023-08-18 23:44:31 +02:00 committed by GitHub
commit 0922fa14d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -200,6 +200,10 @@ class ProjectAnalyzer
UnnecessaryVarAnnotation::class,
];
private const PHP_VERSION_REGEX = '^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\..*)?$';
private const PHP_SUPPORTED_VERSIONS_REGEX = '^(5\.[456]|7\.[01234]|8\.[0123])(\..*)?$';
/**
* @param array<ReportOptions> $generated_report_options
*/
@ -1179,8 +1183,16 @@ class ProjectAnalyzer
*/
public function setPhpVersion(string $version, string $source): void
{
if (!preg_match('/^(5\.[456]|7\.[01234]|8\.[012])(\..*)?$/', $version)) {
throw new UnexpectedValueException('Expecting a version number in the format x.y');
if (!preg_match('/' . self::PHP_VERSION_REGEX . '/', $version)) {
throw new UnexpectedValueException('Expecting a version number in the format x.y or x.y.z');
}
if (!preg_match('/' . self::PHP_SUPPORTED_VERSIONS_REGEX . '/', $version)) {
throw new UnexpectedValueException(
'Psalm supports PHP version ">=5.4". The specified version '
. $version
. " is either not supported or doesn't exist.",
);
}
[$php_major_version, $php_minor_version] = explode('.', $version);

View File

@ -12,6 +12,7 @@ use Psalm\Exception\ConfigNotFoundException;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Report;
use RuntimeException;
use UnexpectedValueException;
use function array_filter;
use function array_key_exists;
@ -485,7 +486,15 @@ final class CliUtils
}
if ($version !== null && $source !== null) {
$project_analyzer->setPhpVersion($version, $source);
try {
$project_analyzer->setPhpVersion($version, $source);
} catch (UnexpectedValueException $e) {
fwrite(
STDERR,
$e->getMessage() . PHP_EOL,
);
exit(2);
}
}
}