Skip to content

MQE-610: [PHPMD] Reduce Cyclomatic Complexity in Problem Methods #464

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 11 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -219,7 +219,6 @@ public function testError(FailEvent $failEvent)
* Override of parent method, polls stepStorage for testcase and formats it according to actionGroup nesting.
*
* @return void
* @SuppressWarnings(PHPMD)
*/
public function testEnd()
{
Expand All @@ -245,11 +244,7 @@ public function testEnd()

$step->setName(str_replace(ActionGroupObject::ACTION_GROUP_CONTEXT_START, '', $step->getName()));
$actionGroupStepContainer = $step;

preg_match(TestGenerator::ACTION_GROUP_STEP_KEY_REGEX, $step->getName(), $matches);
if (!empty($matches['actionGroupStepKey'])) {
$actionGroupStepKey = ucfirst($matches['actionGroupStepKey']);
}
$actionGroupStepKey = $this->retrieveActionGroupStepKey($step);
continue;
}

Expand Down Expand Up @@ -289,6 +284,25 @@ function () use ($rootStep, $formattedSteps) {
$this->getLifecycle()->fire(new TestCaseFinishedEvent());
}

/**
* Reads action group stepKey from step.
*
* @param Step $step
* @return string|null
*/
private function retrieveActionGroupStepKey($step)
{
$actionGroupStepKey = null;

preg_match(TestGenerator::ACTION_GROUP_STEP_KEY_REGEX, $step->getName(), $matches);

if (!empty($matches['actionGroupStepKey'])) {
$actionGroupStepKey = ucfirst($matches['actionGroupStepKey']);
}

return $actionGroupStepKey;
}

