From 7058d8305924e936d59bcb8ee135fad686088e11 Mon Sep 17 00:00:00 2001 From: Yannick Gottschalk Date: Mon, 26 Jun 2023 13:46:53 +0200 Subject: [PATCH] Remove user defined constants and mock php version constants for Config->predefined_constants --- src/Psalm/Config.php | 31 +++++++++++++++++-- .../Reflector/ExpressionResolver.php | 8 +---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index e84a134f37a..d8dcbf5ce1d 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -54,6 +54,7 @@ use function array_pad; use function array_pop; use function array_shift; +use function array_values; use function assert; use function basename; use function chdir; @@ -67,6 +68,7 @@ use function file_exists; use function file_get_contents; use function flock; +use function floor; use function fopen; use function function_exists; use function get_class; @@ -2391,9 +2393,32 @@ public function getPredefinedConstants(): array return $this->predefined_constants; } - public function collectPredefinedConstants(): void + /** + * Collects all (non-user) predefined constants and changes the PHP_*_VERSION constants to + * match the $phpVersioId if given. + */ + public function collectPredefinedConstants(?int $phpVersionId = null): void { - $this->predefined_constants = get_defined_constants(); + $predefined_constants = get_defined_constants(true); + if (isset($predefined_constants['user'])) { + unset($predefined_constants['user']); + } + $predefined_constants = array_merge(...array_values($predefined_constants)); + + if (isset($phpVersionId)) { + $release = $phpVersionId % 100; + $minor = floor($phpVersionId / 100) % 100; + $major = floor($phpVersionId / 10_000) % 100; + $predefined_constants['PHP_VERSION'] = + $major . '.' . $minor . '.' . $release . ($predefined_constants['PHP_VERSION_EXTRA'] ?? ''); + $predefined_constants['PHP_MAJOR_VERSION'] = $major; + $predefined_constants['PHP_MINOR_VERSION'] = $minor; + $predefined_constants['PHP_RELEASE_VERSION'] = $release; + $predefined_constants['PHP_VERSION_ID'] = $phpVersionId; + } + + + $this->predefined_constants = $predefined_constants; } /** @@ -2459,7 +2484,7 @@ public function visitComposerAutoloadFiles(ProjectAnalyzer $project_analyzer, ?P ); } - $this->collectPredefinedConstants(); + $this->collectPredefinedConstants($project_analyzer->getCodebase()->analysis_php_version_id); $autoload_included_files = $this->include_collector->getFilteredIncludedFiles(); diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php b/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php index 93a0ddcecd4..fd68e41dcf1 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php @@ -521,13 +521,7 @@ private static function functionEvaluatesToTrue( && isset($function->getArgs()[0]) && $function->getArgs()[0]->value instanceof PhpParser\Node\Scalar\String_ ) { - $predefined_constants = get_defined_constants(true); - if (isset($predefined_constants['user'])) { - unset($predefined_constants['user']); - } - $predefined_constants = array_merge(...array_values($predefined_constants)); - - return isset($predefined_constants[$function->getArgs()[0]->value->value]); + return isset($codebase->config->getPredefinedConstants()[$function->getArgs()[0]->value->value]); } return null;