Skip to content

Commit 9ba5d05

Browse files
author
Sergey Nezbritskiy
committed
Reduce cyclomatic complexity for OperationDataArrayResolver::resolveOperationDataArray method
1 parent b6c28b3 commit 9ba5d05

File tree

1 file changed

+99
-75
lines changed

1 file changed

+99
-75
lines changed

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

Lines changed: 99 additions & 75 deletions
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,9 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
105103
$operationElementType = $operationElement->getValue();
106104

107105
if (in_array($operationElementType, self::PRIMITIVE_TYPES)) {
108-
$elementData = $this->resolvePrimitiveReference(
109-
$entityObject,
110-
$operationElement->getKey(),
111-
$operationElement->getType()
112-
);
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-
}
106+
$this->resolvePrimitiveReferenceElement($entityObject, $operationElement, $operationElementType, $operationDataArray);
138107
} 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-
}
108+
$this->resolveNonPrimitiveReferenceElement($entityObject, $operation, $fromArray, $operationElementType, $operationElement, $operationDataArray);
182109
}
183110
}
184111

@@ -394,5 +321,102 @@ private function castValue($type, $value)
394321

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

0 commit comments

Comments
 (0)