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+ class DiConfigSniff implements Sniff
14+ {
15+ private const WARNING_CODE = 'FoundObsoleteAttribute ' ;
16+ private const ERROR_CODE = 'WrongXML ' ;
17+
18+ 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. '
32+ ];
33+
34+ public function register (): array
35+ {
36+ return [
37+ T_INLINE_HTML
38+ ];
39+ }
40+
41+ public function process (File $ phpcsFile , $ stackPtr ): int
42+ {
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+ }
54+
55+ foreach ($ this ->xpathObsoleteElems as $ obsoleteElem ) {
56+ $ found = $ xml ->xpath ($ obsoleteElem );
57+ if ($ found === true ) {
58+ $ phpcsFile ->addWarning (
59+ $ this ->messages [$ obsoleteElem ],
60+ $ stackPtr ,
61+ self ::WARNING_CODE
62+ );
63+ }
64+ }
65+ }
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+ }
0 commit comments