Skip to content

Commit d08c40f

Browse files
committed
AC-660: Create phpcs static check for DiConfigTest
- Refactor: Simplify approach
1 parent 5e4b15b commit d08c40f

File tree

6 files changed

+19
-60
lines changed

6 files changed

+19
-60
lines changed

Magento2/Sniffs/Legacy/DiConfigSniff.php

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,19 @@
66

77
namespace Magento2\Sniffs\Legacy;
88

9-
use DOMDocument;
109
use PHP_CodeSniffer\Files\File;
1110
use PHP_CodeSniffer\Sniffs\Sniff;
1211

1312
class 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+
}

Magento2/Tests/Legacy/DiConfigUnitTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ public function getErrorList(): array
2323
public function getWarningList(): array
2424
{
2525
return [
26-
9 => 1,
27-
10 => 1,
28-
11 => 1,
2926
12 => 1,
30-
13 => 1,
31-
15 => 1
27+
16 => 1,
28+
17 => 1,
29+
18 => 1,
30+
19 => 1
3231
];
3332
}
34-
}
33+
}

Magento2/Tests/Legacy/DiConfigUnitTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
</param>
2121
</array>
2222
</instance>
23-
</config>
23+
</config>

Magento2/ruleset.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@
225225
</rule>
226226
<rule ref="Magento2.Legacy.DiConfig">
227227
<include-pattern>*\/di.xml$</include-pattern>
228-
<include-pattern>*\/DiConfigUnitTest.xml$</include-pattern>
229228
<severity>8</severity>
230229
<type>warning</type>
231230
</rule>

composer.json

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

composer.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)