From 005eed3d06042940abdd58e6b3ab77edcf428329 Mon Sep 17 00:00:00 2001 From: Nitamet <13808955+Nitamet@users.noreply.github.com> Date: Sat, 19 Aug 2023 00:14:39 +0300 Subject: [PATCH] Throw and catch exception when specified PHP version is invalid --- src/Psalm/Internal/Analyzer/ProjectAnalyzer.php | 15 +++------------ src/Psalm/Internal/CliUtils.php | 11 ++++++++++- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 96168a311fe..6be055ea43b 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -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; diff --git a/src/Psalm/Internal/CliUtils.php b/src/Psalm/Internal/CliUtils.php index 1e5a1abdd6e..86f6b3261a3 100644 --- a/src/Psalm/Internal/CliUtils.php +++ b/src/Psalm/Internal/CliUtils.php @@ -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; @@ -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); + } } }