From 5f40d265392740af42d53d5688d1881bffce80a1 Mon Sep 17 00:00:00 2001 From: Nitamet <13808955+Nitamet@users.noreply.github.com> Date: Fri, 18 Aug 2023 20:09:17 +0300 Subject: [PATCH 1/4] Fix PHP version checking Separate condition to check both cases if the specified string is a valid version and if it is supported Remove unsupported versions up to 7.4 from regex --- .../Internal/Analyzer/ProjectAnalyzer.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index a418a52bc22..753497aa200 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -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 = '^(7\.4|8\.[012])(\..*)?$'; + /** * @param array $generated_report_options */ @@ -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 requires PHP version ">7.4". The specified version ' + . $version + . " is either not supported or doesn't exist." + . PHP_EOL, + ); + exit(1); + } + + [$php_major_version, $php_minor_version] = explode('.', $version); $php_major_version = (int) $php_major_version; From 2c7391a6492a52d2f15a8cadadc6679dbe04fede Mon Sep 17 00:00:00 2001 From: Nitamet <13808955+Nitamet@users.noreply.github.com> Date: Fri, 18 Aug 2023 20:32:05 +0300 Subject: [PATCH 2/4] Add versions 5.4-7.3 as supported --- src/Psalm/Internal/Analyzer/ProjectAnalyzer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 753497aa200..41add7140d4 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -202,7 +202,7 @@ class ProjectAnalyzer private const PHP_VERSION_REGEX = '^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\..*)?$'; - private const PHP_SUPPORTED_VERSIONS_REGEX = '^(7\.4|8\.[012])(\..*)?$'; + private const PHP_SUPPORTED_VERSIONS_REGEX = '^(5\.[456]|7\.[01234]|8\.[012])(\..*)?$'; /** * @param array $generated_report_options @@ -1195,7 +1195,7 @@ public function setPhpVersion(string $version, string $source): void if (!preg_match('/' . self::PHP_SUPPORTED_VERSIONS_REGEX . '/', $version)) { fwrite( STDERR, - 'Psalm requires PHP version ">7.4". The specified version ' + 'Psalm supports PHP version ">=5.4". The specified version ' . $version . " is either not supported or doesn't exist." . PHP_EOL, From 18b74b181424a94b460639b66b42c7e091e78f68 Mon Sep 17 00:00:00 2001 From: Nitamet <13808955+Nitamet@users.noreply.github.com> Date: Sat, 19 Aug 2023 00:01:17 +0300 Subject: [PATCH 3/4] Add 8.3 as supported version --- src/Psalm/Internal/Analyzer/ProjectAnalyzer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 41add7140d4..96168a311fe 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -202,7 +202,7 @@ class ProjectAnalyzer 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])(\..*)?$'; + private const PHP_SUPPORTED_VERSIONS_REGEX = '^(5\.[456]|7\.[01234]|8\.[0123])(\..*)?$'; /** * @param array $generated_report_options 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 4/4] 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); + } } }