Skip to content

Commit 35b3259

Browse files
committed
added support for anonymous classes
1 parent d641cb3 commit 35b3259

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ class ClassType extends Nette\Object
6666
public static function from($from)
6767
{
6868
$from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from);
69-
$class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
69+
if (PHP_VERSION_ID >= 70000 && $from->isAnonymous()) {
70+
$class = new static('anonymous');
71+
} else {
72+
$class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
73+
}
7074
$class->type = $from->isInterface() ? 'interface' : (PHP_VERSION_ID >= 50400 && $from->isTrait() ? 'trait' : 'class');
7175
$class->final = $from->isFinal() && $class->type === 'class';
7276
$class->abstract = $from->isAbstract() && $class->type === 'class';

src/PhpGenerator/Helpers.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ private static function _dump(& $var, $level = 0)
9797
throw new Nette\InvalidArgumentException('Cannot dump closure.');
9898

9999
} elseif (is_object($var)) {
100+
if (PHP_VERSION_ID >= 70000 && ($rc = new \ReflectionObject($var)) && $rc->isAnonymous()) {
101+
throw new Nette\InvalidArgumentException('Cannot dump anonymous class.');
102+
}
100103
$arr = (array) $var;
101104
$space = str_repeat("\t", $level);
102105
$class = get_class($var);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class anonymous
2+
{
3+
public $a;
4+
5+
private $b;
6+
7+
8+
function a()
9+
{
10+
}
11+
12+
13+
private function b()
14+
{
15+
}
16+
17+
}
18+
19+
class anonymous extends Class1
20+
{
21+
22+
function a()
23+
{
24+
}
25+
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator generator.
5+
* @phpversion 7
6+
*/
7+
8+
use Nette\PhpGenerator\ClassType;
9+
use Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
abstract class Class1
16+
{
17+
function func1() {}
18+
}
19+
20+
21+
$res[] = ClassType::from(new class {
22+
public $a;
23+
private $b;
24+
function a() {}
25+
private function b() {}
26+
});
27+
28+
$res[] = ClassType::from(new class extends Class1 {
29+
function a() {}
30+
});
31+
32+
Assert::matchFile(__DIR__ . '/ClassType.from.php7.expect', implode("\n", $res));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Helpers::dump()
5+
* @phpversion 7
6+
*/
7+
8+
use Nette\PhpGenerator\Helpers;
9+
use Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
Assert::exception(function () {
16+
Helpers::dump(new class {});
17+
}, Nette\InvalidArgumentException::class, 'Cannot dump anonymous class.');

0 commit comments

Comments
 (0)