Skip to content

Commit b10df58

Browse files
committed
AC-663: Create phpcs static check for ClassesTest::testPhpCode
1 parent 48a5816 commit b10df58

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff
1818
private const ERROR_MESSAGE_MODULE = 'Attribute does not follow expected format in module';
1919
private const ERROR_CODE_MODULE = 'WrongXMLModule';
2020

21+
private const FROM_CONTENT = 1;
22+
private const FROM_NAME = 2;
23+
private const FROM_ATTRIBUTE = 3;
24+
2125
/**
2226
* @inheritdoc
2327
*/
@@ -55,7 +59,7 @@ public function process(File $phpcsFile, $stackPtr)
5559
$classes = $this->collectClassesInConfig($xml);
5660
$this->assertNonFactoryName($phpcsFile, $classes);
5761

58-
$modules = $this->getXmlAttributeValues($xml, '//@module', 'module');
62+
$modules = $this->getValuesFromXml($xml, '//@module', self::FROM_ATTRIBUTE, 'module');
5963
$this->assertNonFactoryNameModule($phpcsFile, $modules);
6064
}
6165

@@ -122,21 +126,39 @@ private function getFormattedXML(File $phpcsFile)
122126
*/
123127
private function collectClassesInConfig(SimpleXMLElement $xml): array
124128
{
125-
$classes = $this->getXmlNode(
129+
$classes = $this->getValuesFromXml(
126130
$xml,
127131
'
128132
/config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)]
129133
| //model[not(parent::connection)] | //backend_model | //source_model | //price_model
130134
| //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+
)
132146
);
133-
$classes = array_merge($classes, $this->getXmlAttributeValues($xml, '//@backend_model', 'backend_model'));
134-
$classes = array_merge($classes, $this->getXmlAttributeValues($xml, '/config//preference', 'type'));
135147
$classes = array_merge(
136148
$classes,
137-
$this->getXmlNodeNames(
149+
$this->getValuesFromXml(
138150
$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
140162
)
141163
);
142164

@@ -147,70 +169,34 @@ function (ExtendedNode $extendedNode) {
147169
},
148170
$classes
149171
);
150-
$classes = array_filter(
151-
$classes,
152-
function ($value) {
153-
return !empty($value);
154-
}
155-
);
156172

157173
return $classes;
158174
}
159175

160176
/**
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+
*
165179
* @param SimpleXMLElement $xml
166180
* @param string $xPath
181+
* @param int $extractFrom
182+
* @param string $attribute
167183
* @return array
168184
*/
169-
private function getXmlNode(SimpleXMLElement $xml, string $xPath): array
185+
private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $extractFrom, string $attribute = ''): array
170186
{
171-
$result = [];
172187
$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+
}
212199
}
213-
}
214-
return $result;
200+
}, $nodes);
215201
}
216202
}

0 commit comments

Comments
 (0)