Skip to content

Commit d753278

Browse files
authored
Merge pull request #50 from magento-commerce/imported-magento-magento-coding-standard-230
[Imported] AC-659 Create phpcs static check for ModuleXMLTest
2 parents 2ce314b + 47a0536 commit d753278

8 files changed

+201
-3
lines changed
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
use SimpleXMLElement;
13+
14+
/**
15+
* Test for obsolete nodes/attributes in the module.xml
16+
*/
17+
class ModuleXMLSniff implements Sniff
18+
{
19+
private const WARNING_CODE = 'FoundObsoleteAttribute';
20+
private const ERROR_CODE = 'WrongXML';
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function register(): array
26+
{
27+
return [
28+
T_INLINE_HTML
29+
];
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function process(File $phpcsFile, $stackPtr)
36+
{
37+
$line = $phpcsFile->getTokens()[$stackPtr]['content'];
38+
if (strpos(trim($line), '<module') === false) {
39+
return;
40+
}
41+
42+
// We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement
43+
// returns the position of the closing /> as the position of the tag, and we need the position of <module
44+
// instead, as it is the one we compare with $stackPtr later on.
45+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
46+
if ($xml === false) {
47+
$phpcsFile->addError(
48+
sprintf(
49+
"Couldn't parse contents of '%s', check that they are in valid XML format",
50+
$phpcsFile->getFilename(),
51+
),
52+
$stackPtr,
53+
self::ERROR_CODE
54+
);
55+
}
56+
57+
$foundElements = $xml->xpath('/config/module');
58+
if ($foundElements === false) {
59+
return;
60+
}
61+
62+
foreach ($foundElements as $element) {
63+
if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) {
64+
continue;
65+
}
66+
67+
if (property_exists($element->attributes(), 'version')) {
68+
$phpcsFile->addWarning(
69+
'The "version" attribute is obsolete. Use "setup_version" instead.',
70+
$stackPtr,
71+
self::WARNING_CODE
72+
);
73+
}
74+
75+
if (property_exists($element->attributes(), 'active')) {
76+
$phpcsFile->addWarning(
77+
'The "active" attribute is obsolete. The list of active modules '.
78+
'is defined in deployment configuration.',
79+
$stackPtr,
80+
self::WARNING_CODE
81+
);
82+
}
83+
}
84+
}
85+
86+
/**
87+
* Check if the element passed is in the currently sniffed line
88+
*
89+
* @param SimpleXMLElement $element
90+
* @param int $stackPtr
91+
* @return bool
92+
*/
93+
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool
94+
{
95+
$node = dom_import_simplexml($element);
96+
if ($node->getLineNo() === $stackPtr+1) {
97+
return true;
98+
}
99+
return false;
100+
}
101+
102+
/**
103+
* Format the incoming XML to avoid tags split into several lines.
104+
*
105+
* @param File $phpcsFile
106+
* @return false|string
107+
*/
108+
private function getFormattedXML(File $phpcsFile)
109+
{
110+
$doc = new DomDocument('1.0');
111+
$doc->formatOutput = true;
112+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
113+
return $doc->saveXML();
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_TestModule" active="true" version="1" />
10+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module
10+
name="Magento_TestModule2"
11+
active="true"
12+
version="1"
13+
/>
14+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_TestModule2" />
10+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 ModuleXMLUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList()
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList($testFile = '')
24+
{
25+
if ($testFile === 'ModuleXMLUnitTest.1.xml') {
26+
return [
27+
9 => 2,
28+
];
29+
}
30+
if ($testFile === 'ModuleXMLUnitTest.2.xml') {
31+
return [
32+
9 => 2,
33+
];
34+
}
35+
if ($testFile === 'ModuleXMLUnitTest.3.xml') {
36+
return [];
37+
}
38+
return [];
39+
}
40+
}

Magento2/ruleset.xml

+5
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.ModuleXML">
227+
<include-pattern>*\/module.xml$</include-pattern>
228+
<severity>8</severity>
229+
<type>warning</type>
230+
</rule>
226231
<rule ref="Magento2.Legacy.DiConfig">
227232
<include-pattern>*\/di.xml$</include-pattern>
228233
<severity>8</severity>

composer.json

+3-1
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"

composer.lock

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)