Skip to content

Commit d7f312a

Browse files
Resolve cyclomatic complexity, add support for simple parameter declaration
1 parent 60ea1d7 commit d7f312a

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,32 +233,44 @@ protected function resolveArgumentsInRuntime($requestedType, array $parameters,
233233
{
234234
$resolvedArguments = [];
235235
foreach ($parameters as $parameter) {
236-
list($paramName, $paramType, $paramRequired, $paramDefault, $isVariadic) = $parameter;
237-
$argument = null;
238-
if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) {
239-
$argument = $arguments[$paramName];
240-
} elseif ($paramRequired) {
241-
if ($paramType) {
242-
$argument = ['instance' => $paramType];
243-
} else {
244-
$this->creationStack = [];
245-
throw new \BadMethodCallException(
246-
'Missing required argument $' . $paramName . ' of ' . $requestedType . '.'
247-
);
248-
}
249-
} else {
250-
$argument = $paramDefault;
251-
}
236+
$resolvedArguments[] = $this->getResolvedArgument((string)$requestedType, $parameter, $arguments);
237+
}
252238

253-
if ($isVariadic && is_array($argument)) {
254-
$resolvedArguments[] = $argument;
239+
return empty($resolvedArguments) ? [] : array_merge(...$resolvedArguments);
240+
}
241+
242+
/**
243+
* Get resolved argument from parameter
244+
*
245+
* @param string $requestedType
246+
* @param array $parameter
247+
* @param array $arguments
248+
* @return array
249+
*/
250+
private function getResolvedArgument(string $requestedType, array $parameter, array $arguments): array
251+
{
252+
list($paramName, $paramType, $paramRequired, $paramDefault, $isVariadic) = $parameter;
253+
$argument = null;
254+
if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) {
255+
$argument = $arguments[$paramName];
256+
} elseif ($paramRequired) {
257+
if ($paramType) {
258+
$argument = ['instance' => $paramType];
255259
} else {
256-
$this->resolveArgument($argument, $paramType, $paramDefault, $paramName, $requestedType);
257-
$resolvedArguments[] = [$argument];
260+
$this->creationStack = [];
261+
throw new \BadMethodCallException(
262+
'Missing required argument $' . $paramName . ' of ' . $requestedType . '.'
263+
);
258264
}
265+
} else {
266+
$argument = $paramDefault;
267+
}
259268

269+
if ($isVariadic) {
270+
return is_array($argument) ? $argument : [$argument];
260271
}
261272

262-
return empty($resolvedArguments) ? [] : array_merge(...$resolvedArguments);
273+
$this->resolveArgument($argument, $paramType, $paramDefault, $paramName, $requestedType);
274+
return [$argument];
263275
}
264276
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/FactoryTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,14 @@ public function testCreateUsingVariadicDataProvider()
272272
null,
273273
null,
274274
],
275-
'with_args' => [
275+
'with_single_arg' => [
276+
[
277+
'oneScalars' => $oneScalar1
278+
],
279+
$oneScalar1,
280+
null,
281+
],
282+
'with_full_args' => [
276283
[
277284
'oneScalars' => [
278285
$oneScalar1,
@@ -404,6 +411,14 @@ public function testCreateUsingSemiVariadicDataProvider()
404411
null,
405412
null,
406413
],
414+
'only_with_oneScalars_single_value' => [
415+
[
416+
'oneScalars' => $oneScalar1
417+
],
418+
\Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic::DEFAULT_FOO_VALUE,
419+
$oneScalar1,
420+
null,
421+
],
407422
'only_with_oneScalars_full_value' => [
408423
[
409424
'oneScalars' => [

0 commit comments

Comments
 (0)