Skip to content

Commit

Permalink
Reflection::getUseStatements() throw exceptions on anonymous class
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 15, 2019
1 parent d339ba9 commit 5c076fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,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 5c076fd

Please sign in to comment.