Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Resolving non-instantiables corrupts Container::$with #36212

Merged
merged 3 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand Down
75 changes: 75 additions & 0 deletions tests/Container/ContainerResolveNonInstantiableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Illuminate\Tests\Container;

use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;

class ContainerResolveNonInstantiableTest extends TestCase
{
public function testResolvingNonInstantiableWithDefaultRemovesWiths()
{
$container = new Container;
$object = $container->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;
}
}