Skip to content

Commit 02902f9

Browse files
authored
Merge pull request #464 from magento/MQE-610
MQE-610: [PHPMD] Reduce Cyclomatic Complexity in Problem Methods
2 parents 50ace76 + 01fb9fd commit 02902f9

File tree

17 files changed

+406
-203
lines changed

17 files changed

+406
-203
lines changed

src/Magento/FunctionalTestingFramework/Allure/Adapter/MagentoAllureAdapter.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ public function testError(FailEvent $failEvent)
219219
* Override of parent method, polls stepStorage for testcase and formats it according to actionGroup nesting.
220220
*
221221
* @return void
222-
* @SuppressWarnings(PHPMD)
223222
*/
224223
public function testEnd()
225224
{
@@ -245,11 +244,7 @@ public function testEnd()
245244

246245
$step->setName(str_replace(ActionGroupObject::ACTION_GROUP_CONTEXT_START, '', $step->getName()));
247246
$actionGroupStepContainer = $step;
248-
249-
preg_match(TestGenerator::ACTION_GROUP_STEP_KEY_REGEX, $step->getName(), $matches);
250-
if (!empty($matches['actionGroupStepKey'])) {
251-
$actionGroupStepKey = ucfirst($matches['actionGroupStepKey']);
252-
}
247+
$actionGroupStepKey = $this->retrieveActionGroupStepKey($step);
253248
continue;
254249
}
255250

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

