Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not display ext deprecation note when disableExtensions used #9291

Merged
merged 8 commits into from Feb 28, 2023
43 changes: 24 additions & 19 deletions src/Psalm/Config.php
Expand Up @@ -600,27 +600,31 @@ class Config
/**
* A list of php extensions supported by Psalm.
* Where key - extension name (without ext- prefix), value - whether to load extension’s stub.
* Values:
* - true: ext enabled explicitly or bundled with PHP (should load stubs)
* - false: ext disabled explicitly (should not load stubs)
* - null: state is unknown (e.g. config not processed yet) or ext neither explicitly enabled or disabled.
*
* @psalm-readonly-allow-private-mutation
* @var array<string, bool>
* @var array<string, bool|null>
*/
public $php_extensions = [
"apcu" => false,
"decimal" => false,
"dom" => false,
"ds" => false,
"ffi" => false,
"geos" => false,
"gmp" => false,
"ibm_db2" => false,
"mongodb" => false,
"mysqli" => false,
"pdo" => false,
"random" => false,
"redis" => false,
"simplexml" => false,
"soap" => false,
"xdebug" => false,
"apcu" => null,
"decimal" => null,
"dom" => null,
"ds" => null,
"ffi" => null,
"geos" => null,
"gmp" => null,
"ibm_db2" => null,
"mongodb" => null,
"mysqli" => null,
"pdo" => null,
"random" => null,
"redis" => null,
"simplexml" => null,
"soap" => null,
"xdebug" => null,
];

/**
Expand Down Expand Up @@ -1115,7 +1119,7 @@ private static function fromXmlAndPaths(
}
}
foreach ($required_extensions as $required_ext => $_) {
if (isset($config->php_extensions[$required_ext])) {
if (array_key_exists($required_ext, $config->php_extensions)) {
$config->php_extensions[$required_ext] = true;
} else {
$config->php_extensions_not_supported[$required_ext] = true;
Expand Down Expand Up @@ -2242,7 +2246,8 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null):
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 = in_array($ext_stub_path, $this->internal_stubs, true);
if (! $is_stub_already_loaded && extension_loaded($ext_name)) {
$is_ext_explicitly_disabled = ($this->php_extensions[$ext_name] ?? null) === false;
if (! $is_stub_already_loaded && ! $is_ext_explicitly_disabled && extension_loaded($ext_name)) {
$this->internal_stubs[] = $ext_stub_path;
$this->config_warnings[] = "Psalm 6 will not automatically load stubs for ext-$ext_name."
. " You should explicitly enable or disable this ext in composer.json or Psalm config.";
Expand Down
59 changes: 59 additions & 0 deletions tests/Config/ConfigTest.php
Expand Up @@ -1610,4 +1610,63 @@ public function testConfigFileWithXIncludeWithFallback(): void

$this->assertFalse($config->reportIssueInFile('MixedAssignment', realpath('src/Psalm/Type.php')));
}

/**
* @requires extension apcu
* @deprecated Remove in Psalm 6.
*/
public function testConfigWarnsAboutDeprecatedWayToLoadStubsButLoadsTheStub(): void
{
$config_xml = Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="src" />
</projectFiles>
</psalm>',
);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig($config_xml);
$codebase = $this->project_analyzer->getCodebase();
$config = $this->project_analyzer->getConfig();

$config->visitStubFiles($codebase);

$this->assertContains(realpath('stubs/extensions/apcu.phpstub'), $config->internal_stubs);
$this->assertContains(
'Psalm 6 will not automatically load stubs for ext-apcu. You should explicitly enable or disable this ext in composer.json or Psalm config.',
$config->config_warnings,
);
}

/**
* @requires extension apcu
* @deprecated Remove deprecation warning part in Psalm 6.
*/
public function testConfigWithDisableExtensionsDoesNotLoadExtensionStubsAndHidesDeprecationWarning(): void
{
$config_xml = Config::loadFromXML(
(string)getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="src" />
</projectFiles>
<disableExtensions>
<extension name="apcu"/>
</disableExtensions>
</psalm>',
);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig($config_xml);
$codebase = $this->project_analyzer->getCodebase();
$config = $this->project_analyzer->getConfig();

$config->visitStubFiles($codebase);

$this->assertNotContains(realpath('stubs/extensions/apcu.phpstub'), $config->internal_stubs);
$this->assertNotContains(
'Psalm 6 will not automatically load stubs for ext-apcu. You should explicitly enable or disable this ext in composer.json or Psalm config.',
$config->internal_stubs,
);
}
}