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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Container/Container.php
Expand Up @@ -234,6 +234,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 \TypeError(self::class.'::bind(): Argument #2 ($concrete) must be of type Closure|string|null');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for Taylor: This error message format is what PHP 8.0 does when there's a genuine union type.

}

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

Expand Down
9 changes: 9 additions & 0 deletions tests/Container/ContainerTest.php
Expand Up @@ -120,6 +120,15 @@ public function testSharedConcreteResolution()
$this->assertSame($var1, $var2);
}

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

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

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