Skip to content

Commit 24723b5

Browse files
authored
Merge pull request #318 from sergeynezbritskiy/issue-59-reduce-cyclomatic-complexity
Reduce cyclomatic complexity in Problem Methods
2 parents 4fc3430 + 6ddc2b6 commit 24723b5

File tree

2 files changed

+155
-96
lines changed

2 files changed

+155
-96
lines changed

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/OperationDefinitionObjectHandler.php

+42-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
1010
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\OperationDefinitionParser;
1111
use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor;
12-
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1312
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
1413
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1514

@@ -128,14 +127,12 @@ public function getOperationDefinition($operation, $dataType)
128127
* @return void
129128
* @throws \Exception
130129
*
131-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
132130
* @SuppressWarnings(PHPMD.NPathComplexity)
133131
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
134132
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
135133
*/
136134
private function initialize()
137135
{
138-
//TODO: Reduce CyclomaticComplexity/NPathComplexity/Length of method, remove warning suppression.
139136
$objectManager = ObjectManagerFactory::getObjectManager();
140137
$parser = $objectManager->create(OperationDefinitionParser::class);
141138
$parserOutput = $parser->readOperationMetadata()[OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG];
@@ -149,29 +146,11 @@ private function initialize()
149146
$returnRegex = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_RETURN_REGEX] ?? null;
150147
$contentType = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_CONTENT_TYPE][0]['value']
151148
?? null;
152-
$headers = [];
153-
$params = [];
149+
$headers = $this->initializeHeaders($opDefArray);
150+
$params = $this->initializeParams($opDefArray);
154151
$operationElements = [];
155152
$removeBackend = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_BACKEND_REMOVE] ?? false;
156153

157-
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER, $opDefArray)) {
158-
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER] as $headerEntry) {
159-
if (isset($headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE])
160-
&& $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE] !== 'none') {
161-
$headers[] = $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_PARAM]
162-
. ': '
163-
. $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE];
164-
}
165-
}
166-
}
167-
168-
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM, $opDefArray)) {
169-
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM] as $paramEntry) {
170-
$params[$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_KEY]] =
171-
$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_VALUE];
172-
}
173-
}
174-
175154
// extract relevant OperationObjects as OperationElements
176155
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT, $opDefArray)) {
177156
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT] as $opElementArray) {
@@ -241,4 +220,44 @@ private function initialize()
241220
);
242221
}
243222
}
223+
224+
/**
225+
* Convert headers metadata into an array of objects for further use in.
226+
*
227+
* @param array $opDefArray
228+
* @return array
229+
*/
230+
private function initializeHeaders(array $opDefArray): array
231+
{
232+
$headers = [];
233+
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER, $opDefArray)) {
234+
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER] as $headerEntry) {
235+
if (isset($headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE])
236+
&& $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE] !== 'none') {
237+
$headers[] = $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_PARAM]
238+
. ': '
239+
. $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE];
240+
}
241+
}
242+
}
243+
return $headers;
244+
}
245+
246+
/**
247+
* Convert params metadata into an array of objects.
248+
*
249+
* @param array $opDefArray
250+
* @return array
251+
*/
252+
private function initializeParams(array $opDefArray): array
253+
{
254+
$params = [];
255+
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM, $opDefArray)) {
256+
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM] as $paramEntry) {
257+
$params[$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_KEY]] =
258+
$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_VALUE];
259+
}
260+
}
261+
return $params;
262+
}
244263
}

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/OperationDataArrayResolver.php

+113-73
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ public function __construct($dependentEntities = null)
6565
* @param boolean $fromArray
6666
* @return array
6767
* @throws \Exception
68-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6968
* @SuppressWarnings(PHPMD.NPathComplexity)
7069
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
7170
*/
7271
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
7372
{
74-
//TODO: Refactor to reduce Cyclomatic Complexity, remove SupressWarning accordingly.
7573
$operationDataArray = [];
7674
self::incrementSequence($entityObject->getName());
7775

@@ -105,80 +103,21 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
105103
$operationElementType = $operationElement->getValue();
106104

107105
if (in_array($operationElementType, self::PRIMITIVE_TYPES)) {
108-
$elementData = $this->resolvePrimitiveReference(
106+
$this->resolvePrimitiveReferenceElement(
109107
$entityObject,
110-
$operationElement->getKey(),
111-
$operationElement->getType()
108+
$operationElement,
109+
$operationElementType,
110+
$operationDataArray
112111
);
113-
114-
// If data was defined at all, attempt to put it into operation data array
115-
// If data was not defined, and element is required, throw exception
116-
// If no data is defined, don't input defaults per primitive into operation data array
117-
if ($elementData != null) {
118-
if (array_key_exists($operationElement->getKey(), $entityObject->getUniquenessData())) {
119-
$uniqueData = $entityObject->getUniquenessDataByName($operationElement->getKey());
120-
if ($uniqueData === 'suffix') {
121-
$elementData .= (string)self::getSequence($entityObject->getName());
122-
} else {
123-
$elementData = (string)self::getSequence($entityObject->getName()) . $elementData;
124-
}
125-
}
126-
$operationDataArray[$operationElement->getKey()] = $this->castValue(
127-
$operationElementType,
128-
$elementData
129-
);
130-
} elseif ($operationElement->isRequired()) {
131-
throw new \Exception(sprintf(
132-
self::EXCEPTION_REQUIRED_DATA,
133-
$operationElement->getType(),
134-
$operationElement->getKey(),
135-
$entityObject->getName()
136-
));
137-
}
138112
} else {
139-
$operationElementProperty = null;
140-
if (strpos($operationElementType, '.') !== false) {
141-
$operationElementComponents = explode('.', $operationElementType);
142-
$operationElementType = $operationElementComponents[0];
143-
$operationElementProperty = $operationElementComponents[1];
144-
}
145-
146-
$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($operationElementType);
147-
148-
// If an element is required by metadata, but was not provided in the entity, throw an exception
149-
if ($operationElement->isRequired() && $entityNamesOfType == null) {
150-
throw new \Exception(sprintf(
151-
self::EXCEPTION_REQUIRED_DATA,
152-
$operationElement->getType(),
153-
$operationElement->getKey(),
154-
$entityObject->getName()
155-
));
156-
}
157-
foreach ($entityNamesOfType as $entityName) {
158-
if ($operationElementProperty === null) {
159-
$operationDataSubArray = $this->resolveNonPrimitiveElement(
160-
$entityName,
161-
$operationElement,
162-
$operation,
163-
$fromArray
164-
);
165-
} else {
166-
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
167-
$operationDataSubArray = $linkedEntityObj->getDataByName($operationElementProperty, 0);
168-
169-
if ($operationDataSubArray === null) {
170-
throw new \Exception(
171-
sprintf('Property %s not found in entity %s \n', $operationElementProperty, $entityName)
172-
);
173-
}
174-
}
175-
176-
if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) {
177-
$operationDataArray[$operationElement->getKey()][] = $operationDataSubArray;
178-
} else {
179-
$operationDataArray[$operationElement->getKey()] = $operationDataSubArray;
180-
}
181-
}
113+
$this->resolveNonPrimitiveReferenceElement(
114+
$entityObject,
115+
$operation,
116+
$fromArray,
117+
$operationElementType,
118+
$operationElement,
119+
$operationDataArray
120+
);
182121
}
183122
}
184123

