From af31ced24ccac864dc985503258ea408aa9a7355 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 15 Mar 2019 06:18:00 +0100 Subject: [PATCH] Reflection::expandClassName() & getUseStatements() throw exceptions on anonymous class --- src/Utils/Reflection.php | 12 +++++++++--- tests/Utils/Reflection.expandClassName.phpt | 11 +++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Utils/Reflection.php b/src/Utils/Reflection.php index 181596846..1cff16fb0 100644 --- a/src/Utils/Reflection.php +++ b/src/Utils/Reflection.php @@ -161,11 +161,14 @@ public static function expandClassName(string $name, \ReflectionClass $rc): stri } elseif (isset(self::BUILTIN_TYPES[$lower])) { return $lower; - } elseif ($lower === 'self') { - return $rc->getName(); - } elseif ($name[0] === '\\') { // fully qualified name return ltrim($name, '\\'); + + } elseif ($rc->isAnonymous()) { + throw new Nette\NotImplementedException('Anonymous classes are not supported.'); + + } elseif ($lower === 'self') { + return $rc->getName(); } $uses = self::getUseStatements($rc); @@ -188,6 +191,9 @@ public static function expandClassName(string $name, \ReflectionClass $rc): stri */ public static function getUseStatements(\ReflectionClass $class): array { + if ($class->isAnonymous()) { + throw new Nette\NotImplementedException('Anonymous classes are not supported.'); + } static $cache = []; if (!isset($cache[$name = $class->getName()])) { if ($class->isInternal()) { diff --git a/tests/Utils/Reflection.expandClassName.phpt b/tests/Utils/Reflection.expandClassName.phpt index 38680f100..9d33ccc99 100644 --- a/tests/Utils/Reflection.expandClassName.phpt +++ b/tests/Utils/Reflection.expandClassName.phpt @@ -27,6 +27,12 @@ Assert::exception(function () use ($rcTest) { }, Nette\InvalidArgumentException::class, 'Class name must not be empty.'); +Assert::exception(function () use ($rcTest) { + Reflection::expandClassName('A', new ReflectionClass(new class { + })); +}, Nette\NotImplementedException::class, 'Anonymous classes are not supported.'); + + Assert::same('A', Reflection::expandClassName('A', $rcTest)); Assert::same('A\B', Reflection::expandClassName('C', $rcTest)); @@ -139,3 +145,8 @@ Assert::same( [], Reflection::getUseStatements(new ReflectionClass('stdClass')) ); + +Assert::exception(function () use ($rcTest) { + Reflection::getUseStatements(new ReflectionClass(new class { + })); +}, Nette\NotImplementedException::class, 'Anonymous classes are not supported.');