From b3fe8551162279216e251e49b406e55cd2d255d5 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 14 Mar 2017 18:05:55 +0100 Subject: [PATCH] Helpers::getReturnType() ignores 'object' and 'mixed' --- src/DI/Helpers.php | 7 +++-- ...lder.factory.resolveBuiltinTypes.php7.phpt | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index 989353cce..91783f252 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -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 { diff --git a/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.php7.phpt b/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.php7.phpt index cb29fbaea..2dcc59f2d 100644 --- a/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.php7.phpt +++ b/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.php7.phpt @@ -31,6 +31,17 @@ namespace A { return 0.0; } + + /** @return object */ + function createObject() + { + return (object) NULL; + } + + /** @return mixed */ + function createMixed() + { + } } } @@ -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)"); + }