@@ -394,5 +333,106 @@ private function castValue($type, $value)
394333

395334
return $newVal;
396335
}
336+
337+
/**
338+
* Resolve a reference for a primitive piece of data
339+
*
340+
* @param EntityDataObject $entityObject
341+
* @param $operationElement
342+
* @param $operationElementType
343+
* @param array $operationDataArray
344+
* @throws TestFrameworkException
345+
*/
346+
private function resolvePrimitiveReferenceElement($entityObject, $operationElement, $operationElementType, array &$operationDataArray)
347+
{
348+
$elementData = $this->resolvePrimitiveReference(
349+
$entityObject,
350+
$operationElement->getKey(),
351+
$operationElement->getType()
352+
);
353+
354+
// If data was defined at all, attempt to put it into operation data array
355+
// If data was not defined, and element is required, throw exception
356+
// If no data is defined, don't input defaults per primitive into operation data array
357+
if ($elementData != null) {
358+
if (array_key_exists($operationElement->getKey(), $entityObject->getUniquenessData())) {
359+
$uniqueData = $entityObject->getUniquenessDataByName($operationElement->getKey());
360+
if ($uniqueData === 'suffix') {
361+
$elementData .= (string)self::getSequence($entityObject->getName());
362+
} else {
363+
$elementData = (string)self::getSequence($entityObject->getName()) . $elementData;
364+
}
365+
}
366+
$operationDataArray[$operationElement->getKey()] = $this->castValue(
367+
$operationElementType,
368+
$elementData
369+
);
370+
} elseif ($operationElement->isRequired()) {
371+
throw new \Exception(sprintf(
372+
self::EXCEPTION_REQUIRED_DATA,
373+
$operationElement->getType(),
374+
$operationElement->getKey(),
375+
$entityObject->getName()
376+
));
377+
}
378+
}
379+
380+
/**
381+
* Resolves DataObjects referenced by the operation
382+
*
383+
* @param $entityObject
384+
* @param $operation
385+
* @param $fromArray
386+
* @param $operationElementType
387+
* @param $operationElement
388+
* @param array $operationDataArray
389+
* @throws TestFrameworkException
390+
*/
391+
private function resolveNonPrimitiveReferenceElement($entityObject, $operation, $fromArray, &$operationElementType, $operationElement, array &$operationDataArray)
392+
{
393+
$operationElementProperty = null;
394+
if (strpos($operationElementType, '.') !== false) {
395+
$operationElementComponents = explode('.', $operationElementType);
396+
$operationElementType = $operationElementComponents[0];
397+
$operationElementProperty = $operationElementComponents[1];
398+
}
399+
400+
$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($operationElementType);
401+
402+
// If an element is required by metadata, but was not provided in the entity, throw an exception
403+
if ($operationElement->isRequired() && $entityNamesOfType == null) {
404+
throw new \Exception(sprintf(
405+
self::EXCEPTION_REQUIRED_DATA,
406+
$operationElement->getType(),
407+
$operationElement->getKey(),
408+
$entityObject->getName()
409+
));
410+
}
411+
foreach ($entityNamesOfType as $entityName) {
412+
if ($operationElementProperty === null) {
413+
$operationDataSubArray = $this->resolveNonPrimitiveElement(
414+
$entityName,
415+
$operationElement,
416+
$operation,
417+
$fromArray
418+
);
419+
} else {
420+
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
421+
$operationDataSubArray = $linkedEntityObj->getDataByName($operationElementProperty, 0);
422+
423+
if ($operationDataSubArray === null) {
424+
throw new \Exception(
425+
sprintf('Property %s not found in entity %s \n', $operationElementProperty, $entityName)
426+
);
427+
}
428+
}
429+
430+
if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) {
431+
$operationDataArray[$operationElement->getKey()][] = $operationDataSubArray;
432+
} else {
433+
$operationDataArray[$operationElement->getKey()] = $operationDataSubArray;
434+
}
435+
}
436+
}
397437
// @codingStandardsIgnoreEnd
398438
}

0 commit comments

Comments
 (0)