Skip to content

Commit

Permalink
Throw and catch exception when specified PHP version is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitamet committed Aug 18, 2023
1 parent 18b74b1 commit 005eed3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
15 changes: 3 additions & 12 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,26 +1184,17 @@ public function refactorCodeAfterCompletion(array $to_refactor): void
public function setPhpVersion(string $version, string $source): void
{
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);
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)) {
fwrite(
STDERR,
throw new UnexpectedValueException(
'Psalm supports PHP version ">=5.4". The specified version '
. $version
. " is either not supported or doesn't exist."
. PHP_EOL,
. " is either not supported or doesn't exist.",
);
exit(1);
}


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

$php_major_version = (int) $php_major_version;
Expand Down
11 changes: 10 additions & 1 deletion src/Psalm/Internal/CliUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Report;
use RuntimeException;
use UnexpectedValueException;

use function array_filter;
use function array_key_exists;
Expand Down Expand Up @@ -485,7 +486,15 @@ public static function initPhpVersion(array $options, Config $config, ProjectAna
}

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);
}
}
}

Expand Down

0 comments on commit 005eed3

Please sign in to comment.