1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #6913 from weirdan/deprecate-xml-elements

This commit is contained in:
Bruce Weirdan 2021-11-14 21:02:59 +02:00 committed by GitHub
commit 84551dc89b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 7 deletions

View File

@ -103,6 +103,7 @@ use const SCANDIR_SORT_NONE;
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';
@ -708,7 +709,7 @@ class Config
}
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());
@ -766,21 +767,27 @@ class Config
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;
@ -798,6 +805,35 @@ class Config
);
}
}
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
)
);
}
}
}
/**

View File

@ -12,7 +12,6 @@ use function substr;
class ConfigFile
{
public const NS = 'https://getpsalm.org/schema/config';
/** @var string */
private $path;
@ -94,7 +93,7 @@ class ConfigFile
$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);

View File

@ -69,7 +69,7 @@ class ConfigFileTest extends \Psalm\Tests\TestCase
<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)
));
@ -91,7 +91,7 @@ class ConfigFileTest extends \Psalm\Tests\TestCase
$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)
));
}