@@ -18,6 +18,10 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff
18
18
private const ERROR_MESSAGE_MODULE = 'Attribute does not follow expected format in module ' ;
19
19
private const ERROR_CODE_MODULE = 'WrongXMLModule ' ;
20
20
21
+ private const FROM_CONTENT = 1 ;
22
+ private const FROM_NAME = 2 ;
23
+ private const FROM_ATTRIBUTE = 3 ;
24
+
21
25
/**
22
26
* @inheritdoc
23
27
*/
@@ -55,7 +59,7 @@ public function process(File $phpcsFile, $stackPtr)
55
59
$ classes = $ this ->collectClassesInConfig ($ xml );
56
60
$ this ->assertNonFactoryName ($ phpcsFile , $ classes );
57
61
58
- $ modules = $ this ->getXmlAttributeValues ($ xml , '//@module ' , 'module ' );
62
+ $ modules = $ this ->getValuesFromXml ($ xml , '//@module ' , self :: FROM_ATTRIBUTE , 'module ' );
59
63
$ this ->assertNonFactoryNameModule ($ phpcsFile , $ modules );
60
64
}
61
65
@@ -122,21 +126,39 @@ private function getFormattedXML(File $phpcsFile)
122
126
*/
123
127
private function collectClassesInConfig (SimpleXMLElement $ xml ): array
124
128
{
125
- $ classes = $ this ->getXmlNode (
129
+ $ classes = $ this ->getValuesFromXml (
126
130
$ xml ,
127
131
'
128
132
/config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)]
129
133
| //model[not(parent::connection)] | //backend_model | //source_model | //price_model
130
134
| //model_token | //writer_model | //clone_model | //frontend_model | //working_model
131
- | //admin_renderer | //renderer '
135
+ | //admin_renderer | //renderer ' ,
136
+ self ::FROM_CONTENT
137
+ );
138
+ $ classes = array_merge (
139
+ $ classes ,
140
+ $ this ->getValuesFromXml (
141
+ $ xml ,
142
+ '//@backend_model ' ,
143
+ self ::FROM_ATTRIBUTE ,
144
+ 'backend_model '
145
+ )
132
146
);
133
- $ classes = array_merge ($ classes , $ this ->getXmlAttributeValues ($ xml , '//@backend_model ' , 'backend_model ' ));
134
- $ classes = array_merge ($ classes , $ this ->getXmlAttributeValues ($ xml , '/config//preference ' , 'type ' ));
135
147
$ classes = array_merge (
136
148
$ classes ,
137
- $ this ->getXmlNodeNames (
149
+ $ this ->getValuesFromXml (
138
150
$ xml ,
139
- '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/* '
151
+ '/config//preference ' ,
152
+ self ::FROM_ATTRIBUTE ,
153
+ 'type '
154
+ )
155
+ );
156
+ $ classes = array_merge (
157
+ $ classes ,
158
+ $ this ->getValuesFromXml (
159
+ $ xml ,
160
+ '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/* ' ,
161
+ self ::FROM_NAME
140
162
)
141
163
);
142
164
@@ -147,70 +169,34 @@ function (ExtendedNode $extendedNode) {
147
169
},
148
170
$ classes
149
171
);
150
- $ classes = array_filter (
151
- $ classes ,
152
- function ($ value ) {
153
- return !empty ($ value );
154
- }
155
- );
156
172
157
173
return $ classes ;
158
174
}
159
175
160
176
/**
161
- * Get XML node text values using specified xPath
162
- *
163
- * The node must contain specified attribute
164
- *
177
+ * Extract value from the specified $extractFrom which exist in the XML path
178
+ *
165
179
* @param SimpleXMLElement $xml
166
180
* @param string $xPath
181
+ * @param int $extractFrom
182
+ * @param string $attribute
167
183
* @return array
168
184
*/
169
- private function getXmlNode (SimpleXMLElement $ xml , string $ xPath ): array
185
+ private function getValuesFromXml (SimpleXMLElement $ xml , string $ xPath, int $ extractFrom , string $ attribute = '' ): array
170
186
{
171
- $ result = [];
172
187
$ nodes = $ xml ->xpath ($ xPath ) ?: [];
173
- foreach ($ nodes as $ node ) {
174
- $ result [] = new ExtendedNode ((string )$ node , $ node );
175
- }
176
- return $ result ;
177
- }
178
-
179
- /**
180
- * Get XML node names using specified xPath
181
- *
182
- * @param SimpleXMLElement $xml
183
- * @param string $xpath
184
- * @return array
185
- */
186
- private function getXmlNodeNames (SimpleXMLElement $ xml , string $ xpath ): array
187
- {
188
- $ result = [];
189
- $ nodes = $ xml ->xpath ($ xpath ) ?: [];
190
- foreach ($ nodes as $ node ) {
191
- $ result [] = new ExtendedNode ($ node ->getName (), $ node );
192
- }
193
- return $ result ;
194
- }
195
-
196
- /**
197
- * Get XML node attribute values using specified xPath
198
- *
199
- * @param SimpleXMLElement $xml
200
- * @param string $xPath
201
- * @param string $attributeName
202
- * @return array
203
- */
204
- private function getXmlAttributeValues (SimpleXMLElement $ xml , string $ xPath , string $ attributeName ): array
205
- {
206
- $ result = [];
207
- $ nodes = $ xml ->xpath ($ xPath ) ?: [];
208
- foreach ($ nodes as $ node ) {
209
- $ nodeArray = (array )$ node ;
210
- if (isset ($ nodeArray ['@attributes ' ][$ attributeName ])) {
211
- $ result [] = new ExtendedNode ($ nodeArray ['@attributes ' ][$ attributeName ], $ node );
188
+ return array_map (function ($ item ) use ($ extractFrom , $ attribute ) {
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 ' ][$ attribute ])) {
197
+ return new ExtendedNode ($ nodeArray ['@attributes ' ][$ attribute ], $ item );
198
+ }
212
199
}
213
- }
214
- return $ result ;
200
+ }, $ nodes );
215
201
}
216
202
}
0 commit comments