1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Load PHP-version-specific stubs based on analysis PHP version, and only when visiting stub files

While `visitPreloadedStubFiles` seemed to work at first, it led to overriding symbols declared by
PHP itself too eagerly.

By only loading PHP-version-specific stubs in `visitStubFiles`, we ensure that the reflection-declared
symbols take priority, and that our stubs overlay on top of that, without actually replacing the
symbol entirely, but rather merging with its definition.

This fixes current test failures too, and works with the code examples
from https://github.com/vimeo/psalm/pull/8722#issuecomment-1339711882
This commit is contained in:
Marco Pivetta 2022-12-08 18:22:51 +01:00
parent 042305107e
commit 68d88c546b

View File

@ -2065,7 +2065,7 @@ class Config
$core_generic_files = [];
if ($codebase->analysis_php_version_id >= 8_00_00) {
if (PHP_VERSION_ID < 8_00_00 && $codebase->analysis_php_version_id >= 8_00_00) {
$stringable_path = dirname(__DIR__, 2) . '/stubs/Php80.phpstub';
if (!file_exists($stringable_path)) {
@ -2075,7 +2075,7 @@ class Config
$core_generic_files[] = $stringable_path;
}
if ($codebase->analysis_php_version_id >= 8_01_00) {
if (PHP_VERSION_ID < 8_01_00 && $codebase->analysis_php_version_id >= 8_01_00) {
$stringable_path = dirname(__DIR__, 2) . '/stubs/Php81.phpstub';
if (!file_exists($stringable_path)) {
@ -2085,7 +2085,7 @@ class Config
$core_generic_files[] = $stringable_path;
}
if ($codebase->analysis_php_version_id >= 8_02_00) {
if (PHP_VERSION_ID < 8_02_00 && $codebase->analysis_php_version_id >= 8_02_00) {
$stringable_path = dirname(__DIR__, 2) . '/stubs/Php82.phpstub';
if (!file_exists($stringable_path)) {
@ -2135,16 +2135,21 @@ class Config
$dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'SPL.phpstub',
];
if (PHP_VERSION_ID >= 8_00_00 && $codebase->analysis_php_version_id >= 8_00_00) {
if ($codebase->analysis_php_version_id >= 8_00_00) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php80.phpstub';
$this->internal_stubs[] = $stringable_path;
}
if (PHP_VERSION_ID >= 8_01_00 && $codebase->analysis_php_version_id >= 8_01_00) {
if ($codebase->analysis_php_version_id >= 8_01_00) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php81.phpstub';
$this->internal_stubs[] = $stringable_path;
}
if ($codebase->analysis_php_version_id >= 8_02_00) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php82.phpstub';
$this->internal_stubs[] = $stringable_path;
}
foreach ($this->php_extensions as $ext => $enabled) {
if ($enabled) {
$this->internal_stubs[] = $dir_lvl_2 . DIRECTORY_SEPARATOR . "stubs"