Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nicer PHP version checking #10129

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\.[012])(\..*)?$';
weirdan marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param array<ReportOptions> $generated_report_options
*/
Expand Down Expand Up @@ -1179,10 +1183,27 @@ public function refactorCodeAfterCompletion(array $to_refactor): void
*/
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)) {
fwrite(
STDERR,
'Expecting a version number in the format x.y or x.y.z'
. PHP_EOL,
);
exit(1);
}

if (!preg_match('/' . self::PHP_SUPPORTED_VERSIONS_REGEX . '/', $version)) {
fwrite(
STDERR,
'Psalm supports PHP version ">=5.4". The specified version '
. $version
. " is either not supported or doesn't exist."
. PHP_EOL,
);
exit(1);
}
weirdan marked this conversation as resolved.
Show resolved Hide resolved


[$php_major_version, $php_minor_version] = explode('.', $version);

$php_major_version = (int) $php_major_version;
Expand Down