|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento2\Sniffs\Legacy; |
| 8 | + |
| 9 | +use DOMDocument; |
| 10 | +use PHP_CodeSniffer\Files\File; |
| 11 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 12 | +use SimpleXMLElement; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test for obsolete nodes/attributes in the module.xml |
| 16 | + */ |
| 17 | +class ModuleXMLSniff implements Sniff |
| 18 | +{ |
| 19 | + private const WARNING_CODE = 'FoundObsoleteAttribute'; |
| 20 | + private const ERROR_CODE = 'WrongXML'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @inheritdoc |
| 24 | + */ |
| 25 | + public function register(): array |
| 26 | + { |
| 27 | + return [ |
| 28 | + T_INLINE_HTML |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritDoc |
| 34 | + */ |
| 35 | + public function process(File $phpcsFile, $stackPtr) |
| 36 | + { |
| 37 | + $line = $phpcsFile->getTokens()[$stackPtr]['content']; |
| 38 | + if (strpos(trim($line), '<module') === false) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement |
| 43 | + // returns the position of the closing /> as the position of the tag, and we need the position of <module |
| 44 | + // instead, as it is the one we compare with $stackPtr later on. |
| 45 | + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); |
| 46 | + if ($xml === false) { |
| 47 | + $phpcsFile->addError( |
| 48 | + sprintf( |
| 49 | + "Couldn't parse contents of '%s', check that they are in valid XML format", |
| 50 | + $phpcsFile->getFilename(), |
| 51 | + ), |
| 52 | + $stackPtr, |
| 53 | + self::ERROR_CODE |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + $foundElements = $xml->xpath('/config/module'); |
| 58 | + if ($foundElements === false) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + foreach ($foundElements as $element) { |
| 63 | + if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + if (property_exists($element->attributes(), 'version')) { |
| 68 | + $phpcsFile->addWarning( |
| 69 | + 'The "version" attribute is obsolete. Use "setup_version" instead.', |
| 70 | + $stackPtr, |
| 71 | + self::WARNING_CODE |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (property_exists($element->attributes(), 'active')) { |
| 76 | + $phpcsFile->addWarning( |
| 77 | + 'The "active" attribute is obsolete. The list of active modules '. |
| 78 | + 'is defined in deployment configuration.', |
| 79 | + $stackPtr, |
| 80 | + self::WARNING_CODE |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Check if the element passed is in the currently sniffed line |
| 88 | + * |
| 89 | + * @param SimpleXMLElement $element |
| 90 | + * @param int $stackPtr |
| 91 | + * @return bool |
| 92 | + */ |
| 93 | + private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool |
| 94 | + { |
| 95 | + $node = dom_import_simplexml($element); |
| 96 | + if ($node->getLineNo() === $stackPtr+1) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Format the incoming XML to avoid tags split into several lines. |
| 104 | + * |
| 105 | + * @param File $phpcsFile |
| 106 | + * @return false|string |
| 107 | + */ |
| 108 | + private function getFormattedXML(File $phpcsFile) |
| 109 | + { |
| 110 | + $doc = new DomDocument('1.0'); |
| 111 | + $doc->formatOutput = true; |
| 112 | + $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); |
| 113 | + return $doc->saveXML(); |
| 114 | + } |
| 115 | +} |
0 commit comments