From dff25bed72d715fadad36842c01407d693c3e9dd Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Sat, 2 Mar 2024 22:23:57 -0500 Subject: [PATCH] perf: obtaining values obtained from variables that have default values has been supported --- src/DependencyInjection/Container.php | 6 +++++- tests/ContainerTest.php | 26 ++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index 11a6451..fefbe91 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -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()]; diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index c722ca8..09422e2 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -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 = [ @@ -87,7 +88,10 @@ 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')] @@ -95,14 +99,32 @@ public function testGetParameters(): void $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 */