diff --git a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php index da25de0d..338834a8 100644 --- a/Magento2/Sniffs/Legacy/ModuleXMLSniff.php +++ b/Magento2/Sniffs/Legacy/ModuleXMLSniff.php @@ -18,7 +18,7 @@ class ModuleXMLSniff implements Sniff { private const WARNING_CODE = 'FoundObsoleteAttribute'; private const ERROR_CODE = 'WrongXML'; - + /** * @inheritdoc */ @@ -34,8 +34,7 @@ public function register(): array */ public function process(File $phpcsFile, $stackPtr) { - $line = $phpcsFile->getTokens()[$stackPtr]['content']; - if (strpos(trim($line), ' 0) { return; } @@ -52,53 +51,33 @@ public function process(File $phpcsFile, $stackPtr) $stackPtr, self::ERROR_CODE ); - } - - $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. * diff --git a/Magento2/Tests/Legacy/ModuleXMLUnitTest.4.xml b/Magento2/Tests/Legacy/ModuleXMLUnitTest.4.xml new file mode 100644 index 00000000..cbeb6c9b --- /dev/null +++ b/Magento2/Tests/Legacy/ModuleXMLUnitTest.4.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/Magento2/Tests/Legacy/ModuleXMLUnitTest.php b/Magento2/Tests/Legacy/ModuleXMLUnitTest.php index c16d1e8b..4b2a2cde 100644 --- a/Magento2/Tests/Legacy/ModuleXMLUnitTest.php +++ b/Magento2/Tests/Legacy/ModuleXMLUnitTest.php @@ -35,6 +35,11 @@ public function getWarningList($testFile = '') if ($testFile === 'ModuleXMLUnitTest.3.xml') { return []; } + if ($testFile === 'ModuleXMLUnitTest.4.xml') { + return [ + 9 => 2, + ]; + } return []; } }