Skip to content

Reduce cyclomatic complexity in Problem Methods #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\OperationDefinitionParser;
use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;

Expand Down Expand Up @@ -128,14 +127,12 @@ public function getOperationDefinition($operation, $dataType)
* @return void
* @throws \Exception
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function initialize()
{
//TODO: Reduce CyclomaticComplexity/NPathComplexity/Length of method, remove warning suppression.
$objectManager = ObjectManagerFactory::getObjectManager();
$parser = $objectManager->create(OperationDefinitionParser::class);
$parserOutput = $parser->readOperationMetadata()[OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG];
Expand All @@ -149,29 +146,11 @@ private function initialize()
$returnRegex = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_RETURN_REGEX] ?? null;
$contentType = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_CONTENT_TYPE][0]['value']
?? null;
$headers = [];
$params = [];
$headers = $this->initializeHeaders($opDefArray);
$params = $this->initializeParams($opDefArray);
$operationElements = [];
$removeBackend = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_BACKEND_REMOVE] ?? false;

if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER, $opDefArray)) {
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER] as $headerEntry) {
if (isset($headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE])
&& $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE] !== 'none') {
$headers[] = $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_PARAM]
. ': '
. $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE];
}
}
}

if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM, $opDefArray)) {
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM] as $paramEntry) {
$params[$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_KEY]] =
$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_VALUE];
}
}

// extract relevant OperationObjects as OperationElements
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT, $opDefArray)) {
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT] as $opElementArray) {
Expand Down Expand Up @@ -241,4 +220,40 @@ private function initialize()
);
}
}

/**
* @param array $opDefArray
* @return array
*/
private function initializeHeaders(array $opDefArray): array
{
$headers = [];
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER, $opDefArray)) {
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER] as $headerEntry) {
if (isset($headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE])
&& $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE] !== 'none') {
$headers[] = $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_PARAM]
. ': '
. $headerEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_HEADER_VALUE];
}
}
}
return $headers;
}

/**
* @param array $opDefArray
* @return array
*/
private function initializeParams(array $opDefArray): array
{
$params = [];
if (array_key_exists(OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM, $opDefArray)) {
foreach ($opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM] as $paramEntry) {
$params[$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_KEY]] =
$paramEntry[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL_PARAM_VALUE];
}
}
return $params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ public function __construct($dependentEntities = null)
* @param boolean $fromArray
* @return array
* @throws \Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
{
//TODO: Refactor to reduce Cyclomatic Complexity, remove SupressWarning accordingly.
$operationDataArray = [];
self::incrementSequence($entityObject->getName());

Expand Down Expand Up @@ -105,80 +103,9 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
$operationElementType = $operationElement->getValue();

if (in_array($operationElementType, self::PRIMITIVE_TYPES)) {
$elementData = $this->resolvePrimitiveReference(
$entityObject,
$operationElement->getKey(),
$operationElement->getType()
);

// If data was defined at all, attempt to put it into operation data array
// If data was not defined, and element is required, throw exception
// If no data is defined, don't input defaults per primitive into operation data array
if ($elementData != null) {
if (array_key_exists($operationElement->getKey(), $entityObject->getUniquenessData())) {
$uniqueData = $entityObject->getUniquenessDataByName($operationElement->getKey());
if ($uniqueData === 'suffix') {
$elementData .= (string)self::getSequence($entityObject->getName());
} else {
$elementData = (string)self::getSequence($entityObject->getName()) . $elementData;
}
}
$operationDataArray[$operationElement->getKey()] = $this->castValue(
$operationElementType,
$elementData
);
} elseif ($operationElement->isRequired()) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
}
$this->resolvePrimitiveReferenceElement($entityObject, $operationElement, $operationElementType, $operationDataArray);
} else {
$operationElementProperty = null;
if (strpos($operationElementType, '.') !== false) {
$operationElementComponents = explode('.', $operationElementType);
$operationElementType = $operationElementComponents[0];
$operationElementProperty = $operationElementComponents[1];
}

$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($operationElementType);

// If an element is required by metadata, but was not provided in the entity, throw an exception
if ($operationElement->isRequired() && $entityNamesOfType == null) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
}
foreach ($entityNamesOfType as $entityName) {
if ($operationElementProperty === null) {
$operationDataSubArray = $this->resolveNonPrimitiveElement(
$entityName,
$operationElement,
$operation,
$fromArray
);
} else {
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
$operationDataSubArray = $linkedEntityObj->getDataByName($operationElementProperty, 0);

if ($operationDataSubArray === null) {
throw new \Exception(
sprintf('Property %s not found in entity %s \n', $operationElementProperty, $entityName)
);
}
}

if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) {
$operationDataArray[$operationElement->getKey()][] = $operationDataSubArray;
} else {
$operationDataArray[$operationElement->getKey()] = $operationDataSubArray;
}
}
$this->resolveNonPrimitiveReferenceElement($entityObject, $operation, $fromArray, $operationElementType, $operationElement, $operationDataArray);
}
}

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

