Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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 to find obsolete acl declaration
*/
class ObsoleteMenuSniff implements Sniff
{
private const WARNING_OBSOLETE_MENU_STRUCTURE = 'ObsoleteMenuStructure';

/**
* @var string
*/
private $xpath = '/config/menu/*[boolean(./children) or boolean(./title) or boolean(./action)]';

/**
* @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));
$foundElements = $xml->xpath($this->xpath);
foreach ($foundElements as $element) {
$phpcsFile->addWarning(
'Obsolete menu structure detected in line ' . dom_import_simplexml($element)->getLineNo(),
dom_import_simplexml($element)->getLineNo() - 1,
self::WARNING_OBSOLETE_MENU_STRUCTURE
);
}
}

/**
* 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();
}
}
38 changes: 38 additions & 0 deletions Magento2/Tests/Legacy/ObsoleteMenuUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

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

/**
* @inheritdoc
*/
public function getWarningList(): array
{
return [
17 => 1,
20 => 1,
23 => 1,
28 => 1,
31 => 1,
34 => 1,
39 => 1,
42 => 1,
45 => 1,
49 => 1
];
}
}
59 changes: 59 additions & 0 deletions Magento2/Tests/Legacy/ObsoleteMenuUnitTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* 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:Acl/etc/acl.xsd">
<children>
<title value="foo" />
</children>
<title value="bar" />
<children default_policy="deny" />
<action method="random" />
<menu>
<children default_policy="deny" />
<children>
<title value="foo" />
</children>
<children>
<action method="random" />
</children>
<children>
<title value="foo" />
<action method="random" />
</children>
<title value="bar" />
<title>
<children default_policy="deny" />
</title>
<title>
<action method="random" />
</title>
<title>
<children default_policy="deny" />
<action method="random" />
</title>
<action method="random" />
<action>
<title value="bar" />
</action>
<action>
<children default_policy="deny" />
</action>
<action>
<children default_policy="deny" />
<title value="foo" />
</action>
<action
>
<children
default_policy="deny"/>

<title
value="foo"
/>
</action>
</menu>
</config>
7 changes: 7 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.ObsoleteMenu">
<include-pattern>etc/config.xml</include-pattern>
<include-pattern>etc/config.*.xml</include-pattern>
<include-pattern>etc/*/config.xml</include-pattern>
<severity>8</severity>
<type>warning</type>
</rule>

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