Skip to content

AC-659 Create phpcs static check for ModuleXMLTest #230

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 16 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions Magento2/Sniffs/Legacy/ModuleXMLSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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;
use SimpleXMLElement;

/**
* Test for obsolete nodes/attributes in the module.xml
*/
class ModuleXMLSniff implements Sniff
{
private const WARNING_CODE = 'FoundObsoleteAttribute';
private const ERROR_CODE = 'WrongXML';

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

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$line = $phpcsFile->getTokens()[$stackPtr]['content'];
if (strpos(trim($line), '<module') === false) {
return;
}

// We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement
// returns the position of the closing /> as the position of the tag, and we need the position of <module
// instead, as it is the one we compare with $stackPtr later on.
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
if ($xml === false) {
$phpcsFile->addError(
sprintf(
"Couldn't parse contents of '%s', check that they are in valid XML format",
$phpcsFile->getFilename(),
),
$stackPtr,
self::ERROR_CODE
);
}

$foundElements = $xml->xpath('/config/module');
if ($foundElements === false) {
return;
}

foreach ($foundElements as $element) {
if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) {
continue;
}

if (property_exists($element->attributes(), 'version')) {
$phpcsFile->addWarning(
'The "version" attribute is obsolete. Use "setup_version" instead.',
$stackPtr,
self::WARNING_CODE
);
}

if (property_exists($element->attributes(), 'active')) {
$phpcsFile->addWarning(
'The "active" attribute is obsolete. The list of active modules '.
'is defined in deployment configuration.',
$stackPtr,
self::WARNING_CODE
);
}
}
}

/**
* Check if the element passed is in the currently sniffed line
*
* @param SimpleXMLElement $element
* @param int $stackPtr
* @return bool
*/
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool
{
$node = dom_import_simplexml($element);
if ($node->getLineNo() === $stackPtr+1) {
return true;
}
return false;
}

/**
* 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();
}
}
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/ModuleXMLUnitTest.1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_TestModule" active="true" version="1" />
</config>
14 changes: 14 additions & 0 deletions Magento2/Tests/Legacy/ModuleXMLUnitTest.2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module
name="Magento_TestModule2"
active="true"
version="1"
/>
</config>
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/ModuleXMLUnitTest.3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_TestModule2" />
</config>
40 changes: 40 additions & 0 deletions Magento2/Tests/Legacy/ModuleXMLUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ModuleXMLUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = '')
{
if ($testFile === 'ModuleXMLUnitTest.1.xml') {
return [
9 => 2,
];
}
if ($testFile === 'ModuleXMLUnitTest.2.xml') {
return [
9 => 2,
];
}
if ($testFile === 'ModuleXMLUnitTest.3.xml') {
return [];
}
return [];
}
}
5 changes: 5 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.ModuleXML">
<include-pattern>*\/module.xml$</include-pattern>
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.DiConfig">
<include-pattern>*\/di.xml$</include-pattern>
<severity>8</severity>
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"require": {
"php": ">=7.3",
"squizlabs/php_codesniffer": "^3.6",
"webonyx/graphql-php": "^14.9"
"webonyx/graphql-php": "^14.9",
"ext-simplexml": "*",
"ext-dom": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5.8"
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.