Skip to content

Commit b2111fa

Browse files
authored
Merge pull request #82 from magento-commerce/imported-loginesta-magento-coding-standard-285
[Imported] AC-674: Create phpcs static check for ObsoleteAclTest
2 parents 8f9269d + ffdf5a8 commit b2111fa

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
12+
/**
13+
* Test to find obsolete acl declaration
14+
*/
15+
class ObsoleteAclSniff implements Sniff
16+
{
17+
private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure';
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function register(): array
23+
{
24+
return [
25+
T_INLINE_HTML
26+
];
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function process(File $phpcsFile, $stackPtr)
33+
{
34+
if ($stackPtr > 0) {
35+
return;
36+
}
37+
38+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
39+
$foundElements = $xml->xpath('/config/acl/*[boolean(./children) or boolean(./title)]');
40+
foreach ($foundElements as $element) {
41+
$phpcsFile->addWarning(
42+
'Obsolete acl structure detected in line ' . dom_import_simplexml($element)->getLineNo(),
43+
dom_import_simplexml($element)->getLineNo() - 1,
44+
self::WARNING_OBSOLETE_ACL_STRUCTURE
45+
);
46+
}
47+
}
48+
49+
/**
50+
* Format the incoming XML to avoid tags split into several lines.
51+
*
52+
* @param File $phpcsFile
53+
* @return false|string
54+
*/
55+
private function getFormattedXML(File $phpcsFile)
56+
{
57+
$doc = new DomDocument('1.0');
58+
$doc->formatOutput = true;
59+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
60+
return $doc->saveXML();
61+
}
62+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
23 => 1
29+
];
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<children>
24+
<title
25+
value="foo"/>
26+
</children>
27+
</acl>
28+
</config>

Magento2/ruleset.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@
283283
<severity>8</severity>
284284
<type>warning</type>
285285
</rule>
286+
<rule ref="Magento2.Legacy.ObsoleteAcl">
287+
<include-pattern>etc/config.xml</include-pattern>
288+
<include-pattern>etc/config.*.xml</include-pattern>
289+
<include-pattern>etc/*/config.xml</include-pattern>
290+
<severity>8</severity>
291+
<type>warning</type>
292+
</rule>
286293

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

0 commit comments

Comments
 (0)