Skip to content

Commit f0b61be

Browse files
committed
AC-660: Create phpcs static check for DiConfigTest
- DiConfigTest: Test for obsolete nodes in di.xml
1 parent a636ed7 commit f0b61be

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Legacy;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class DiConfigUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList(): array
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList(): array
24+
{
25+
return [
26+
9 => 1,
27+
10 => 1,
28+
11 => 1,
29+
12 => 1,
30+
13 => 1,
31+
15 => 1
32+
];
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<root>
9+
<instance>
10+
<param name="param1" new_attribute="2">
11+
<array key="amounts">
12+
<item key="item1" new_attribute="3">
13+
<value new_attribute="4">scalar20</value>
14+
</item>
15+
<item>50.00</item>
16+
</array>
17+
</param>
18+
</instance>
19+
</root>

Magento2/ruleset.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@
223223
<severity>8</severity>
224224
<type>warning</type>
225225
</rule>
226+
<rule ref="Magento2.Legacy.DiConfig">
227+
<severity>8</severity>
228+
<type>warning</type>
229+
<include-pattern>*\/DiConfigUnitTest.xml$</include-pattern>
230+
</rule>
226231

227232
<!-- Severity 7 warnings: General code issues. -->
228233
<rule ref="Generic.Arrays.DisallowLongArraySyntax">

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"require": {
1111
"php": ">=7.3",
1212
"squizlabs/php_codesniffer": "^3.6",
13-
"webonyx/graphql-php": "^14.9"
13+
"webonyx/graphql-php": "^14.9",
14+
"ext-simplexml": "*",
15+
"ext-dom": "*"
1416
},
1517
"require-dev": {
1618
"phpunit/phpunit": "^9.5.8"

0 commit comments

Comments
 (0)