Skip to content

AC-661: Create phpcs static check for XmlTest #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 13, 2021
114 changes: 114 additions & 0 deletions Magento2/Sniffs/Legacy/WidgetXMLSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

use DOMDocument;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Test for obsolete nodes/attributes in the widget.xml
*/
class WidgetXMLSniff implements Sniff
{
private const ERROR_CODE_OBSOLETE = 'FoundObsoleteNode';
private const ERROR_CODE_FACTORY = 'FoundFactory';
private const ERROR_CODE_XML = 'WrongXML';

/**
* @inheritdoc
*/
public function register(): array
{
return [
T_INLINE_HTML
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($stackPtr > 0) {
return;
}

$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));

if ($xml === false) {
$this->invalidXML($phpcsFile, $stackPtr);
return;
}

$foundElements = $xml->xpath('/widgets/*[@type]');

foreach ($foundElements as $element) {
if (!property_exists($element->attributes(), 'type')) {
continue;
}
$type = $element['type'];
if (preg_match('/\//', $type)) {
$phpcsFile->addError(
"Factory name detected: {$type}.",
dom_import_simplexml($element)->getLineNo() - 1,
self::ERROR_CODE_FACTORY
);
}
}

$foundElements = $xml->xpath('/widgets/*/supported_blocks');
foreach ($foundElements as $element) {
$phpcsFile->addError(
"Obsolete node: <supported_blocks>. To be replaced with <supported_containers>",
dom_import_simplexml($element)->getLineNo() - 1,
self::ERROR_CODE_OBSOLETE
);
}

$foundElements = $xml->xpath('/widgets/*/*/*/block_name');
foreach ($foundElements as $element) {
$phpcsFile->addError(
"Obsolete node: <block_name>. To be replaced with <container_name>",
dom_import_simplexml($element)->getLineNo() - 1,
self::ERROR_CODE_OBSOLETE
);
}
}

/**
* Adds an invalid XML error
*
* @param File $phpcsFile
* @param int $stackPtr
*/
protected function invalidXML(File $phpcsFile, int $stackPtr): void
{
$phpcsFile->addError(
sprintf(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$phpcsFile->getFilename(),
),
$stackPtr,
self::ERROR_CODE_XML
);
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
* @param File $phpcsFile
* @return false|string
*/
private function getFormattedXML(File $phpcsFile)
{
$doc = new DomDocument('1.0');
$doc->formatOutput = true;
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
return $doc->saveXML();
}
}
19 changes: 19 additions & 0 deletions Magento2/Tests/Legacy/WidgetXMLUnitTest.1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<widgets>
<widget type="/Deprecated">
<label translate="true">Catalog New Products List</label>
<description translate="true">List of Products that are set as New</description>
<supported_blocks>Deprecated
<block>
<block_name>Deprecated</block_name>
</block>
</supported_blocks>
</widget>
</widgets>

33 changes: 33 additions & 0 deletions Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<widgets>
<widget
type="/Deprecated"
>
<label
translate="true"
>Catalog New Products List

</label>


<description translate="true">List of Products that are set as New</description>

<supported_blocks>


Deprecated


<block>
<block_name>Deprecated</block_name>
</block>
</supported_blocks>
</widget>
</widgets>

22 changes: 22 additions & 0 deletions Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<widgets>
<widget
>
<label
translate="true"
/>
<description
translate="true"
>
List of Products that are set as New
</description>

</widget>
</widgets>

41 changes: 41 additions & 0 deletions Magento2/Tests/Legacy/WidgetXMLUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class WidgetXMLUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList($testFile = '')
{
if ($testFile === 'WidgetXMLUnitTest.1.xml') {
return [
9 => 1,
12 => 1,
14 => 1,
];
}
if ($testFile === 'WidgetXMLUnitTest.2.xml') {
return [
9 => 1,
17 => 1,
24 => 1,
];
}
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = '')
{
return [];
}
}
5 changes: 5 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.WidgetXML.FoundObsoleteNode">
<include-pattern>*\/widget.xml$</include-pattern>
<severity>8</severity>
<type>warning</type>
</rule>

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