Skip to content

Commit af31ced

Browse files
committed
Reflection::expandClassName() & getUseStatements() throw exceptions on anonymous class
1 parent 8eed052 commit af31ced

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Utils/Reflection.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,14 @@ public static function expandClassName(string $name, \ReflectionClass $rc): stri
161161
} elseif (isset(self::BUILTIN_TYPES[$lower])) {
162162
return $lower;
163163

164-
} elseif ($lower === 'self') {
165-
return $rc->getName();
166-
167164
} elseif ($name[0] === '\\') { // fully qualified name
168165
return ltrim($name, '\\');
166+
167+
} elseif ($rc->isAnonymous()) {
168+
throw new Nette\NotImplementedException('Anonymous classes are not supported.');
169+
170+
} elseif ($lower === 'self') {
171+
return $rc->getName();
169172
}
170173

171174
$uses = self::getUseStatements($rc);
@@ -188,6 +191,9 @@ public static function expandClassName(string $name, \ReflectionClass $rc): stri
188191
*/
189192
public static function getUseStatements(\ReflectionClass $class): array
190193
{
194+
if ($class->isAnonymous()) {
195+
throw new Nette\NotImplementedException('Anonymous classes are not supported.');
196+
}
191197
static $cache = [];
192198
if (!isset($cache[$name = $class->getName()])) {
193199
if ($class->isInternal()) {

tests/Utils/Reflection.expandClassName.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Assert::exception(function () use ($rcTest) {
2727
}, Nette\InvalidArgumentException::class, 'Class name must not be empty.');
2828

2929

30+
Assert::exception(function () use ($rcTest) {
31+
Reflection::expandClassName('A', new ReflectionClass(new class {
32+
}));
33+
}, Nette\NotImplementedException::class, 'Anonymous classes are not supported.');
34+
35+
3036
Assert::same('A', Reflection::expandClassName('A', $rcTest));
3137
Assert::same('A\B', Reflection::expandClassName('C', $rcTest));
3238

@@ -139,3 +145,8 @@ Assert::same(
139145
[],
140146
Reflection::getUseStatements(new ReflectionClass('stdClass'))
141147
);
148+
149+
Assert::exception(function () use ($rcTest) {
150+
Reflection::getUseStatements(new ReflectionClass(new class {
151+
}));
152+
}, Nette\NotImplementedException::class, 'Anonymous classes are not supported.');

0 commit comments

Comments
 (0)