Skip to content

Errors when parameters are passed by reference #306

@bradynpoulsen

Description

@bradynpoulsen

DI\Container::make fails silently and returns null when provided argument is not provided by reference, but the dependency expects it to be passed by reference. In example

class A
{
}

class B
{
    public function __construct(A &$aInstance, SomeOtherDependencies $baz)
    {
    }
}

$foo = $container->get('A');
// do some stuff to $foo
try
{
    $bar = $container->make('B', [
        'aInstance' => $foo
    ]);
}
catch(DependencyException $e)
{
    // never gets fired
}
echo is_object($bar) ? get_class($bar) : gettype($bar); // prints 'NULL'

$barReal = $container->make('B', [
    'aInstance' => &$foo
]);
echo is_object($bar) ? get_class($bar) : gettype($bar); // prints 'B'

Whereas, DI\FactoryInterface defines that a DI\DependencyException will be thrown when there is an error while resolving the entry; however, the exception is not thrown

Whereas, \ReflectionParameter::isPassedByReference() is available to check if the parameter should be passed by reference

Therefore,

  1. DI\Container::make should throw a DI\DependencyException if DI\Definition\Resolver\ObjectCreator::createInstance is unable to provide an object of a class after calling \ReflectionClass::newInstanceArgs(array $args)
  2. I believe the DI\Definition\Resolver\ParameterResolver::resolveParameters() should check if \ReflectionParameter::isPassedByReference() is true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions