thephpleague / container

Small but powerful dependency injection container

Home Page:http://container.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reflection container attempting to inject interface/abstract name as string rather than default value when abstract does not have a concrete

hetangyuese opened this issue · comments

if ($class !== null) {

if ($class !== null) {
    $reflection_class = new \ReflectionClass($class->name);
    if (!$reflection_class->isInterface() && !$reflection_class->isAbstract()) {
        return $class->getName();
    }
}

I'd need a little more context on what issue exists/what problem this solves...

I'd need a little more context on what issue exists/what problem this solves...

<?php
require __DIR__ . '/vendor/autoload.php';

class Test
{
    public function __construct(?\Psr\SimpleCache\CacheInterface $cache = null)
    {
        if ($cache) {
            $this->cache = $cache;
        }
    }
}

$container = new \League\Container\Container;

$container->delegate(
    new \League\Container\ReflectionContainer
);

$test = $container->get(\Test::class);

image

I'm still not sure what your problem is?

Are you saying that if there is an optional argument and it can't resolve a class, pass null?

I think I see what you're saying here, it is definitely a bug, but the solution isn't as simple as what is suggested above. I will investigate further.

I'm still not sure what your problem is?

Are you saying that if there is an optional argument and it can't resolve a class, pass null?

My English is poor, it's difficult to communicate, sorry

Does what I have changed the issue title to make sense?

To me, yes, a little ;)
Ran into the same type of error when upgrading to 3.3.1 from 3.2.2:
"exception": "TypeError", "message": "Argument 2 passed to Api\\Controllers\\Publisher\\MaterialController::getAllGroups() must implement interface Psr\\Http\\Message\\ServerRequestInterface, string given"
Code:
`
/**

  • @param \Api\Middleware\Paginator $paginator
  • @param \Psr\Http\Message\ServerRequestInterface $request
  • @param \Psr\Http\Message\ResponseInterface $response
  • @return \Psr\Http\Message\ResponseInterface
    */

public function getAllGroups(Paginator $paginator, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
`

Solved in the latest release, with improvements to how it's handled in the 4.x branch