Skip to content

Commit

Permalink
Reflection::expandClassName() & getUseStatements() throw exceptions o…
Browse files Browse the repository at this point in the history
…n anonymous class
  • Loading branch information
dg committed Mar 15, 2019
1 parent 8eed052 commit af31ced
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Utils/Reflection.expandClassName.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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.');

0 comments on commit af31ced

Please sign in to comment.