-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathClassReferencesInConfigurationFilesSniff.php
209 lines (188 loc) · 6.21 KB
/
ClassReferencesInConfigurationFilesSniff.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Legacy;
use DOMDocument;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use SimpleXMLElement;
class ClassReferencesInConfigurationFilesSniff implements Sniff
{
use ParseXMLTrait;
private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference';
private const ERROR_CODE_CONFIG = 'IncorrectClassReference';
private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference';
private const ERROR_CODE_MODULE = 'IncorrectModuleReference';
/**
* @inheritdoc
*/
public function register()
{
return [
T_INLINE_HTML,
];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($stackPtr > 0) {
return;
}
// We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement
// returns the position of the closing /> as the position of the tag, and we need the position of <
// instead, as it is the one we compare with $stackPtr later on.
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
if ($xml === false) {
return;
}
$classes = $this->collectClassesInConfig($xml);
$this->assertNonFactoryName($phpcsFile, $classes);
$modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module');
$this->assertNonFactoryNameModule($phpcsFile, $modules);
}
/**
* Check whether specified class names are right according PSR-1 Standard.
*
* @param File $phpcsFile
* @param array $elements
*/
private function assertNonFactoryName(File $phpcsFile, array $elements)
{
foreach ($elements as $element) {
if (stripos($element['value'], 'Magento') === false) {
continue;
}
if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) {
$phpcsFile->addError(
self::ERROR_MESSAGE_CONFIG,
$element['lineNumber'],
self::ERROR_CODE_CONFIG,
);
}
}
}
/**
* Check whether specified class names in modules are right according PSR-1 Standard.
*
* @param File $phpcsFile
* @param array $classes
*/
private function assertNonFactoryNameModule(File $phpcsFile, array $classes)
{
foreach ($classes as $element) {
if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element['value']) !== 1) {
$phpcsFile->addError(
self::ERROR_MESSAGE_MODULE,
$element['lineNumber'],
self::ERROR_CODE_MODULE,
);
}
}
}
/**
* Parse an XML for references to PHP class names in selected tags or attributes
*
* @param SimpleXMLElement $xml
*
* @return array
*/
private function collectClassesInConfig(SimpleXMLElement $xml): array
{
$classes = $this->getValuesFromXmlTagContent(
$xml,
'
/config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)]
| //model[not(parent::connection)] | //backend_model | //source_model | //price_model
| //model_token | //writer_model | //clone_model | //frontend_model | //working_model
| //admin_renderer | //renderer',
);
$classes = array_merge(
$classes,
$this->getValuesFromXmlTagAttribute(
$xml,
'//@backend_model',
'backend_model'
),
$this->getValuesFromXmlTagAttribute(
$xml,
'/config//preference',
'type'
),
$this->getValuesFromXmlTagName(
$xml,
'/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*',
)
);
$classes = array_map(
function (array $extendedNode) {
$extendedNode['value'] = explode('::', trim($extendedNode['value']))[0];
return $extendedNode;
},
$classes
);
return $classes;
}
/**
* Extract value from tag contents which exist in the XML path
*
* @param SimpleXMLElement $xml
* @param string $xPath
*
* @return array
*/
private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array
{
$nodes = $xml->xpath($xPath) ?: [];
return array_map(function ($item) {
return [
'value' => (string)$item,
'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1,
];
}, $nodes);
}
/**
* Extract value from tag names which exist in the XML path
*
* @param SimpleXMLElement $xml
* @param string $xPath
*
* @return array
*/
private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): array
{
$nodes = $xml->xpath($xPath) ?: [];
return array_map(function ($item) {
return [
'value' => $item->getName(),
'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1,
];
}, $nodes);
}
/**
* Extract value from tag attributes which exist in the XML path
*
* @param SimpleXMLElement $xml
* @param string $xPath
* @param string $attr
*
* @return array
*/
private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array
{
$nodes = $xml->xpath($xPath) ?: [];
return array_map(function ($item) use ($attr) {
$nodeArray = (array)$item;
if (isset($nodeArray['@attributes'][$attr])) {
return [
'value' => $nodeArray['@attributes'][$attr],
'lineNumber' => dom_import_simplexml($item)->getLineNo() - 1,
];
}
}, $nodes);
}
}