-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathObsoleteSystemConfigurationSniff.php
91 lines (78 loc) · 2.17 KB
/
ObsoleteSystemConfigurationSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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;
class ObsoleteSystemConfigurationSniff implements Sniff
{
private const ERROR_CODE_XML = 'WrongXML';
private const WARNING_CODE_OBSOLETE = 'FoundObsoleteSystemConfiguration';
/**
* @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));
if ($xml === false) {
$this->invalidXML($phpcsFile, $stackPtr);
return;
}
$foundElements = $xml->xpath('/config/tabs|/config/sections');
if ($foundElements === false) {
return;
}
foreach ($foundElements as $element) {
$phpcsFile->addWarning(
"Obsolete system configuration structure detected in file.",
dom_import_simplexml($element)->getLineNo() - 1,
self::WARNING_CODE_OBSOLETE
);
}
}
/**
* Adds an invalid XML error
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function invalidXML(File $phpcsFile, int $stackPtr): void
{
$phpcsFile->addError(
sprintf(
"Couldn't parse contents of '%s', check that they are in valid XML format.",
$phpcsFile->getFilename(),
),
$stackPtr,
self::ERROR_CODE_XML
);
}
/**
* 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();
}
}