zendframework / zend-servicemanager

ServiceManager component from Zend Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReflectionBasedAbstractFactory throws exception when parameter has default value as `null`

popovserhii opened this issue · comments

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
));