66
77namespace Magento2 \Sniffs \Legacy ;
88
9+ use DOMDocument ;
910use PHP_CodeSniffer \Files \File ;
1011use PHP_CodeSniffer \Sniffs \Sniff ;
1112use SimpleXMLElement ;
1516 */
1617class ModuleXMLSniff implements Sniff
1718{
18- /**
19- * Error violation code.
20- */
21- const WARNING_CODE = 'FoundObsoleteAttribute ' ;
19+ private const WARNING_CODE = 'FoundObsoleteAttribute ' ;
20+ private const ERROR_CODE = 'WrongXML ' ;
2221
2322 /**
2423 * @inheritdoc
@@ -36,19 +35,19 @@ public function register(): array
3635 public function process (File $ phpcsFile , $ stackPtr )
3736 {
3837 $ line = $ phpcsFile ->getTokens ()[$ stackPtr ]['content ' ];
39- if (strpos (trim ($ line ), '<module ' ) === false ) {
38+ if (strpos (trim ($ line ), '<module ' ) === false ) {
4039 return ;
4140 }
42-
43- $ xml = simplexml_load_string ($ phpcsFile -> getTokensAsString ( 0 , 999999 ));
41+
42+ $ xml = simplexml_load_string ($ this -> getFormattedXML ( $ phpcsFile ));
4443 if ($ xml === false ) {
4544 $ phpcsFile ->addError (
4645 sprintf (
4746 "Couldn't parse contents of '%s', check that they are in valid XML format " ,
4847 $ phpcsFile ->getFilename (),
4948 ),
5049 $ stackPtr ,
51- self ::WARNING_CODE
50+ self ::ERROR_CODE
5251 );
5352 }
5453
@@ -96,4 +95,16 @@ private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $s
9695 }
9796 return false ;
9897 }
98+
99+ /**
100+ * @param File $phpcsFile
101+ * @return false|string
102+ */
103+ private function getFormattedXML (File $ phpcsFile )
104+ {
105+ $ doc = new DomDocument ('1.0 ' );
106+ $ doc ->formatOutput = true ;
107+ $ doc ->loadXML ($ phpcsFile ->getTokensAsString (0 , 999999 ));
108+ return $ doc ->saveXML ();
109+ }
99110}
0 commit comments