|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento2\Sniffs\Legacy; |
| 7 | + |
| 8 | +use DOMDocument; |
| 9 | +use PHP_CodeSniffer\Files\File; |
| 10 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test to find obsolete acl declaration |
| 14 | + */ |
| 15 | +class ObsoleteAclSniff implements Sniff |
| 16 | +{ |
| 17 | + private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @inheritdoc |
| 21 | + */ |
| 22 | + public function register(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + T_INLINE_HTML |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @inheritDoc |
| 31 | + */ |
| 32 | + public function process(File $phpcsFile, $stackPtr) |
| 33 | + { |
| 34 | + if ($stackPtr > 0) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); |
| 39 | + $foundElements = $xml->xpath('/config/acl/*[boolean(./children) or boolean(./title)]'); |
| 40 | + foreach ($foundElements as $element) { |
| 41 | + $phpcsFile->addWarning( |
| 42 | + 'Obsolete acl structure detected in line ' . dom_import_simplexml($element)->getLineNo(), |
| 43 | + dom_import_simplexml($element)->getLineNo() - 1, |
| 44 | + self::WARNING_OBSOLETE_ACL_STRUCTURE |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Format the incoming XML to avoid tags split into several lines. |
| 51 | + * |
| 52 | + * @param File $phpcsFile |
| 53 | + * @return false|string |
| 54 | + */ |
| 55 | + private function getFormattedXML(File $phpcsFile) |
| 56 | + { |
| 57 | + $doc = new DomDocument('1.0'); |
| 58 | + $doc->formatOutput = true; |
| 59 | + $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); |
| 60 | + return $doc->saveXML(); |
| 61 | + } |
| 62 | +} |
0 commit comments