|
| 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 widget.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 | + |
| 43 | + if ($xml === false) { |
| 44 | + $this->invalidXML($phpcsFile, $stackPtr); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $foundElements = $xml->xpath('/widgets/*[@type]'); |
| 49 | + |
| 50 | + foreach ($foundElements as $element) { |
| 51 | + if (!property_exists($element->attributes(), 'type')) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + $type = $element['type']; |
| 55 | + if (preg_match('/\//', $type)) { |
| 56 | + $phpcsFile->addError( |
| 57 | + "Factory name detected: {$type}.", |
| 58 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 59 | + self::ERROR_CODE_FACTORY |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $foundElements = $xml->xpath('/widgets/*/supported_blocks'); |
| 65 | + foreach ($foundElements as $element) { |
| 66 | + $phpcsFile->addError( |
| 67 | + "Obsolete node: <supported_blocks>. To be replaced with <supported_containers>", |
| 68 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 69 | + self::ERROR_CODE_OBSOLETE |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + $foundElements = $xml->xpath('/widgets/*/*/*/block_name'); |
| 74 | + foreach ($foundElements as $element) { |
| 75 | + $phpcsFile->addError( |
| 76 | + "Obsolete node: <block_name>. To be replaced with <container_name>", |
| 77 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 78 | + self::ERROR_CODE_OBSOLETE |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Adds an invalid XML error |
| 85 | + * |
| 86 | + * @param File $phpcsFile |
| 87 | + * @param int $stackPtr |
| 88 | + */ |
| 89 | + protected function invalidXML(File $phpcsFile, int $stackPtr): void |
| 90 | + { |
| 91 | + $phpcsFile->addError( |
| 92 | + sprintf( |
| 93 | + "Couldn't parse contents of '%s', check that they are in valid XML format", |
| 94 | + $phpcsFile->getFilename(), |
| 95 | + ), |
| 96 | + $stackPtr, |
| 97 | + self::ERROR_CODE_XML |
| 98 | + ); |
| 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