|
| 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 | + |
| 13 | +/** |
| 14 | + * Test for obsolete nodes/attributes in the module.xml |
| 15 | + */ |
| 16 | +class WidgetXMLSniff implements Sniff |
| 17 | +{ |
| 18 | + private const ERROR_CODE_OBSOLETE = 'FoundObsoleteNode'; |
| 19 | + private const ERROR_CODE_FACTORY = 'FoundFactory'; |
| 20 | + private const ERROR_CODE_XML = '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 | + if($stackPtr > 0) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); |
| 42 | + if ($xml === false) { |
| 43 | + $phpcsFile->addError( |
| 44 | + sprintf( |
| 45 | + "Couldn't parse contents of '%s', check that they are in valid XML format", |
| 46 | + $phpcsFile->getFilename(), |
| 47 | + ), |
| 48 | + 1, |
| 49 | + self::ERROR_CODE_XML |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + $foundElements = $xml->xpath('/widgets/*[@type]'); |
| 54 | + |
| 55 | + foreach ($foundElements as $element) { |
| 56 | + if (property_exists($element->attributes(), 'type')) { |
| 57 | + $type = $element['type']; |
| 58 | + if (preg_match('/\//', $type)) { |
| 59 | + $phpcsFile->addError( |
| 60 | + "Factory name detected: {$type}.", |
| 61 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 62 | + self::ERROR_CODE_FACTORY |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $foundElements = $xml->xpath('/widgets/*/supported_blocks'); |
| 69 | + foreach ($foundElements as $element) { |
| 70 | + $phpcsFile->addError( |
| 71 | + "Obsolete node: <supported_blocks>. To be replaced with <supported_containers>", |
| 72 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 73 | + self::ERROR_CODE_OBSOLETE |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + $foundElements = $xml->xpath('/widgets/*/*/*/block_name'); |
| 78 | + foreach ($foundElements as $element) { |
| 79 | + $phpcsFile->addError( |
| 80 | + "Obsolete node: <block_name>. To be replaced with <container_name>", |
| 81 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 82 | + self::ERROR_CODE_OBSOLETE |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Check if the element passed is in the currently sniffed line |
| 89 | + * |
| 90 | + * @param SimpleXMLElement $element |
| 91 | + * @param int $stackPtr |
| 92 | + * @return bool |
| 93 | + */ |
| 94 | + private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool |
| 95 | + { |
| 96 | + $node = dom_import_simplexml($element); |
| 97 | + |
| 98 | + return $node->getLineNo() === $stackPtr + 1; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Format the incoming XML to avoid tags split into several lines. |
| 103 | + * |
| 104 | + * @param File $phpcsFile |
| 105 | + * @return false|string |
| 106 | + */ |
| 107 | + private function getFormattedXML(File $phpcsFile) |
| 108 | + { |
| 109 | + $doc = new DomDocument('1.0'); |
| 110 | + $doc->formatOutput = true; |
| 111 | + $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); |
| 112 | + return $doc->saveXML(); |
| 113 | + } |
| 114 | +} |
0 commit comments