Skip to content
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
10 changes: 8 additions & 2 deletions src/Container/Capsule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bow\Container;

use ArrayAccess;
use Bow\Container\Exception\ContainerException;
use Closure;
use InvalidArgumentException;
use ReflectionClass;
Expand Down Expand Up @@ -86,14 +87,19 @@ public function makeWith(string $key, array $parameters = []): mixed
*
* @param string $key
* @return mixed
* @throws
* @throws ContainerException when $key is an interface or abstract class without a binding
* @throws \ReflectionException when $key is not a class at all
*/
private function resolve(string $key): mixed
{
$reflection = new ReflectionClass($key);

// An interface or abstract class with no binding cannot be built.
// Returning the key here handed a *string* to every caller expecting
// an instance, which surfaced far away as a TypeError in the
// controller injector. Fail here, naming the missing binding.
if (!$reflection->isInstantiable()) {
return $key;
throw ContainerException::notInstantiable($key);
}

$constructor = $reflection->getConstructor();
Expand Down
33 changes: 33 additions & 0 deletions src/Container/Exception/ContainerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Bow\Container\Exception;

use RuntimeException;

/**
* Raised when the container cannot build a dependency.
*/
class ContainerException extends RuntimeException
{
/**
* The key asked of the container could not be turned into an instance
* because it is an interface or an abstract class with no binding.
*
* @param string $key
* @return ContainerException
*/
public static function notInstantiable(string $key): ContainerException
{
$kind = interface_exists($key) ? 'interface' : 'abstract class';

return new static(sprintf(
'Cannot resolve [%s]: it is an %s and no binding is registered for it. '
. 'Bind a concrete implementation, for example app()->bind(%s::class, Concrete::class).',
$key,
$kind,
$key
));
}
}
48 changes: 48 additions & 0 deletions tests/Container/CapsuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Bow\Tests\Container;

use Bow\Container\Capsule;
use Bow\Container\Exception\ContainerException;
use Bow\Tests\Container\Stubs\AbstractRepository;
use Bow\Tests\Container\Stubs\FileLogger;
use Bow\Tests\Container\Stubs\LoggerInterface;
use Bow\Tests\Container\Stubs\MyClass;
Expand Down Expand Up @@ -217,6 +219,52 @@ public function test_array_access_offset_unset()
$capsule->make('removable');
}

/**
* Regression: make() on an interface with no binding used to return the
* interface *name* (a string), which then blew up far away as
* "Compass::getInjectParameter(): Return value must be of type ?object,
* string returned" when a controller depended on that interface.
*/
public function test_make_unbound_interface_throws_a_clear_exception()
{
$capsule = new Capsule();

$this->expectException(ContainerException::class);
$this->expectExceptionMessage(PaymentGatewayInterface::class);

$capsule->make(PaymentGatewayInterface::class);
}

public function test_make_unbound_abstract_class_throws_a_clear_exception()
{
$capsule = new Capsule();

$this->expectException(ContainerException::class);
$this->expectExceptionMessage('abstract class');

$capsule->make(AbstractRepository::class);
}

public function test_make_with_unbound_interface_throws_a_clear_exception()
{
$capsule = new Capsule();

$this->expectException(ContainerException::class);

$capsule->makeWith(PaymentGatewayInterface::class, []);
}

public function test_dependency_on_unbound_interface_throws_a_clear_exception()
{
// OrderService needs a PaymentGatewayInterface; nothing is bound.
$capsule = new Capsule();

$this->expectException(ContainerException::class);
$this->expectExceptionMessage(PaymentGatewayInterface::class);

$capsule->make(OrderService::class);
}

public function test_make_with_parameters()
{
$capsule = new Capsule();
Expand Down
8 changes: 8 additions & 0 deletions tests/Container/Stubs/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Bow\Tests\Container\Stubs;

abstract class AbstractRepository
{
abstract public function find(int $id): ?array;
}
Loading