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

Report deprecated config elements #6913

Merged
merged 3 commits into from Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/Psalm/Config.php
Expand Up @@ -103,6 +103,7 @@
class Config
{
private const DEFAULT_FILE_NAME = 'psalm.xml';
public const CONFIG_NAMESPACE = 'https://getpsalm.org/schema/config';
public const REPORT_INFO = 'info';
public const REPORT_ERROR = 'error';
public const REPORT_SUPPRESS = 'suppress';
Expand Down Expand Up @@ -708,7 +709,7 @@ private static function validateXmlConfig(string $base_dir, string $file_content
}

if (!$psalm_node->hasAttribute('xmlns')) {
$psalm_node->setAttribute('xmlns', 'https://getpsalm.org/schema/config');
$psalm_node->setAttribute('xmlns', self::CONFIG_NAMESPACE);

$old_dom_document = $dom_document;
$dom_document = self::loadDomDocument($base_dir, $old_dom_document->saveXML());
Expand Down Expand Up @@ -766,21 +767,27 @@ private static function processConfigDeprecations(
string $file_contents,
string $config_path
): void {
$config->config_issues = [];

// Attributes to be removed in Psalm 5
$deprecated_attributes = [
'allowCoercionFromStringToClassConst',
'allowPhpStormGenerics',
'exitFunctions'
];

$config->config_issues = [];
$deprecated_elements = [
'exitFunctions',
];

$psalm_element_item = $dom_document->getElementsByTagName('psalm')->item(0);
assert($psalm_element_item !== null);
$attributes = $psalm_element_item->attributes;

foreach ($attributes as $attribute) {
if (in_array($attribute->name, $deprecated_attributes, true)) {
$line = $attribute->getLineNo();
assert($line > 0); // getLineNo() always returns non-zero for nodes loaded from file

$offset = self::lineNumberToByteOffset($file_contents, $line);
$attribute_start = strrpos($file_contents, $attribute->name, $offset - strlen($file_contents)) ?: 0;
$attribute_end = $attribute_start + strlen($attribute->name) - 1;
Expand All @@ -798,6 +805,35 @@ private static function processConfigDeprecations(
);
}
}

foreach ($deprecated_elements as $deprecated_element) {
$deprecated_elements_xml = $dom_document->getElementsByTagNameNS(
self::CONFIG_NAMESPACE,
$deprecated_element
);
if ($deprecated_elements_xml->count()) {
$deprecated_element_xml = $deprecated_elements_xml->item(0);
assert($deprecated_element_xml !== null);
$line = $deprecated_element_xml->getLineNo();
assert($line > 0);

$offset = self::lineNumberToByteOffset($file_contents, $line);
$element_start = strpos($file_contents, $deprecated_element, $offset) ?: 0;
$element_end = $element_start + strlen($deprecated_element) - 1;

$config->config_issues[] = new ConfigIssue(
'Element "' . $deprecated_element . '" is deprecated '
. 'and is going to be removed in the next major version',
new CodeLocation\Raw(
$file_contents,
$config_path,
basename($config_path),
$element_start,
$element_end
)
);
}
}
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/PluginManager/ConfigFile.php
Expand Up @@ -12,7 +12,6 @@

class ConfigFile
{
public const NS = 'https://getpsalm.org/schema/config';
/** @var string */
private $path;

Expand Down Expand Up @@ -94,7 +93,7 @@ public function addPlugin(string $plugin_class): void

$plugin_class_element = $config_xml->createElement('pluginClass');
if ($plugin_class_element) {
$plugin_class_element->setAttribute('xmlns', self::NS);
$plugin_class_element->setAttribute('xmlns', Config::CONFIG_NAMESPACE);
$plugin_class_element->setAttribute('class', $plugin_class);
if ($plugins_element) {
$plugins_element->appendChild($plugin_class_element);
Expand Down
4 changes: 2 additions & 2 deletions tests/Config/ConfigFileTest.php
Expand Up @@ -69,7 +69,7 @@ public function addCanAddPluginClassToExistingPluginsNode(): void
<psalm
name="bar"
>
<plugins><pluginClass xmlns="' . ConfigFile::NS . '" class="a\b\c"/></plugins>
<plugins><pluginClass xmlns="' . Config::CONFIG_NAMESPACE . '" class="a\b\c"/></plugins>
</psalm>',
file_get_contents($this->file_path)
));
Expand All @@ -91,7 +91,7 @@ public function addCanCreateMissingPluginsNode(): void

$this->assertTrue(static::compareContentWithTemplateAndTrailingLineEnding(
'<?xml version="1.0"?>
<psalm><plugins><pluginClass xmlns="' . ConfigFile::NS . '" class="a\b\c"/></plugins></psalm>',
<psalm><plugins><pluginClass xmlns="' . Config::CONFIG_NAMESPACE . '" class="a\b\c"/></plugins></psalm>',
file_get_contents($this->file_path)
));
}
Expand Down