Skip to content

Commit 9b4da0c

Browse files
authored
Merge pull request #60 from magento-commerce/imported-magento-magento-coding-standard-266
[Imported] AC-661: Create phpcs static check for XmlTest
2 parents e3f4d87 + dae1ab3 commit 9b4da0c

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
/**
14+
* Test for obsolete nodes/attributes in the widget.xml
15+
*/
16+
class WidgetXMLSniff implements Sniff
17+
{
18+
private const ERROR_CODE_OBSOLETE = 'FoundObsoleteNode';
19+
private const ERROR_CODE_FACTORY = 'FoundFactory';
20+
private const ERROR_CODE_XML = '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+
if ($stackPtr > 0) {
38+
return;
39+
}
40+
41+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
42+
43+
if ($xml === false) {
44+
$this->invalidXML($phpcsFile, $stackPtr);
45+
return;
46+
}
47+
48+
$foundElements = $xml->xpath('/widgets/*[@type]');
49+
50+
foreach ($foundElements as $element) {
51+
if (!property_exists($element->attributes(), 'type')) {
52+
continue;
53+
}
54+
$type = $element['type'];
55+
if (preg_match('/\//', $type)) {
56+
$phpcsFile->addError(
57+
"Factory name detected: {$type}.",
58+
dom_import_simplexml($element)->getLineNo() - 1,
59+
self::ERROR_CODE_FACTORY
60+
);
61+
}
62+
}
63+
64+
$foundElements = $xml->xpath('/widgets/*/supported_blocks');
65+
foreach ($foundElements as $element) {
66+
$phpcsFile->addError(
67+
"Obsolete node: <supported_blocks>. To be replaced with <supported_containers>",
68+
dom_import_simplexml($element)->getLineNo() - 1,
69+
self::ERROR_CODE_OBSOLETE
70+
);
71+
}
72+
73+
$foundElements = $xml->xpath('/widgets/*/*/*/block_name');
74+
foreach ($foundElements as $element) {
75+
$phpcsFile->addError(
76+
"Obsolete node: <block_name>. To be replaced with <container_name>",
77+
dom_import_simplexml($element)->getLineNo() - 1,
78+
self::ERROR_CODE_OBSOLETE
79+
);
80+
}
81+
}
82+
83+
/**
84+
* Adds an invalid XML error
85+
*
86+
* @param File $phpcsFile
87+
* @param int $stackPtr
88+
*/
89+
protected function invalidXML(File $phpcsFile, int $stackPtr): void
90+
{
91+
$phpcsFile->addError(
92+
sprintf(
93+
"Couldn't parse contents of '%s', check that they are in valid XML format",
94+
$phpcsFile->getFilename(),
95+
),
96+
$stackPtr,
97+
self::ERROR_CODE_XML
98+
);
99+
}
100+
101+
/**
102+
* Format the incoming XML to avoid tags split into several lines.
103+
*
104+
* @param File $phpcsFile
105+
* @return false|string
106+
*/
107+
private function getFormattedXML(File $phpcsFile)
108+
{
109+
$doc = new DomDocument('1.0');
110+
$doc->formatOutput = true;
111+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
112+
return $doc->saveXML();
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<widgets>
9+
<widget type="/Deprecated">
10+
<label translate="true">Catalog New Products List</label>
11+
<description translate="true">List of Products that are set as New</description>
12+
<supported_blocks>Deprecated
13+
<block>
14+
<block_name>Deprecated</block_name>
15+
</block>
16+
</supported_blocks>
17+
</widget>
18+
</widgets>
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<widgets>
9+
<widget
10+
type="/Deprecated"
11+
>
12+
<label
13+
translate="true"
14+
>Catalog New Products List
15+
16+
</label>
17+
18+
19+
<description translate="true">List of Products that are set as New</description>
20+
21+
<supported_blocks>
22+
23+
24+
Deprecated
25+
26+
27+
<block>
28+
<block_name>Deprecated</block_name>
29+
</block>
30+
</supported_blocks>
31+
</widget>
32+
</widgets>
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<widgets>
9+
<widget
10+
>
11+
<label
12+
translate="true"
13+
/>
14+
<description
15+
translate="true"
16+
>
17+
List of Products that are set as New
18+
</description>
19+
20+
</widget>
21+
</widgets>
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 WidgetXMLUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList($testFile = '')
16+
{
17+
if ($testFile === 'WidgetXMLUnitTest.1.xml') {
18+
return [
19+
9 => 1,
20+
12 => 1,
21+
14 => 1,
22+
];
23+
}
24+
if ($testFile === 'WidgetXMLUnitTest.2.xml') {
25+
return [
26+
9 => 1,
27+
17 => 1,
28+
24 => 1,
29+
];
30+
}
31+
return [];
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getWarningList($testFile = '')
38+
{
39+
return [];
40+
}
41+
}

Magento2/ruleset.xml

+5
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@
247247
<severity>8</severity>
248248
<type>warning</type>
249249
</rule>
250+
<rule ref="Magento2.Legacy.WidgetXML.FoundObsoleteNode">
251+
<include-pattern>*\/widget.xml$</include-pattern>
252+
<severity>8</severity>
253+
<type>warning</type>
254+
</rule>
250255

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

0 commit comments

Comments
 (0)