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

[7.x] Throw a TypeError if concrete is not a string or closure #33539

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ public function isAlias($name)
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
*
* @throws \InvalidArgumentException
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
*/
public function bind($abstract, $concrete = null, $shared = false)
{
Expand All @@ -234,6 +236,10 @@ public function bind($abstract, $concrete = null, $shared = false)
// bound into this container to the abstract type and we will just wrap it
// up inside its own Closure to give us more convenience when extending.
if (! $concrete instanceof Closure) {
if (! is_string($concrete)) {
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
throw new \InvalidArgumentException('Concrete implementation must be a string or closure');
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
}

$concrete = $this->getClosure($abstract, $concrete);
}

Expand Down Expand Up @@ -337,6 +343,8 @@ public function addContextualBinding($concrete, $abstract, $implementation)
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
*
* @throws \InvalidArgumentException
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
*/
public function bindIf($abstract, $concrete = null, $shared = false)
{
Expand All @@ -351,6 +359,8 @@ public function bindIf($abstract, $concrete = null, $shared = false)
* @param string $abstract
* @param \Closure|string|null $concrete
* @return void
*
* @throws \InvalidArgumentException
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
*/
public function singleton($abstract, $concrete = null)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public function testSharedConcreteResolution()
$this->assertSame($var1, $var2);
}

public function testBindFailsLoudlyWithInvalidArgument()
{
$this->expectException(\InvalidArgumentException::class);
$container = new Container;

$concrete = new ContainerConcreteStub();
$container->bind(ContainerConcreteStub::class, $concrete);
}

public function testAbstractToConcreteResolution()
{
$container = new Container;
Expand Down