Skip to content

Commit

Permalink
Helpers::getReturnType() ignores 'object' and 'mixed'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 14, 2017
1 parent cd4ad17 commit b3fe855
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ public static function getReturnType(\ReflectionFunctionAbstract $func)
if ($type = Reflection::getReturnType($func)) {
return $type;
} elseif ($type = preg_replace('#[|\s].*#', '', (string) self::parseAnnotation($func, 'return'))) {
if ($func instanceof \ReflectionMethod) {
$lower = strtolower($type);
return $lower === 'static' || $lower === '$this'
if ($type === 'object' || $type === 'mixed') {
return NULL;
} elseif ($func instanceof \ReflectionMethod) {
return $type === 'static' || $type === '$this'
? $func->getDeclaringClass()->getName()
: Reflection::expandClassName($type, $func->getDeclaringClass());
} else {
Expand Down
29 changes: 29 additions & 0 deletions tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.php7.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ namespace A
{
return 0.0;
}

/** @return object */
function createObject()
{
return (object) NULL;
}

/** @return mixed */
function createMixed()
{
}
}

}
Expand Down Expand Up @@ -80,4 +91,22 @@ namespace
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Class or interface 'float' not found. Is return type of A\\Factory::createFloat() used in service 'f' correct?");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('factory')
->setClass('A\Factory');
$builder->addDefinition('f')
->setFactory('@factory::createObject');
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Unknown type of service 'f', declare return type of factory method (for PHP 5 use annotation @return)");

Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('factory')
->setClass('A\Factory');
$builder->addDefinition('f')
->setFactory('@factory::createMixed');
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Unknown type of service 'f', declare return type of factory method (for PHP 5 use annotation @return)");

}

0 comments on commit b3fe855

Please sign in to comment.