Skip to content

Commit 702cd60

Browse files
committed
AC-661: Create phpcs static check for XmlTest
1 parent e3f4d87 commit 702cd60

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 114 additions & 0 deletions
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 module.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+
if ($xml === false) {
43+
$phpcsFile->addError(
44+
sprintf(
45+
"Couldn't parse contents of '%s', check that they are in valid XML format",
46+
$phpcsFile->getFilename(),
47+
),
48+
1,
49+
self::ERROR_CODE_XML
50+
);
51+
}
52+
53+
$foundElements = $xml->xpath('/widgets/*[@type]');
54+
55+
foreach ($foundElements as $element) {
56+
if (property_exists($element->attributes(), 'type')) {
57+
$type = $element['type'];
58+
if (preg_match('/\//', $type)) {
59+
$phpcsFile->addError(
60+
"Factory name detected: {$type}.",
61+
dom_import_simplexml($element)->getLineNo() - 1,
62+
self::ERROR_CODE_FACTORY
63+
);
64+
}
65+
}
66+
}
67+
68+
$foundElements = $xml->xpath('/widgets/*/supported_blocks');
69+
foreach ($foundElements as $element) {
70+
$phpcsFile->addError(
71+
"Obsolete node: <supported_blocks>. To be replaced with <supported_containers>",
72+
dom_import_simplexml($element)->getLineNo() - 1,
73+
self::ERROR_CODE_OBSOLETE
74+
);
75+
}
76+
77+
$foundElements = $xml->xpath('/widgets/*/*/*/block_name');
78+
foreach ($foundElements as $element) {
79+
$phpcsFile->addError(
80+
"Obsolete node: <block_name>. To be replaced with <container_name>",
81+
dom_import_simplexml($element)->getLineNo() - 1,
82+
self::ERROR_CODE_OBSOLETE
83+
);
84+
}
85+
}
86+
87+
/**
88+
* Check if the element passed is in the currently sniffed line
89+
*
90+
* @param SimpleXMLElement $element
91+
* @param int $stackPtr
92+
* @return bool
93+
*/
94+
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool
95+
{
96+
$node = dom_import_simplexml($element);
97+
98+
return $node->getLineNo() === $stackPtr + 1;
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+
}
Lines changed: 19 additions & 0 deletions
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+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
return [
18+
9 => 1,
19+
12 => 1,
20+
14 => 1,
21+
];
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function getWarningList($testFile = '')
28+
{
29+
return [];
30+
}
31+
}

0 commit comments

Comments
 (0)