return $newVal;
}

/**
* @param EntityDataObject $entityObject
* @param $operationElement
* @param $operationElementType
* @param array $operationDataArray
* @throws TestFrameworkException
*/
private function resolvePrimitiveReferenceElement($entityObject, $operationElement, $operationElementType, array &$operationDataArray)
{
$elementData = $this->resolvePrimitiveReference(
$entityObject,
$operationElement->getKey(),
$operationElement->getType()
);

// If data was defined at all, attempt to put it into operation data array
// If data was not defined, and element is required, throw exception
// If no data is defined, don't input defaults per primitive into operation data array
if ($elementData != null) {
if (array_key_exists($operationElement->getKey(), $entityObject->getUniquenessData())) {
$uniqueData = $entityObject->getUniquenessDataByName($operationElement->getKey());
if ($uniqueData === 'suffix') {
$elementData .= (string)self::getSequence($entityObject->getName());
} else {
$elementData = (string)self::getSequence($entityObject->getName()) . $elementData;
}
}
$operationDataArray[$operationElement->getKey()] = $this->castValue(
$operationElementType,
$elementData
);
} elseif ($operationElement->isRequired()) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
}
}

/**
* @param $entityObject
* @param $operation
* @param $fromArray
* @param $operationElementType
* @param $operationElement
* @param array $operationDataArray
* @throws TestFrameworkException
*/
private function resolveNonPrimitiveReferenceElement($entityObject, $operation, $fromArray, &$operationElementType, $operationElement, array &$operationDataArray)
{
$operationElementProperty = null;
if (strpos($operationElementType, '.') !== false) {
$operationElementComponents = explode('.', $operationElementType);
$operationElementType = $operationElementComponents[0];
$operationElementProperty = $operationElementComponents[1];
}

$entityNamesOfType = $entityObject->getLinkedEntitiesOfType($operationElementType);

// If an element is required by metadata, but was not provided in the entity, throw an exception
if ($operationElement->isRequired() && $entityNamesOfType == null) {
throw new \Exception(sprintf(
self::EXCEPTION_REQUIRED_DATA,
$operationElement->getType(),
$operationElement->getKey(),
$entityObject->getName()
));
}
foreach ($entityNamesOfType as $entityName) {
if ($operationElementProperty === null) {
$operationDataSubArray = $this->resolveNonPrimitiveElement(
$entityName,
$operationElement,
$operation,
$fromArray
);
} else {
$linkedEntityObj = $this->resolveLinkedEntityObject($entityName);
$operationDataSubArray = $linkedEntityObj->getDataByName($operationElementProperty, 0);

if ($operationDataSubArray === null) {
throw new \Exception(
sprintf('Property %s not found in entity %s \n', $operationElementProperty, $entityName)
);
}
}

if ($operationElement->getType() == OperationDefinitionObjectHandler::ENTITY_OPERATION_ARRAY) {
$operationDataArray[$operationElement->getKey()][] = $operationDataSubArray;
} else {
$operationDataArray[$operationElement->getKey()] = $operationDataSubArray;
}
}
}
// @codingStandardsIgnoreEnd
}