Skip to content

Commit 247578e

Browse files
committed
AC-679: Create phpcs static check for ObsoleteSystemConfigurationTest
1 parent 8f9269d commit 247578e

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use DOMDocument;
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
14+
class ObsoleteSystemConfigurationSniff implements Sniff
15+
{
16+
private const ERROR_CODE_XML = 'WrongXML';
17+
private const WARNING_CODE_OBSOLETE = 'FoundObsoleteSystemConfiguration';
18+
19+
public function register()
20+
{
21+
return [
22+
T_INLINE_HTML
23+
];
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function process(File $phpcsFile, $stackPtr)
30+
{
31+
if ($stackPtr > 0) {
32+
return;
33+
}
34+
35+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
36+
37+
if ($xml === false) {
38+
$this->invalidXML($phpcsFile, $stackPtr);
39+
return;
40+
}
41+
42+
$foundElements = $xml->xpath('/config/tabs|/config/sections');
43+
44+
if ($foundElements === false) {
45+
return;
46+
}
47+
48+
foreach ($foundElements as $element) {
49+
$phpcsFile->addWarning(
50+
"Obsolete system configuration structure detected in file.",
51+
dom_import_simplexml($element)->getLineNo() - 1,
52+
self::WARNING_CODE_OBSOLETE
53+
);
54+
}
55+
}
56+
57+
/**
58+
* Adds an invalid XML error
59+
*
60+
* @param File $phpcsFile
61+
* @param int $stackPtr
62+
*/
63+
protected function invalidXML(File $phpcsFile, int $stackPtr): void
64+
{
65+
$phpcsFile->addError(
66+
sprintf(
67+
"Couldn't parse contents of '%s', check that they are in valid XML format.",
68+
$phpcsFile->getFilename(),
69+
),
70+
$stackPtr,
71+
self::ERROR_CODE_XML
72+
);
73+
}
74+
75+
/**
76+
* Format the incoming XML to avoid tags split into several lines.
77+
*
78+
* @param File $phpcsFile
79+
* @return false|string
80+
*/
81+
private function getFormattedXML(File $phpcsFile)
82+
{
83+
$doc = new DomDocument('1.0');
84+
$doc->formatOutput = true;
85+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
86+
return $doc->saveXML();
87+
}
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
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:module:Magento_Config:etc/system_file.xsd">
9+
<sections>test sections</sections>
10+
<tabs>test tabs</tabs>
11+
</config>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
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:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<sections>test no error</sections>
11+
<tabs>test no error</tabs>
12+
<section id="admin">
13+
</section>
14+
</system>
15+
</config>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. 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 ObsoleteSystemConfigurationUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList($testFile = '')
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList($testFile = '')
24+
{
25+
if ($testFile === 'ObsoleteSystemConfigurationUnitTest.1.xml') {
26+
return [
27+
9 => 1,
28+
10 => 1,
29+
];
30+
}
31+
if ($testFile === 'ObsoleteSystemConfigurationUnitTest.2.xml') {
32+
return [];
33+
}
34+
return [];
35+
}
36+
}

Magento2/ruleset.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@
283283
<severity>8</severity>
284284
<type>warning</type>
285285
</rule>
286+
<rule ref="Magento2.Legacy.ObsoleteSystemConfiguration">
287+
<include-pattern>etc/system.xml</include-pattern>
288+
<severity>8</severity>
289+
<type>warning</type>
290+
</rule>
286291

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

0 commit comments

Comments
 (0)