Skip to content

Commit

Permalink
perf: obtaining values obtained from variables that have default valu…
Browse files Browse the repository at this point in the history
…es has been supported
  • Loading branch information
Sleon4 committed Mar 3, 2024
1 parent 4d0a4ed commit dff25be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ private function getParameters(ReflectionFunctionAbstract $method, array $params

foreach ($method->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) {
$args[] = $parameter->getDefaultValue();
if (!empty($params[$parameter->getName()])) {
$args[] = $params[$parameter->getName()];
} else {
$args[] = $parameter->getDefaultValue();
}
} else {
if (!empty($params[$parameter->getName()])) {
$args[] = $params[$parameter->getName()];
Expand Down
26 changes: 24 additions & 2 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ContainerTest extends Test
use ContainerProviderTrait;

const STR = 'test';
const DEFAULT_VALUE = 'default-value';
const FOLDER = './tests/';
const PATH_FILE = './Provider/CustomClass.php';
const FILES = [
Expand Down Expand Up @@ -87,22 +88,43 @@ public function testGetParameters(): void

$this->assertIsArray($parameters);
$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
}

public function testGetParametersWithDefaultValue(): void
{
$parameters = $this->getPrivateMethod(
'getParameters',
[new ReflectionMethod(new CustomClass(), 'setMultiple')]
);

$this->assertIsArray($parameters);

$first = reset($parameters);
$second = end($parameters);

$this->assertInstanceOf(FactoryProvider::class, $first);
$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
$this->assertIsString($second);
$this->assertSame(self::STR, $second);
}

public function testGetParametersWithDefaultDeclaredValue(): void
{
$parameters = $this->getPrivateMethod(
'getParameters',
[
new ReflectionMethod(new CustomClass(), 'setMultiple'),
['str' => self::DEFAULT_VALUE]
]
);

$this->assertIsArray($parameters);

$second = end($parameters);

$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
$this->assertIsString($second);
$this->assertSame(self::DEFAULT_VALUE, $second);
}

public function testInjectDependenciesMethod(): void
{
/** @var FactoryProvider $factoryProvider */
Expand Down

0 comments on commit dff25be

Please sign in to comment.