Skip to content

Commit 936c28c

Browse files
committed
AC-663: Create phpcs static check for ClassesTest::testPhpCode
1 parent 9a69638 commit 936c28c

File tree

2 files changed

+60
-69
lines changed

2 files changed

+60
-69
lines changed

Magento2/Sniffs/Legacy/ClassReferencesInConfigurationFilesSniff.php

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ class ClassReferencesInConfigurationFilesSniff implements Sniff
1616
private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference';
1717
private const ERROR_CODE_CONFIG = 'IncorrectClassReference';
1818
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';
2420

2521
/**
2622
* @inheritdoc
@@ -59,26 +55,26 @@ public function process(File $phpcsFile, $stackPtr)
5955
$classes = $this->collectClassesInConfig($xml);
6056
$this->assertNonFactoryName($phpcsFile, $classes);
6157

62-
$modules = $this->getValuesFromXml($xml, '//@module', self::FROM_ATTRIBUTE, 'module');
58+
$modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module');
6359
$this->assertNonFactoryNameModule($phpcsFile, $modules);
6460
}
6561

6662
/**
6763
* Check whether specified class names are right according PSR-1 Standard.
6864
*
6965
* @param File $phpcsFile
70-
* @param ExtendedNode[] $elements
66+
* @param array $elements
7167
*/
7268
private function assertNonFactoryName(File $phpcsFile, array $elements)
7369
{
7470
foreach ($elements as $element) {
75-
if (stripos($element->value, 'Magento') === false) {
71+
if (stripos($element['value'], 'Magento') === false) {
7672
continue;
7773
}
78-
if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element->value) !== 1) {
74+
if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) {
7975
$phpcsFile->addError(
8076
self::ERROR_MESSAGE_CONFIG,
81-
$element->lineNumber - 1,
77+
$element['lineNumber'],
8278
self::ERROR_CODE_CONFIG,
8379
);
8480
}
@@ -89,15 +85,15 @@ private function assertNonFactoryName(File $phpcsFile, array $elements)
8985
* Check whether specified class names in modules are right according PSR-1 Standard.
9086
*
9187
* @param File $phpcsFile
92-
* @param ExtendedNode[] $classes
88+
* @param array $classes
9389
*/
9490
private function assertNonFactoryNameModule(File $phpcsFile, array $classes)
9591
{
9692
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) {
9894
$phpcsFile->addError(
9995
self::ERROR_MESSAGE_MODULE,
100-
$element->lineNumber - 1,
96+
$element['lineNumber'],
10197
self::ERROR_CODE_MODULE,
10298
);
10399
}
@@ -126,45 +122,41 @@ private function getFormattedXML(File $phpcsFile)
126122
*/
127123
private function collectClassesInConfig(SimpleXMLElement $xml): array
128124
{
129-
$classes = $this->getValuesFromXml(
125+
$classes = $this->getValuesFromXmlTagContent(
130126
$xml,
131127
'
132128
/config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)]
133129
| //model[not(parent::connection)] | //backend_model | //source_model | //price_model
134130
| //model_token | //writer_model | //clone_model | //frontend_model | //working_model
135131
| //admin_renderer | //renderer',
136-
self::FROM_CONTENT
137132
);
138133
$classes = array_merge(
139134
$classes,
140-
$this->getValuesFromXml(
135+
$this->getValuesFromXmlTagAttribute(
141136
$xml,
142137
'//@backend_model',
143-
self::FROM_ATTRIBUTE,
144138
'backend_model'
145139
)
146140
);
147141
$classes = array_merge(
148142
$classes,
149-
$this->getValuesFromXml(
143+
$this->getValuesFromXmlTagAttribute(
150144
$xml,
151145
'/config//preference',
152-
self::FROM_ATTRIBUTE,
153146
'type'
154147
)
155148
);
156149
$classes = array_merge(
157150
$classes,
158-
$this->getValuesFromXml(
151+
$this->getValuesFromXmlTagName(
159152
$xml,
160153
'/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*',
161-
self::FROM_NAME
162154
)
163155
);
164156

165157
$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];
168160
return $extendedNode;
169161
},
170162
$classes
@@ -174,28 +166,59 @@ function (ExtendedNode $extendedNode) {
174166
}
175167

176168
/**
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
178206
*
179207
* @param SimpleXMLElement $xml
180208
* @param string $xPath
181-
* @param int $extractFrom
182209
* @param string $attr
183210
* @return array
184211
*/
185-
private function getValuesFromXml(SimpleXMLElement $xml, string $xPath, int $extractFrom, string $attr = ''): array
212+
private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array
186213
{
187214
$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+
];
199222
}
200223
}, $nodes);
201224
}

Magento2/Sniffs/Legacy/ExtendedNode.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)