From 32445023d820fbed2a4aa9fac89b675b585d0ec4 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Thu, 17 Aug 2023 21:36:47 +0200 Subject: [PATCH 1/7] `strip_tags()/$allowed_tags` can accept arrays since 7.4 Fixes vimeo/psalm#4317 --- src/Psalm/Config.php | 4 ++++ stubs/Php74.phpstub | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 stubs/Php74.phpstub diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 32ff16ea8..1439c9bb3 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -2258,6 +2258,10 @@ class Config $stubsDir . 'SPL.phpstub', ]; + if ($codebase->analysis_php_version_id >= 7_04_00) { + $this->internal_stubs[] = $stubsDir . 'Php74.phpstub'; + } + if ($codebase->analysis_php_version_id >= 8_00_00) { $this->internal_stubs[] = $stubsDir . 'CoreGenericAttributes.phpstub'; $this->internal_stubs[] = $stubsDir . 'Php80.phpstub'; diff --git a/stubs/Php74.phpstub b/stubs/Php74.phpstub new file mode 100644 index 000000000..4ade3a6da --- /dev/null +++ b/stubs/Php74.phpstub @@ -0,0 +1,11 @@ + return + * + * @param null|string|array $allowed_tags + */ +function strip_tags(string $string, null|string|array $allowed_tags = null) : string {} From 73ce16de96be63867197c38001ae22cafa078f84 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Thu, 17 Aug 2023 22:09:16 +0200 Subject: [PATCH 2/7] Fixes crash when assertion array is not a list Fixes vimeo/psalm#9984 --- src/Psalm/Type/Reconciler.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Psalm/Type/Reconciler.php b/src/Psalm/Type/Reconciler.php index 074ef233a..1ae92b405 100644 --- a/src/Psalm/Type/Reconciler.php +++ b/src/Psalm/Type/Reconciler.php @@ -424,6 +424,10 @@ class Reconciler { foreach ($new_types as $nk => $type) { if (strpos($nk, '[') || strpos($nk, '->')) { + $type = array_values($type); + if (!isset($type[0][0])) { + continue; + } if ($type[0][0] instanceof IsEqualIsset || $type[0][0] instanceof IsIsset || $type[0][0] instanceof NonEmpty From ed9dacf486601269e0c55f45c5171aa076335ce8 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Thu, 17 Aug 2023 22:16:07 +0200 Subject: [PATCH 3/7] Update baseline --- psalm-baseline.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 702c0718b..49462a409 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + tags['variablesfrom'][0]]]> @@ -601,8 +601,6 @@ $const_name - $type[0] - $type[0][0] 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 4/7] 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 a418a52bc..753497aa2 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 @@ 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)) { + 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 5/7] 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 753497aa2..41add7140 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 @@ class ProjectAnalyzer 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 6/7] 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 41add7140..96168a311 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 7/7] 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 96168a311..6be055ea4 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -1184,26 +1184,17 @@ class ProjectAnalyzer 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 1e5a1abdd..86f6b3261 100644 --- a/src/Psalm/Internal/CliUtils.php +++ b/src/Psalm/Internal/CliUtils.php @@ -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); + } } }