Skip to content

Commit 8d23aa4

Browse files
committed
AC-659: Create phpcs static check for ModuleXMLTest
1 parent aa53065 commit 8d23aa4

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

Magento2/Sniffs/Legacy/ModuleXMLSniff.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use PHP_CodeSniffer\Files\File;
1010
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use SimpleXMLElement;
1112

1213
/**
1314
* Test for obsolete nodes/attributes in the module.xml
@@ -36,22 +37,52 @@ public function register(): array
3637
*/
3738
public function process(File $phpcsFile, $stackPtr)
3839
{
39-
$xml = simplexml_load_string($phpcsFile->getTokensAsString(0, 999999));
40+
$line = $phpcsFile->getTokens()[$stackPtr]['content'];
41+
if (strpos(trim($line), '<module ') === false) {
42+
return;
43+
}
4044

41-
if ($xml->xpath('/config/module/@version') !== false) {
42-
$phpcsFile->addWarning(
43-
'The "version" attribute is obsolete. Use "setup_version" instead.',
44-
$stackPtr,
45-
$this->warningCode
46-
);
45+
$xml = simplexml_load_string($phpcsFile->getTokensAsString(0, 999999));
46+
47+
$foundElements = $xml->xpath('/config/module');
48+
if ($foundElements === false) {
49+
return;
4750
}
4851

49-
if ($xml->xpath('/config/module/@active') !== false) {
50-
$phpcsFile->addWarning(
51-
'The "active" attribute is obsolete. The list of active modules is defined in deployment configuration.',
52-
$stackPtr,
53-
$this->warningCode
54-
);
52+
foreach ($foundElements as $element) {
53+
if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) {
54+
continue;
55+
}
56+
57+
if (property_exists($element->attributes(), 'version')) {
58+
$phpcsFile->addWarning(
59+
'The "version" attribute is obsolete. Use "setup_version" instead.',
60+
$stackPtr,
61+
$this->warningCode
62+
);
63+
}
64+
65+
if (property_exists($element->attributes(), 'active')) {
66+
$phpcsFile->addWarning(
67+
'The "active" attribute is obsolete. The list of active modules is defined in deployment configuration.',
68+
$stackPtr,
69+
$this->warningCode
70+
);
71+
}
72+
}
73+
}
74+
75+
/**
76+
* @param SimpleXMLElement $element
77+
* @param int $stackPtr
78+
* @return bool
79+
*/
80+
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool
81+
{
82+
$node = dom_import_simplexml($element);
83+
if ($node->getLineNo() === $stackPtr+1) {
84+
return true;
5585
}
86+
return false;
5687
}
5788
}

Magento2/Tests/Legacy/ModuleXMLUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function getErrorList()
2323
public function getWarningList()
2424
{
2525
return [
26-
1 => 2
26+
9 => 2
2727
];
2828
}
2929
}

Magento2/ruleset.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<description>Magento Coding Standard</description>
44

55
<!-- File extensions to be checked. -->
6-
<arg name="extensions" value="php,phtml,graphqls/GraphQL"/>
6+
<arg name="extensions" value="php,phtml,graphqls/GraphQL,xml"/>
77

88
<!-- Severity 10 errors: Critical code issues. -->
99
<rule ref="Generic.Functions.CallTimePassByReference">
@@ -224,9 +224,8 @@
224224
<type>warning</type>
225225
</rule>
226226
<rule ref="Magento2.Legacy.ModuleXML">
227-
<exclude-pattern>*\.php$</exclude-pattern>
228-
<include-pattern>module.xml</include-pattern>
229-
<include-pattern>ModuleXMLUnitTest.xml</include-pattern>
227+
<include-pattern>*\/module.xml$</include-pattern>
228+
<include-pattern>*\/ModuleXMLUnitTest.xml$</include-pattern>
230229
<severity>8</severity>
231230
<type>warning</type>
232231
</rule>
@@ -265,6 +264,7 @@
265264
<type>warning</type>
266265
</rule>
267266
<rule ref="Generic.PHP.DisallowShortOpenTag">
267+
<exclude-pattern>*\.xml$</exclude-pattern>
268268
<severity>7</severity>
269269
<type>warning</type>
270270
</rule>

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"php": ">=7.3",
1212
"squizlabs/php_codesniffer": "^3.6",
1313
"webonyx/graphql-php": "^14.9",
14-
"ext-simplexml": "*"
14+
"ext-simplexml": "*",
15+
"ext-dom": "*"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": "^9.5.8"

0 commit comments

Comments
 (0)