Skip to content

Commit 8afc287

Browse files
committed
AC-674: Create phpcs static check for ObsoleteAclTest
1 parent 5df47dc commit 8afc287

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Legacy;
7+
8+
use DOMDocument;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use SimpleXMLElement;
12+
13+
/**
14+
* Test to find obsolete acl declaration
15+
*/
16+
class ObsoleteAclSniff implements Sniff
17+
{
18+
private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure';
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function register(): array
24+
{
25+
return [
26+
T_INLINE_HTML
27+
];
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function process(File $phpcsFile, $stackPtr)
34+
{
35+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
36+
$foundElements = $xml->xpath('/config/acl/*[boolean(./children) or boolean(./title)]');
37+
foreach ($foundElements as $element) {
38+
if (!$this->elementIsCurrentlySniffedLine($element, $stackPtr)) {
39+
continue;
40+
}
41+
42+
$phpcsFile->addWarning(
43+
'Obsolete acl structure detected in line ' . ($stackPtr + 1),
44+
$stackPtr,
45+
self::WARNING_OBSOLETE_ACL_STRUCTURE
46+
);
47+
}
48+
}
49+
50+
/**
51+
* Check if the element passed is in the currently sniffed line
52+
*
53+
* @param SimpleXMLElement $element
54+
* @param int $stackPtr
55+
* @return bool
56+
*/
57+
private function elementIsCurrentlySniffedLine(SimpleXMLElement $element, int $stackPtr): bool
58+
{
59+
$node = dom_import_simplexml($element);
60+
if ($node->getLineNo() === $stackPtr+1) {
61+
return true;
62+
}
63+
return false;
64+
}
65+
66+
/**
67+
* Format the incoming XML to avoid tags split into several lines.
68+
*
69+
* @param File $phpcsFile
70+
* @return false|string
71+
*/
72+
private function getFormattedXML(File $phpcsFile)
73+
{
74+
$doc = new DomDocument('1.0');
75+
$doc->formatOutput = true;
76+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
77+
return $doc->saveXML();
78+
}
79+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. 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 ObsoleteAclUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList(): array
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList(): array
24+
{
25+
return [
26+
15 => 1,
27+
20 => 1
28+
];
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
9+
<children>
10+
<title value="foo"/>
11+
</children>
12+
<title value="bar"/>
13+
<children default_policy="deny"/>
14+
<acl>
15+
<children>
16+
<title value="foo"/>
17+
</children>
18+
<children default_policy="deny"/>
19+
<title value="bar"/>
20+
<title>
21+
<children default_policy="deny"/>
22+
</title>
23+
</acl>
24+
</config>

Magento2/ruleset.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@
285285
<severity>8</severity>
286286
<type>warning</type>
287287
</rule>
288+
<rule ref="Magento2.Legacy.ObsoleteAcl">
289+
<include-pattern>*\/widget.xml$</include-pattern>
290+
<severity>8</severity>
291+
<type>warning</type>
292+
</rule>
288293

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

0 commit comments

Comments
 (0)