66
77namespace Magento2 \Sniffs \Legacy ;
88
9- use DOMDocument ;
109use PHP_CodeSniffer \Files \File ;
1110use PHP_CodeSniffer \Sniffs \Sniff ;
1211
1312class DiConfigSniff implements Sniff
1413{
1514 private const WARNING_CODE = 'FoundObsoleteAttribute ' ;
16- private const ERROR_CODE = 'WrongXML ' ;
1715
1816 private $ xpathObsoleteElems = [
19- 'param ' ,
20- 'instance ' ,
21- 'array ' ,
22- 'item[@key] ' ,
23- 'value '
24- ];
25-
26- private $ messages = [
27- 'param ' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="..."> ' ,
28- 'instance ' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object"> ' ,
29- 'array ' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array"> ' ,
30- 'item[@key] ' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="..."> ' ,
31- 'value ' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal. '
17+ '<param ' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="..."> ' ,
18+ '<instance ' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object"> ' ,
19+ '<array ' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array"> ' ,
20+ '<item key= ' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="..."> ' ,
21+ '<value ' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal. '
3222 ];
3323
3424 public function register (): array
@@ -40,41 +30,16 @@ public function register(): array
4030
4131 public function process (File $ phpcsFile , $ stackPtr )
4232 {
43- $ xml = simplexml_load_string ($ this ->getFormattedXML ($ phpcsFile ));
44- if ($ xml === false ) {
45- $ phpcsFile ->addError (
46- sprintf (
47- "Couldn't parse contents of '%s', check that they are in valid XML format " ,
48- $ phpcsFile ->getFilename (),
49- ),
50- $ stackPtr ,
51- self ::ERROR_CODE
52- );
53- }
33+ $ lineContent = $ phpcsFile ->getTokensAsString ($ stackPtr , 1 );
5434
55- foreach ($ this ->xpathObsoleteElems as $ obsoleteElem ) {
56- $ found = $ xml ->xpath ($ obsoleteElem );
57- if ($ found === true ) {
35+ foreach ($ this ->xpathObsoleteElems as $ elem => $ message ) {
36+ if (strpos ($ lineContent , $ elem ) !== false ) {
5837 $ phpcsFile ->addWarning (
59- $ this -> messages [ $ obsoleteElem ] ,
38+ $ message ,
6039 $ stackPtr ,
6140 self ::WARNING_CODE
6241 );
6342 }
6443 }
6544 }
66-
67- /**
68- * Format the incoming XML to avoid tags split into several lines.
69- *
70- * @param File $phpcsFile
71- * @return false|string
72- */
73- private function getFormattedXML (File $ phpcsFile )
74- {
75- $ doc = new DomDocument ('1.0 ' );
76- $ doc ->formatOutput = true ;
77- $ doc ->loadXML ($ phpcsFile ->getTokensAsString (0 , 999999 ));
78- return $ doc ->saveXML ();
79- }
80- }
45+ }
0 commit comments