287+
/**
288+
* Reads action group stepKey from step.
289+
*
290+
* @param Step $step
291+
* @return string|null
292+
*/
293+
private function retrieveActionGroupStepKey($step)
294+
{
295+
$actionGroupStepKey = null;
296+
297+
preg_match(TestGenerator::ACTION_GROUP_STEP_KEY_REGEX, $step->getName(), $matches);
298+
299+
if (!empty($matches['actionGroupStepKey'])) {
300+
$actionGroupStepKey = ucfirst($matches['actionGroupStepKey']);
301+
}
302+
303+
return $actionGroupStepKey;
304+
}
305+
292306
/**
293307
* Reading stepKey from file.
294308
*

src/Magento/FunctionalTestingFramework/Codeception/Subscriber/Console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Console extends \Codeception\Subscriber\Console
3838
* @param array $extensionOptions
3939
* @param array $options
4040
*
41-
* @SuppressWarnings(PHPMD)
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4242
*/
4343
public function __construct($extensionOptions = [], $options = [])
4444
{

src/Magento/FunctionalTestingFramework/Config/Converter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function convert($source)
8383
* @param \DOMNodeList|array $elements
8484
* @return array
8585
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
86+
* @TODO ported magento code - to be refactored later
8687
*/
8788
protected function convertXml($elements)
8889
{

src/Magento/FunctionalTestingFramework/Config/Converter/Dom/Flat.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function getNodeAttributes(\DOMNode $node)
7070
* @return string|array
7171
* @throws \UnexpectedValueException
7272
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
73+
* Revisited to reduce cyclomatic complexity, left unrefactored for readability
7374
*/
7475
public function convert(\DOMNode $source, $basePath = '')
7576
{

src/Magento/FunctionalTestingFramework/Config/Dom.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ protected function mergeNode(\DOMElement $node, $parentPath)
145145
* @return void
146146
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
147147
* @SuppressWarnings(PHPMD.NPathComplexity)
148+
* @TODO Ported magento code - to be refactored later
148149
*/
149150
protected function mergeMatchingNode(\DomElement $node, $parentPath, $matchedNode, $path)
150151
{

src/Magento/FunctionalTestingFramework/Console/GenerateSuiteCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\FunctionalTestingFramework\Console;
99

10+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1011
use Magento\FunctionalTestingFramework\Suite\SuiteGenerator;
1112
use Symfony\Component\Console\Input\InputArgument;
1213
use Symfony\Component\Console\Input\InputInterface;

src/Magento/FunctionalTestingFramework/DataGenerator/Objects/EntityDataObject.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1010
use Magento\FunctionalTestingFramework\DataGenerator\Util\GenerationDataReferenceResolver;
1111
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
12+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1213
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
1314

1415
/**
@@ -161,7 +162,6 @@ public function getAllData()
161162
* @param integer $uniquenessFormat
162163
* @return string|null
163164
* @throws TestFrameworkException
164-
* @SuppressWarnings(PHPMD)
165165
*/
166166
public function getDataByName($name, $uniquenessFormat)
167167
{
@@ -177,12 +177,24 @@ public function getDataByName($name, $uniquenessFormat)
177177
throw new TestFrameworkException($exceptionMessage);
178178
}
179179

180-
$name_lower = strtolower($name);
181-
182180
if ($this->data === null) {
183181
return null;
184182
}
183+
return $this->resolveDataReferences($name, $uniquenessFormat);
184+
}
185185

186+
/**
187+
* Resolves data references in entities while generating static test files.
188+
*
189+
* @param string $name
190+
* @param integer $uniquenessFormat
191+
* @return string|null
192+
* @throws TestFrameworkException
193+
* @throws TestReferenceException
194+
*/
195+
private function resolveDataReferences($name, $uniquenessFormat)
196+
{
197+
$name_lower = strtolower($name);
186198
$dataReferenceResolver = new GenerationDataReferenceResolver();
187199
if (array_key_exists($name_lower, $this->data)) {
188200
if (is_array($this->data[$name_lower])) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor;
1414
use Magento\FunctionalTestingFramework\DataGenerator\Util\RuntimeDataReferenceResolver;
1515
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
16+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1617

1718
class OperationDataArrayResolver
1819
{
@@ -66,7 +67,6 @@ public function __construct($dependentEntities = null)
6667
* @param boolean $fromArray
6768
* @return array
6869
* @throws \Exception
69-
* @SuppressWarnings(PHPMD)
7070
*/
7171
public function resolveOperationDataArray($entityObject, $operationMetadata, $operation, $fromArray = false)
7272
{
@@ -120,7 +120,19 @@ public function resolveOperationDataArray($entityObject, $operationMetadata, $op
120120
);
121121
}
122122
}
123+
return $this->resolveRunTimeDataReferences($operationDataArray, $entityObject);
124+
}
123125

126+
/**
127+
* Resolve data references at run time.
128+
* @param array $operationDataArray
129+
* @param EntityDataObject $entityObject
130+
* @return array
131+
* @throws TestFrameworkException
132+
* @throws TestReferenceException
133+
*/
134+
private function resolveRunTimeDataReferences($operationDataArray, $entityObject)
135+
{
124136
$dataReferenceResolver = new RuntimeDataReferenceResolver();
125137
foreach ($operationDataArray as $key => $operationDataValue) {
126138
if (is_array($operationDataValue)) {

src/Magento/FunctionalTestingFramework/Extension/PageReadinessExtension.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ public function beforeTest(TestEvent $e)
106106
* @param StepEvent $e
107107
* @return void
108108
* @throws \Exception
109-
*
110-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
111109
*/
112110
public function beforeStep(StepEvent $e)
113111
{
@@ -117,7 +115,26 @@ public function beforeStep(StepEvent $e)
117115
return;
118116
}
119117

120-
// Check if page has changed and reset metric tracking if so
118+
$this->resetMetricTracker($step);
119+
120+
$metrics = $this->readinessMetrics;
121+
122+
$this->waitForReadiness($metrics);
123+
124+
/** @var AbstractMetricCheck $metric */
125+
foreach ($metrics as $metric) {
126+
$metric->finalizeForStep($step);
127+
}
128+
}
129+
130+
/**
131+
* Check if page has changed, if so reset metric tracking
132+
*
133+
* @param Step $step
134+
* @return void
135+
*/
136+
private function resetMetricTracker($step)
137+
{
121138
if ($this->pageChanged($step)) {
122139
$this->logDebug(
123140
'Page URI changed; resetting readiness metric failure tracking',
@@ -131,16 +148,24 @@ public function beforeStep(StepEvent $e)
131148
$metric->resetTracker();
132149
}
133150
}
151+
}
134152

153+
/**
154+
* Wait for page readiness.
155+
* @param array $metrics
156+
* @return void
157+
* @throws \Codeception\Exception\ModuleRequireException
158+
* @throws \Facebook\WebDriver\Exception\NoSuchElementException
159+
*/
160+
private function waitForReadiness($metrics)
161+
{
135162
// todo: Implement step parameter to override global timeout configuration
136163
if (isset($this->config['timeout'])) {
137164
$timeout = intval($this->config['timeout']);
138165
} else {
139166
$timeout = $this->getDriver()->_getConfig()['pageload_timeout'];
140167
}
141168

142-
$metrics = $this->readinessMetrics;
143-
144169
try {
145170
$this->getDriver()->webDriver->wait($timeout)->until(
146171
function () use ($metrics) {
@@ -160,11 +185,6 @@ function () use ($metrics) {
160185
);
161186
} catch (TimeoutException $exception) {
162187
}
163-
164-
/** @var AbstractMetricCheck $metric */
165-
foreach ($metrics as $metric) {
166-
$metric->finalizeForStep($step);
167-
}
168188
}
169189

170190
/**

src/Magento/FunctionalTestingFramework/ObjectManager/Config/Config.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function getPreference($type)
153153
* @param string $type
154154
* @return array
155155
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
156+
* Revisited to reduce cyclomatic complexity, left unrefactored for readability
156157
*/
157158
protected function collectConfiguration($type)
158159
{
@@ -194,7 +195,6 @@ protected function collectConfiguration($type)
194195
*
195196
* @param array $configuration
196197
* @return void
197-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
198198
*/
199199
protected function mergeConfiguration(array $configuration)
200200
{
@@ -207,28 +207,39 @@ protected function mergeConfiguration(array $configuration)
207207
break;
208208

209209
default:
210-
$key = ltrim($key, '\\');
211-
if (isset($curConfig['type'])) {
212-
$this->virtualTypes[$key] = ltrim($curConfig['type'], '\\');
213-
}
214-
if (isset($curConfig['arguments'])) {
215-
if (!empty($this->mergedArguments)) {
216-
$this->mergedArguments = [];
217-
}
218-
if (isset($this->arguments[$key])) {
219-
$this->arguments[$key] = array_replace($this->arguments[$key], $curConfig['arguments']);
220-
} else {
221-
$this->arguments[$key] = $curConfig['arguments'];
222-
}
223-
}
224-
if (isset($curConfig['shared'])) {
225-
if (!$curConfig['shared']) {
226-
$this->nonShared[$key] = 1;
227-
} else {
228-
unset($this->nonShared[$key]);
229-
}
230-
}
231-
break;
210+
$this->setConfiguration($key, $curConfig);
211+
}
212+
}
213+
}
214+
215+
/**
216+
* Set configuration
217+
*
218+
* @param string $key
219+
* @param array $config
220+
* @return void
221+
*/
222+
private function setConfiguration($key, $config)
223+
{
224+
$key = ltrim($key, '\\');
225+
if (isset($config['type'])) {
226+
$this->virtualTypes[$key] = ltrim($config['type'], '\\');
227+
}
228+
if (isset($config['arguments'])) {
229+
if (!empty($this->mergedArguments)) {
230+
$this->mergedArguments = [];
231+
}
232+
if (isset($this->arguments[$key])) {
233+
$this->arguments[$key] = array_replace($this->arguments[$key], $config['arguments']);
234+
} else {
235+
$this->arguments[$key] = $config['arguments'];
236+
}
237+
}
238+
if (isset($config['shared'])) {
239+
if (!$config['shared']) {
240+
$this->nonShared[$key] = 1;
241+
} else {
242+
unset($this->nonShared[$key]);
232243
}
233244
}
234245
}

0 commit comments

Comments
 (0)