From 16ab9f786bbfaf6c5d8f172490ee0133ffc30a16 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sat, 10 Dec 2022 17:02:47 +0100 Subject: [PATCH 1/8] Unify a way how to load stubs for extentions --- src/Psalm/Config.php | 27 +++++-------------- .../apcu.phpstub} | 0 .../{ext-random.phpstub => random.phpstub} | 0 .../redis.phpstub} | 0 4 files changed, 7 insertions(+), 20 deletions(-) rename stubs/{ext-apcu.phpstub => extensions/apcu.phpstub} (100%) rename stubs/extensions/{ext-random.phpstub => random.phpstub} (100%) rename stubs/{phpredis.phpstub => extensions/redis.phpstub} (100%) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index ee172a4300a..94bbc218408 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -62,7 +62,6 @@ use function count; use function dirname; use function explode; -use function extension_loaded; use function fclose; use function file_exists; use function file_get_contents; @@ -590,11 +589,16 @@ class Config public $threads; /** + * A list of php extensions supported by Psalm. + * Where key - extension name (without ext- prefix), value - whether to load extension’s stub. + * * @psalm-readonly-allow-private-mutation * @var array{ + * apcu: bool, * decimal: bool, * dom: bool, * ds: bool, + * ffi: bool, * geos: bool, * gmp: bool, * mongodb: bool, @@ -603,13 +607,14 @@ class Config * simplexml: bool, * soap: bool, * xdebug: bool, - * ffi: bool, * } */ public $php_extensions = [ + "apcu" => false, "decimal" => false, "dom" => false, "ds" => false, + "ffi" => false, "geos" => false, "gmp" => false, "mongodb" => false, @@ -618,7 +623,6 @@ class Config "simplexml" => false, "soap" => false, "xdebug" => false, - "ffi" => false, ]; /** @@ -2142,23 +2146,6 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null): } } - // phpredis - if (extension_loaded('redis')) { - $ext_phpredis_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'phpredis.phpstub'; - $this->internal_stubs[] = $ext_phpredis_path; - } - - if (extension_loaded('apcu')) { - $ext_apcu_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'ext-apcu.phpstub'; - $this->internal_stubs[] = $ext_apcu_path; - } - - if (extension_loaded('random')) { - $ext_random_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR - . 'extensions' . DIRECTORY_SEPARATOR . 'ext-random.phpstub'; - $this->internal_stubs[] = $ext_random_path; - } - foreach ($this->internal_stubs as $stub_path) { if (!file_exists($stub_path)) { throw new UnexpectedValueException('Cannot locate ' . $stub_path); diff --git a/stubs/ext-apcu.phpstub b/stubs/extensions/apcu.phpstub similarity index 100% rename from stubs/ext-apcu.phpstub rename to stubs/extensions/apcu.phpstub diff --git a/stubs/extensions/ext-random.phpstub b/stubs/extensions/random.phpstub similarity index 100% rename from stubs/extensions/ext-random.phpstub rename to stubs/extensions/random.phpstub diff --git a/stubs/phpredis.phpstub b/stubs/extensions/redis.phpstub similarity index 100% rename from stubs/phpredis.phpstub rename to stubs/extensions/redis.phpstub From 0fd4c520deaeafb83b776123f5cba086edc0b8db Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 14:01:20 +0100 Subject: [PATCH 2/8] Load ext stubs using extension_loaded but diaplay a deprecation note --- src/Psalm/Config.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 94bbc218408..b15234b18e2 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -62,6 +62,7 @@ use function count; use function dirname; use function explode; +use function extension_loaded; use function fclose; use function file_exists; use function file_get_contents; @@ -2139,10 +2140,23 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null): $this->internal_stubs[] = $stringable_path; } + $ext_stubs_dir = $dir_lvl_2 . DIRECTORY_SEPARATOR . "stubs" . DIRECTORY_SEPARATOR . "extensions"; foreach ($this->php_extensions as $ext => $enabled) { if ($enabled) { - $this->internal_stubs[] = $dir_lvl_2 . DIRECTORY_SEPARATOR . "stubs" - . DIRECTORY_SEPARATOR . "extensions" . DIRECTORY_SEPARATOR . "$ext.phpstub"; + $this->internal_stubs[] = $ext_stubs_dir . DIRECTORY_SEPARATOR . "$ext.phpstub"; + } + } + + /** @deprecated Will be removed in Psalm 6 */ + $extensions_to_load_stubs_using_deprecated_way = ['apcu', 'random', 'redis']; + foreach ($extensions_to_load_stubs_using_deprecated_way as $ext_name) { + $ext_stub_path = $ext_stubs_dir . DIRECTORY_SEPARATOR . "$ext_name.phpstub"; + $is_stub_already_loaded = array_key_exists($ext_stub_path, $this->internal_stubs); + if (! $is_stub_already_loaded && extension_loaded($ext_name)) { + $this->internal_stubs[] = $ext_stub_path; + $progress->write("Deprecation: Psalm stubs for ext-$ext_name loaded using legacy way." + . " Instead, please declare ext-$ext_name as dependency in composer.json" + . " or use directive in Psalm config.\n"); } } From 62c998b8cf4e8aa5415cf4ac929f7cedadaa4b96 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 14:01:20 +0100 Subject: [PATCH 3/8] Load ext stubs using extension_loaded but display a deprecation note --- src/Psalm/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index b15234b18e2..c77bdb64329 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -2151,7 +2151,7 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null): $extensions_to_load_stubs_using_deprecated_way = ['apcu', 'random', 'redis']; foreach ($extensions_to_load_stubs_using_deprecated_way as $ext_name) { $ext_stub_path = $ext_stubs_dir . DIRECTORY_SEPARATOR . "$ext_name.phpstub"; - $is_stub_already_loaded = array_key_exists($ext_stub_path, $this->internal_stubs); + $is_stub_already_loaded = in_array($ext_stub_path, $this->internal_stubs, true); if (! $is_stub_already_loaded && extension_loaded($ext_name)) { $this->internal_stubs[] = $ext_stub_path; $progress->write("Deprecation: Psalm stubs for ext-$ext_name loaded using legacy way." From 74f65cba719d2c4661d657af331780f1992a4727 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 14:38:38 +0100 Subject: [PATCH 4/8] Make Psalm about stub file for redis extension --- src/Psalm/Config.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index c77bdb64329..206d486e5fa 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -605,6 +605,7 @@ class Config * mongodb: bool, * mysqli: bool, * pdo: bool, + * redis: bool, * simplexml: bool, * soap: bool, * xdebug: bool, @@ -621,6 +622,7 @@ class Config "mongodb" => false, "mysqli" => false, "pdo" => false, + "redis" => false, "simplexml" => false, "soap" => false, "xdebug" => false, From 5e95d13157e0776b8747dc7769bcaa45e53ec712 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 14:45:20 +0100 Subject: [PATCH 5/8] Make Psalm about stub file for random (core) extension --- src/Psalm/Config.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 206d486e5fa..cc8ac663ab5 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -622,6 +622,7 @@ class Config "mongodb" => false, "mysqli" => false, "pdo" => false, + "random" => false, "redis" => false, "simplexml" => false, "soap" => false, From 729eb59169865e8d90d65fdab58d9bef26a0ad6c Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 14:55:42 +0100 Subject: [PATCH 6/8] Remove an excessive array shape annotation --- src/Psalm/Config.php | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index cc8ac663ab5..e8c9cbdcbce 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -594,22 +594,7 @@ class Config * Where key - extension name (without ext- prefix), value - whether to load extension’s stub. * * @psalm-readonly-allow-private-mutation - * @var array{ - * apcu: bool, - * decimal: bool, - * dom: bool, - * ds: bool, - * ffi: bool, - * geos: bool, - * gmp: bool, - * mongodb: bool, - * mysqli: bool, - * pdo: bool, - * redis: bool, - * simplexml: bool, - * soap: bool, - * xdebug: bool, - * } + * @var array */ public $php_extensions = [ "apcu" => false, From a8046a8682901c31b86914d258a8ae2dbdf31fd1 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 15:01:34 +0100 Subject: [PATCH 7/8] Simplify array type declaration --- src/Psalm/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index e8c9cbdcbce..a21109b8eeb 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -594,7 +594,7 @@ class Config * Where key - extension name (without ext- prefix), value - whether to load extension’s stub. * * @psalm-readonly-allow-private-mutation - * @var array + * @var array */ public $php_extensions = [ "apcu" => false, From c8b6b0b50456c8c4b9eb22865d33ad3a26e72e01 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 11 Dec 2022 15:10:43 +0100 Subject: [PATCH 8/8] Remove unused @psalm-suppress --- src/Psalm/Config.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index a21109b8eeb..b262480f486 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -1040,7 +1040,6 @@ private static function fromXmlAndPaths( } foreach ($required_extensions as $required_ext => $_) { if (isset($config->php_extensions[$required_ext])) { - /** @psalm-suppress PropertyTypeCoercion isset doesn't narrow $required_ext like it should */ $config->php_extensions[$required_ext] = true; } else { $config->php_extensions_not_supported[$required_ext] = true;