diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 765df0d873d8..2cfa72f51bb7 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -985,10 +985,14 @@ protected function resolveClass(ReflectionParameter $parameter) // the value of the dependency, similarly to how we do this with scalars. catch (BindingResolutionException $e) { if ($parameter->isDefaultValueAvailable()) { + array_pop($this->with); + return $parameter->getDefaultValue(); } if ($parameter->isVariadic()) { + array_pop($this->with); + return []; } diff --git a/tests/Container/ContainerResolveNonInstantiableTest.php b/tests/Container/ContainerResolveNonInstantiableTest.php new file mode 100644 index 000000000000..1f39322c40b8 --- /dev/null +++ b/tests/Container/ContainerResolveNonInstantiableTest.php @@ -0,0 +1,75 @@ +make(ParentClass::class, ['i' => 42]); + + $this->assertSame(42, $object->i); + } + + public function testResolvingNonInstantiableWithVariadicRemovesWiths() + { + $container = new Container; + $parent = $container->make(VariadicParentClass::class, ['i' => 42]); + + $this->assertCount(0, $parent->child->objects); + $this->assertSame(42, $parent->i); + } +} + +interface TestInterface +{ +} + +class ParentClass +{ + /** + * @var int + */ + public $i; + + public function __construct(TestInterface $testObject = null, int $i = 0) + { + $this->i = $i; + } +} + +class VariadicParentClass +{ + /** + * @var \Illuminate\Tests\Container\ChildClass + */ + public $child; + + /** + * @var int + */ + public $i; + + public function __construct(ChildClass $child, int $i = 0) + { + $this->child = $child; + $this->i = $i; + } +} + +class ChildClass +{ + /** + * @var array + */ + public $objects; + + public function __construct(TestInterface ...$objects) + { + $this->objects = $objects; + } +}