ReflectionBasedAbstractFactory throws exception when parameter has default value as `null`
popovserhii opened this issue · comments
Serhii Popov commented
- [+] I was not able to find an open or closed issue matching what I'm seeing.
- [+] This is not a question. (Questions should be asked on slack (Signup for Slack here) or our forums.)
I trying to use ReflectionBasedAbstractFactory
on Zend Expressive Skeleton 3.0 with default configuration. If a parameter has a default value as null
it throws an exception.
Code to reproduce the issue
Install zend-expressive-skeleton
composer create-project "zendframework/zend-expressive-skeleton:3.0.x-dev" <project-path>
Add to config/autoload/dependencies.global.php
//...
'abstract_factories' => [
ReflectionBasedAbstractFactory::class, // universal auto-wiring factory
],
//...
And run application in browser
Expected results
Application should run without any errors
Actual results
Zend\Expressive\Middleware\ImplicitHeadMiddleware"; unable to resolve parameter "response" using type hint "Psr\Http\Message\ResponseInterface
As solution can be change ReflectionBasedAbstractFactory on ~221 line:
if (! $container->has($type)) {
throw new ServiceNotFoundException(sprintf(
'Unable to create service "%s"; unable to resolve parameter "%s" using type hint "%s"',
$requestedName,
$parameter->getName(),
$type
));
}
return $container->get($type);
to
if ($container->has($type)) {
return $container->get($type);
} elseif ($parameter->isOptional()) {
return $parameter->getDefaultValue();
}
throw new ServiceNotFoundException(sprintf(
'Unable to create service "%s"; unable to resolve parameter "%s" using type hint "%s"',
$requestedName,
$parameter->getName(),
$type
));