Skip to content

Commit

Permalink
Merge pull request #7272 from weirdan/drop-deprecated-config-entries
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Jan 3, 2022
2 parents b8bad62 + 55eb4b0 commit 0dba2a6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
10 changes: 0 additions & 10 deletions config.xsd
Expand Up @@ -32,16 +32,6 @@
<xs:attribute name="serializer" type="xs:string" />

<xs:attribute name="addParamDefaultToDocblockType" type="xs:boolean" default="false" />

<xs:attribute name="allowCoercionFromStringToClassConst" type="xs:boolean" default="false">
<xs:annotation>
<!-- note: for PHPStorm to mark the attribute as deprecated the doc entry has to be *single line* and start with the word `deprecated` -->
<xs:documentation xml:lang="en">
Deprecated. Has no effect since Psalm 3 and is going to be removed in Psalm 5.
</xs:documentation>
</xs:annotation>
</xs:attribute>

<xs:attribute name="allowFileIncludes" type="xs:boolean" default="true" />
<xs:attribute name="allowStringToStandInForClass" type="xs:boolean" default="false" />
<xs:attribute name="checkForThrowsDocblock" type="xs:boolean" default="false" />
Expand Down
104 changes: 59 additions & 45 deletions src/Psalm/Config.php
Expand Up @@ -5,6 +5,7 @@
use Composer\Autoload\ClassLoader;
use Composer\Semver\Constraint\Constraint;
use Composer\Semver\VersionParser;
use DOMAttr;
use DOMDocument;
use DomElement;
use InvalidArgumentException;
Expand Down Expand Up @@ -762,6 +763,58 @@ private static function lineNumberToByteOffset(string $string, int $line_number)
return $offset;
}

private static function processDeprecatedAttribute(
DOMAttr $attribute,
string $file_contents,
self $config,
string $config_path
): void {
$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;

$config->config_issues[] = new ConfigIssue(
'Attribute "' . $attribute->name . '" is deprecated '
. 'and is going to be removed in the next major version',
new Raw(
$file_contents,
$config_path,
basename($config_path),
$attribute_start,
$attribute_end
)
);
}

private static function processDeprecatedElement(
DomElement $deprecated_element_xml,
string $file_contents,
self $config,
string $config_path
): void {
$line = $deprecated_element_xml->getLineNo();
assert($line > 0);

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

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

private static function processConfigDeprecations(
self $config,
DOMDocument $dom_document,
Expand All @@ -770,40 +823,19 @@ private static function processConfigDeprecations(
): void {
$config->config_issues = [];

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

$deprecated_elements = [
'exitFunctions',
];
/** @var list<string> */
$deprecated_elements = [];

$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;

$config->config_issues[] = new ConfigIssue(
'Attribute "' . $attribute->name . '" is deprecated '
. 'and is going to be removed in the next major version',
new Raw(
$file_contents,
$config_path,
basename($config_path),
$attribute_start,
$attribute_end
)
);
self::processDeprecatedAttribute($attribute, $file_contents, $config, $config_path);
}
}

Expand All @@ -814,25 +846,7 @@ private static function processConfigDeprecations(
);
if ($deprecated_elements_xml->length) {
$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 Raw(
$file_contents,
$config_path,
basename($config_path),
$element_start,
$element_end
)
);
self::processDeprecatedElement($deprecated_element_xml, $file_contents, $config, $config_path);
}
}
}
Expand Down

0 comments on commit 0dba2a6

Please sign in to comment.