Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

ReflectionBasedAbstractFactory throws exception when parameter has default value as null #239

Closed
popovserhii opened this issue Jan 26, 2018 · 0 comments

Comments

@popovserhii
Copy link

popovserhii commented Jan 26, 2018

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
));
weierophinney added a commit to weierophinney/zend-servicemanager that referenced this issue Jan 29, 2018
…tainer

Per zendframework#239, currently when an argument is type-hinted but has a default
value (typically `null`), the factory raises an exception about not
being able to retrieve the value from the container. In such cases, we
can use the default value for the argument during injection.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant