Skip to content

Commit

Permalink
Remove user defined constants and mock php version constants for Conf…
Browse files Browse the repository at this point in the history
…ig->predefined_constants
  • Loading branch information
ygottschalk committed Jun 26, 2023
1 parent f93d238 commit 7058d83
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 28 additions & 3 deletions src/Psalm/Config.php
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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();

Expand Down
Expand Up @@ -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;
Expand Down

0 comments on commit 7058d83

Please sign in to comment.