@@ -16,11 +16,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff
16
16
private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference ' ;
17
17
private const ERROR_CODE_CONFIG = 'IncorrectClassReference ' ;
18
18
private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference ' ;
19
- private const ERROR_CODE_MODULE = 'InforrectModuleReference ' ;
20
-
21
- private const FROM_CONTENT = 1 ;
22
- private const FROM_NAME = 2 ;
23
- private const FROM_ATTRIBUTE = 3 ;
19
+ private const ERROR_CODE_MODULE = 'IncorrectModuleReference ' ;
24
20
25
21
/**
26
22
* @inheritdoc
@@ -59,26 +55,26 @@ public function process(File $phpcsFile, $stackPtr)
59
55
$ classes = $ this ->collectClassesInConfig ($ xml );
60
56
$ this ->assertNonFactoryName ($ phpcsFile , $ classes );
61
57
62
- $ modules = $ this ->getValuesFromXml ($ xml , '//@module ' , self :: FROM_ATTRIBUTE , 'module ' );
58
+ $ modules = $ this ->getValuesFromXmlTagAttribute ($ xml , '//@module ' , 'module ' );
63
59
$ this ->assertNonFactoryNameModule ($ phpcsFile , $ modules );
64
60
}
65
61
66
62
/**
67
63
* Check whether specified class names are right according PSR-1 Standard.
68
64
*
69
65
* @param File $phpcsFile
70
- * @param ExtendedNode[] $elements
66
+ * @param array $elements
71
67
*/
72
68
private function assertNonFactoryName (File $ phpcsFile , array $ elements )
73
69
{
74
70
foreach ($ elements as $ element ) {
75
- if (stripos ($ element-> value , 'Magento ' ) === false ) {
71
+ if (stripos ($ element[ ' value ' ] , 'Magento ' ) === false ) {
76
72
continue ;
77
73
}
78
- if (preg_match ('/^([A-Z][a-z\d \\\\]+)+$/ ' , $ element-> value ) !== 1 ) {
74
+ if (preg_match ('/^([A-Z][a-z\d \\\\]+)+$/ ' , $ element[ ' value ' ] ) !== 1 ) {
79
75
$ phpcsFile ->addError (
80
76
self ::ERROR_MESSAGE_CONFIG ,
81
- $ element-> lineNumber - 1 ,
77
+ $ element[ ' lineNumber ' ] ,
82
78
self ::ERROR_CODE_CONFIG ,
83
79
);
84
80
}
@@ -89,15 +85,15 @@ private function assertNonFactoryName(File $phpcsFile, array $elements)
89
85
* Check whether specified class names in modules are right according PSR-1 Standard.
90
86
*
91
87
* @param File $phpcsFile
92
- * @param ExtendedNode[] $classes
88
+ * @param array $classes
93
89
*/
94
90
private function assertNonFactoryNameModule (File $ phpcsFile , array $ classes )
95
91
{
96
92
foreach ($ classes as $ element ) {
97
- if (preg_match ('/^([A-Z][A-Za-z\d_]+)+$/ ' , $ element-> value ) !== 1 ) {
93
+ if (preg_match ('/^([A-Z][A-Za-z\d_]+)+$/ ' , $ element[ ' value ' ] ) !== 1 ) {
98
94
$ phpcsFile ->addError (
99
95
self ::ERROR_MESSAGE_MODULE ,
100
- $ element-> lineNumber - 1 ,
96
+ $ element[ ' lineNumber ' ] ,
101
97
self ::ERROR_CODE_MODULE ,
102
98
);
103
99
}
@@ -126,45 +122,41 @@ private function getFormattedXML(File $phpcsFile)
126
122
*/
127
123
private function collectClassesInConfig (SimpleXMLElement $ xml ): array
128
124
{
129
- $ classes = $ this ->getValuesFromXml (
125
+ $ classes = $ this ->getValuesFromXmlTagContent (
130
126
$ xml ,
131
127
'
132
128
/config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)]
133
129
| //model[not(parent::connection)] | //backend_model | //source_model | //price_model
134
130
| //model_token | //writer_model | //clone_model | //frontend_model | //working_model
135
131
| //admin_renderer | //renderer ' ,
136
- self ::FROM_CONTENT
137
132
);
138
133
$ classes = array_merge (
139
134
$ classes ,
140
- $ this ->getValuesFromXml (
135
+ $ this ->getValuesFromXmlTagAttribute (
141
136
$ xml ,
142
137
'//@backend_model ' ,
143
- self ::FROM_ATTRIBUTE ,
144
138
'backend_model '
145
139
)
146
140
);
147
141
$ classes = array_merge (
148
142
$ classes ,
149
- $ this ->getValuesFromXml (
143
+ $ this ->getValuesFromXmlTagAttribute (
150
144
$ xml ,
151
145
'/config//preference ' ,
152
- self ::FROM_ATTRIBUTE ,
153
146
'type '
154
147
)
155
148
);
156
149
$ classes = array_merge (
157
150
$ classes ,
158
- $ this ->getValuesFromXml (
151
+ $ this ->getValuesFromXmlTagName (
159
152
$ xml ,
160
153
'/logging/*/expected_models/* | /logging/*/actions/*/expected_models/* ' ,
161
- self ::FROM_NAME
162
154
)
163
155
);
164
156
165
157
$ classes = array_map (
166
- function (ExtendedNode $ extendedNode ) {
167
- $ extendedNode-> value = explode (':: ' , trim ($ extendedNode-> value ))[0 ];
158
+ function (array $ extendedNode ) {
159
+ $ extendedNode[ ' value ' ] = explode (':: ' , trim ($ extendedNode[ ' value ' ] ))[0 ];
168
160
return $ extendedNode ;
169
161
},
170
162
$ classes
@@ -174,28 +166,59 @@ function (ExtendedNode $extendedNode) {
174
166
}
175
167
176
168
/**
177
- * Extract value from the specified $extractFrom which exist in the XML path
169
+ * Extract value from tag contents which exist in the XML path
170
+ *
171
+ * @param SimpleXMLElement $xml
172
+ * @param string $xPath
173
+ * @return array
174
+ */
175
+ private function getValuesFromXmlTagContent (SimpleXMLElement $ xml , string $ xPath ): array
176
+ {
177
+ $ nodes = $ xml ->xpath ($ xPath ) ?: [];
178
+ return array_map (function ($ item ) {
179
+ return [
180
+ 'value ' => (string )$ item ,
181
+ 'lineNumber ' => dom_import_simplexml ($ item )->getLineNo ()-1 ,
182
+ ];
183
+ }, $ nodes );
184
+ }
185
+
186
+ /**
187
+ * Extract value from tag names which exist in the XML path
188
+ *
189
+ * @param SimpleXMLElement $xml
190
+ * @param string $xPath
191
+ * @return array
192
+ */
193
+ private function getValuesFromXmlTagName (SimpleXMLElement $ xml , string $ xPath ): array
194
+ {
195
+ $ nodes = $ xml ->xpath ($ xPath ) ?: [];
196
+ return array_map (function ($ item ) {
197
+ return [
198
+ 'value ' => $ item ->getName (),
199
+ 'lineNumber ' => dom_import_simplexml ($ item )->getLineNo ()-1 ,
200
+ ];
201
+ }, $ nodes );
202
+ }
203
+
204
+ /**
205
+ * Extract value from tag attributes which exist in the XML path
178
206
*
179
207
* @param SimpleXMLElement $xml
180
208
* @param string $xPath
181
- * @param int $extractFrom
182
209
* @param string $attr
183
210
* @return array
184
211
*/
185
- private function getValuesFromXml (SimpleXMLElement $ xml , string $ xPath , int $ extractFrom , string $ attr = '' ): array
212
+ private function getValuesFromXmlTagAttribute (SimpleXMLElement $ xml , string $ xPath , string $ attr ): array
186
213
{
187
214
$ nodes = $ xml ->xpath ($ xPath ) ?: [];
188
- return array_map (function ($ item ) use ($ extractFrom , $ attr ) {
189
- switch ($ extractFrom ) {
190
- case self ::FROM_CONTENT :
191
- return new ExtendedNode ((string )$ item , $ item );
192
- case self ::FROM_NAME :
193
- return new ExtendedNode ($ item ->getName (), $ item );
194
- case self ::FROM_ATTRIBUTE :
195
- $ nodeArray = (array )$ item ;
196
- if (isset ($ nodeArray ['@attributes ' ][$ attr ])) {
197
- return new ExtendedNode ($ nodeArray ['@attributes ' ][$ attr ], $ item );
198
- }
215
+ return array_map (function ($ item ) use ($ attr ) {
216
+ $ nodeArray = (array )$ item ;
217
+ if (isset ($ nodeArray ['@attributes ' ][$ attr ])) {
218
+ return [
219
+ 'value ' => $ nodeArray ['@attributes ' ][$ attr ],
220
+ 'lineNumber ' => dom_import_simplexml ($ item )->getLineNo ()-1 ,
221
+ ];
199
222
}
200
223
}, $ nodes );
201
224
}
0 commit comments