Skip to content

Commit aca6b41

Browse files
committed
AC-659: Create phpcs static check for ModuleXMLTest
1 parent c2786fe commit aca6b41

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

Magento2/Sniffs/Legacy/ModuleXMLSniff.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento2\Sniffs\Legacy;
88

9+
use DOMDocument;
910
use PHP_CodeSniffer\Files\File;
1011
use PHP_CodeSniffer\Sniffs\Sniff;
1112
use SimpleXMLElement;
@@ -15,10 +16,8 @@
1516
*/
1617
class 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
}

Magento2/Tests/Legacy/ModuleXMLUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function getErrorList()
2323
public function getWarningList()
2424
{
2525
return [
26-
9 => 2
26+
9 => 2,
27+
10 => 2
2728
];
2829
}
2930
}

Magento2/Tests/Legacy/ModuleXMLUnitTest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
99
<module name="Magento_TestModule" active="true" version="1" />
10+
<module
11+
name="Magento_TestModule2"
12+
active="true"
13+
version="1"
14+
/>
1015
</config>

0 commit comments

Comments
 (0)