Skip to content

AC-1655: Fixed obsolete attributes not detected #380

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 12 additions & 33 deletions Magento2/Sniffs/Legacy/ModuleXMLSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ModuleXMLSniff implements Sniff
{
private const WARNING_CODE = 'FoundObsoleteAttribute';
private const ERROR_CODE = 'WrongXML';

/**
* @inheritdoc
*/
Expand All @@ -34,8 +34,7 @@ public function register(): array
*/
public function process(File $phpcsFile, $stackPtr)
{
$line = $phpcsFile->getTokens()[$stackPtr]['content'];
if (strpos(trim($line), '<module') === false) {
if ($stackPtr > 0) {
return;
}

Expand All @@ -52,53 +51,33 @@ public function process(File $phpcsFile, $stackPtr)
$stackPtr,
self::ERROR_CODE
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a return after adding an error if $xml === false

}

$foundElements = $xml->xpath('/config/module');
if ($foundElements === false) {
return;
}

foreach ($foundElements as $element) {
if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) {
continue;
}

if (property_exists($element->attributes(), 'version')) {

$foundElements = $xml->xpath('/config/module[@version]');
if ($foundElements !== false) {
foreach ($foundElements as $element) {
$phpcsFile->addWarning(
'The "version" attribute is obsolete. Use "setup_version" instead.',
$stackPtr,
dom_import_simplexml($element)->getLineNo()-1,
self::WARNING_CODE
);
}
}

if (property_exists($element->attributes(), 'active')) {
$foundElements = $xml->xpath('/config/module[@active]');
if ($foundElements !== false) {
foreach ($foundElements as $element) {
$phpcsFile->addWarning(
'The "active" attribute is obsolete. The list of active modules '.
'is defined in deployment configuration.',
$stackPtr,
dom_import_simplexml($element)->getLineNo()-1,
self::WARNING_CODE
);
}
}
}

/**
* Check if the element passed is in the currently sniffed line
*
* @param SimpleXMLElement $element
* @param int $stackPtr
* @return bool
*/
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool
{
$node = dom_import_simplexml($element);
if ($node->getLineNo() === $stackPtr+1) {
return true;
}
return false;
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
Expand Down
11 changes: 11 additions & 0 deletions Magento2/Tests/Legacy/ModuleXMLUnitTest.4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Training_Backend" active="true" version="1" />
</config>
5 changes: 5 additions & 0 deletions Magento2/Tests/Legacy/ModuleXMLUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function getWarningList($testFile = '')
if ($testFile === 'ModuleXMLUnitTest.3.xml') {
return [];
}
if ($testFile === 'ModuleXMLUnitTest.4.xml') {
return [
9 => 2,
];
}
return [];
}
}