Skip to content

Commit 06d6ec6

Browse files
committed
ClassType::from() fixes for ReflectionObject
1 parent 35b3259 commit 06d6ec6

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ClassType extends Nette\Object
6565
*/
6666
public static function from($from)
6767
{
68-
$from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from);
68+
$from = new \ReflectionClass($from instanceof \ReflectionClass ? $from->getName() : $from);
6969
if (PHP_VERSION_ID >= 70000 && $from->isAnonymous()) {
7070
$class = new static('anonymous');
7171
} else {
@@ -81,12 +81,12 @@ public static function from($from)
8181
$class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames());
8282
}
8383
foreach ($from->getProperties() as $prop) {
84-
if ($prop->getDeclaringClass() == $from) { // intentionally ==
84+
if ($prop->getDeclaringClass()->getName() === $from->getName()) {
8585
$class->properties[$prop->getName()] = Property::from($prop);
8686
}
8787
}
8888
foreach ($from->getMethods() as $method) {
89-
if ($method->getDeclaringClass() == $from) { // intentionally ==
89+
if ($method->getDeclaringClass()->getName() === $from->getName()) {
9090
$class->methods[$method->getName()] = Method::from($method)->setNamespace($class->namespace);
9191
}
9292
}

tests/PhpGenerator/ClassType.from.expect

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ class Class2 extends Class1 implements Interface2
5757
{
5858
}
5959

60-
}
60+
}
61+
62+
class Class3
63+
{
64+
public $prop1;
65+
66+
}

tests/PhpGenerator/ClassType.from.phpt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace Abc;
88

99
use Nette\PhpGenerator\ClassType;
10-
use ReflectionClass;
1110
use Tester\Assert;
1211

1312

@@ -62,10 +61,17 @@ class Class2 extends Class1 implements Interface2
6261
{}
6362
}
6463

64+
class Class3
65+
{
66+
public $prop1;
67+
}
6568

6669
$res[] = ClassType::from('Abc\Interface1');
6770
$res[] = ClassType::from('Abc\Interface2');
6871
$res[] = ClassType::from('Abc\Class1');
69-
$res[] = ClassType::from(new ReflectionClass('Abc\Class2'));
72+
$res[] = ClassType::from(new \ReflectionClass('Abc\Class2'));
73+
$obj = new Class3;
74+
$obj->prop2 = 1;
75+
$res[] = ClassType::from(new \ReflectionObject($obj));
7076

7177
Assert::matchFile(__DIR__ . '/ClassType.from.expect', implode("\n", $res));

0 commit comments

Comments
 (0)