/**
* Reading stepKey from file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Console extends \Codeception\Subscriber\Console
* @param array $extensionOptions
* @param array $options
*
* @SuppressWarnings(PHPMD)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct($extensionOptions = [], $options = [])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function convert($source)
* @param \DOMNodeList|array $elements
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @TODO ported magento code - to be refactored later
*/
protected function convertXml($elements)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function getNodeAttributes(\DOMNode $node)
* @return string|array
* @throws \UnexpectedValueException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* Revisited to reduce cyclomatic complexity, left unrefactored for readability
*/
public function convert(\DOMNode $source, $basePath = '')
{
Expand Down
1 change: 1 addition & 0 deletions src/Magento/FunctionalTestingFramework/Config/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ protected function mergeNode(\DOMElement $node, $parentPath)
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @TODO Ported magento code - to be refactored later
*/
protected function mergeMatchingNode(\DomElement $node, $parentPath, $matchedNode, $path)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Module implements FileResolverInterface
* Module constructor.
* @param ModuleResolver|null $moduleResolver
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @TODO ported magento code - to be refactored later
*/
public function __construct(ModuleResolver $moduleResolver = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\FunctionalTestingFramework\Console;

use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Suite\SuiteGenerator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ public function getAllData()
* @param integer $uniquenessFormat
* @return string|null
* @throws TestFrameworkException
* @SuppressWarnings(PHPMD)
*/
public function getDataByName($name, $uniquenessFormat)
{
Expand All @@ -177,12 +176,24 @@ public function getDataByName($name, $uniquenessFormat)
throw new TestFrameworkException($exceptionMessage);
}

$name_lower = strtolower($name);

if ($this->data === null) {
return null;
}
return $this->resolveDataReferences($name, $uniquenessFormat);
}

/**
* Resolves data references in entities while generating static test files.
*
* @param string $name
* @param integer $uniquenessFormat
* @return string|null
* @throws TestFrameworkException
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
*/
private function resolveDataReferences($name, $uniquenessFormat)
{
$name_lower = strtolower($name);
$dataReferenceResolver = new GenerationDataReferenceResolver();
if (array_key_exists($name_lower, $this->data)) {
if (is_array($this->data[$name_lower])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public function __construct($dependentEntities = null)
* @param boolean $fromArray
* @return array
* @throws \Exception
* @SuppressWarnings(PHPMD)
*/
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
{
Expand Down Expand Up @@ -120,7 +119,19 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
);
}
}
return $this->resolveRunTimeDataReferences($operationDataArray, $entityObject);
}

/**
* Resolve data references at run time.
* @param array $operationDataArray
* @param EntityDataObject $entityObject
* @return mixed
* @throws TestFrameworkException
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
*/
private function resolveRunTimeDataReferences($operationDataArray, $entityObject)
{
$dataReferenceResolver = new RuntimeDataReferenceResolver();
foreach ($operationDataArray as $key => $operationDataValue) {
if (is_array($operationDataValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public function beforeTest(TestEvent $e)
* @param StepEvent $e
* @return void
* @throws \Exception
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function beforeStep(StepEvent $e)
{
Expand All @@ -117,7 +115,26 @@ public function beforeStep(StepEvent $e)
return;
}

// Check if page has changed and reset metric tracking if so
$this->resetMetricTracker($step);

$metrics = $this->readinessMetrics;

$this->waitForReadiness($metrics);

/** @var AbstractMetricCheck $metric */
foreach ($metrics as $metric) {
$metric->finalizeForStep($step);
}
}

/**
* Check if page has changed, if so reset metric tracking
*
* @param Step $step
* @return void
*/
private function resetMetricTracker($step)
{
if ($this->pageChanged($step)) {
$this->logDebug(
'Page URI changed; resetting readiness metric failure tracking',
Expand All @@ -131,16 +148,24 @@ public function beforeStep(StepEvent $e)
$metric->resetTracker();
}
}
}

/**
* Wait for page readiness.
* @param array $metrics
* @return void
* @throws \Codeception\Exception\ModuleRequireException
* @throws \Facebook\WebDriver\Exception\NoSuchElementException
*/
private function waitForReadiness($metrics)
{
// todo: Implement step parameter to override global timeout configuration
if (isset($this->config['timeout'])) {
$timeout = intval($this->config['timeout']);
} else {
$timeout = $this->getDriver()->_getConfig()['pageload_timeout'];
}

$metrics = $this->readinessMetrics;

try {
$this->getDriver()->webDriver->wait($timeout)->until(
function () use ($metrics) {
Expand All @@ -160,11 +185,6 @@ function () use ($metrics) {
);
} catch (TimeoutException $exception) {
}

/** @var AbstractMetricCheck $metric */
foreach ($metrics as $metric) {
$metric->finalizeForStep($step);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function getPreference($type)
* @param string $type
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* Revisited to reduce cyclomatic complexity, left unrefactored for readability
*/
protected function collectConfiguration($type)
{
Expand Down Expand Up @@ -194,7 +195,6 @@ protected function collectConfiguration($type)
*
* @param array $configuration
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function mergeConfiguration(array $configuration)
{
Expand All @@ -207,32 +207,44 @@ protected function mergeConfiguration(array $configuration)
break;

default:
$key = ltrim($key, '\\');
if (isset($curConfig['type'])) {
$this->virtualTypes[$key] = ltrim($curConfig['type'], '\\');
}
if (isset($curConfig['arguments'])) {
if (!empty($this->mergedArguments)) {
$this->mergedArguments = [];
}
if (isset($this->arguments[$key])) {
$this->arguments[$key] = array_replace($this->arguments[$key], $curConfig['arguments']);
} else {
$this->arguments[$key] = $curConfig['arguments'];
}
}
if (isset($curConfig['shared'])) {
if (!$curConfig['shared']) {
$this->nonShared[$key] = 1;
} else {
unset($this->nonShared[$key]);
}
}
$this->setArguments($key, $curConfig);
break;
}
}
}

/**
* Set arguments
*
* @param string $key
* @param array $config
* @return void
*/
private function setArguments($key, $config)
{
$key = ltrim($key, '\\');
if (isset($config['type'])) {
$this->virtualTypes[$key] = ltrim($config['type'], '\\');
}
if (isset($config['arguments'])) {
if (!empty($this->mergedArguments)) {
$this->mergedArguments = [];
}
if (isset($this->arguments[$key])) {
$this->arguments[$key] = array_replace($this->arguments[$key], $config['arguments']);
} else {
$this->arguments[$key] = $config['arguments'];
}
}
if (isset($config['shared'])) {
if (!$config['shared']) {
$this->nonShared[$key] = 1;
} else {
unset($this->nonShared[$key]);
}
}
}

/**
* Extend configuration
*
Expand Down
Loading