Skip to content

Commit dc56f12

Browse files
Merge pull request #84 from magento-commerce/imported-eliseacornejo-magento-coding-standard-296
[Imported] AC-679: Create phpcs static check for ObsoleteSystemConfigurationTest
2 parents 3771108 + f0af108 commit dc56f12

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
/**
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+
40+
if ($xml === false) {
41+
$this->invalidXML($phpcsFile, $stackPtr);
42+
return;
43+
}
44+
45+
$foundElements = $xml->xpath('/config/tabs|/config/sections');
46+
47+
if ($foundElements === false) {
48+
return;
49+
}
50+
51+
foreach ($foundElements as $element) {
52+
$phpcsFile->addWarning(
53+
"Obsolete system configuration structure detected in file.",
54+
dom_import_simplexml($element)->getLineNo() - 1,
55+
self::WARNING_CODE_OBSOLETE
56+
);
57+
}
58+
}
59+
60+
/**
61+
* Adds an invalid XML error
62+
*
63+
* @param File $phpcsFile
64+
* @param int $stackPtr
65+
*/
66+
private function invalidXML(File $phpcsFile, int $stackPtr): void
67+
{
68+
$phpcsFile->addError(
69+
sprintf(
70+
"Couldn't parse contents of '%s', check that they are in valid XML format.",
71+
$phpcsFile->getFilename(),
72+
),
73+
$stackPtr,
74+
self::ERROR_CODE_XML
75+
);
76+
}
77+
78+
/**
79+
* Format the incoming XML to avoid tags split into several lines.
80+
*
81+
* @param File $phpcsFile
82+
* @return false|string
83+
*/
84+
private function getFormattedXML(File $phpcsFile)
85+
{
86+
$doc = new DomDocument('1.0');
87+
$doc->formatOutput = true;
88+
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
89+
return $doc->saveXML();
90+
}
91+
}
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
@@ -297,6 +297,11 @@
297297
<severity>8</severity>
298298
<type>warning</type>
299299
</rule>
300+
<rule ref="Magento2.Legacy.ObsoleteSystemConfiguration">
301+
<include-pattern>etc/system.xml</include-pattern>
302+
<severity>8</severity>
303+
<type>warning</type>
304+
</rule>
300305

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

0 commit comments

Comments
 (0)