Skip to content

Commit c00b49b

Browse files
committed
Factory methods from() are using PhpNamespace
1 parent 2f64aa3 commit c00b49b

File tree

4 files changed

+8
-25
lines changed

4 files changed

+8
-25
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,16 @@ 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());
69+
$class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
7070
$class->type = $from->isInterface() ? 'interface' : ($from->isTrait() ? 'trait' : 'class');
7171
$class->final = $from->isFinal() && $class->type === 'class';
7272
$class->abstract = $from->isAbstract() && $class->type === 'class';
7373
$class->implements = $from->getInterfaceNames();
7474
$class->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
75-
$namespace = $from->getNamespaceName();
7675
if ($from->getParentClass()) {
7776
$class->extends = $from->getParentClass()->getName();
78-
if ($namespace) {
79-
$class->extends = Strings::startsWith($class->extends, "$namespace\\") ? substr($class->extends, strlen($namespace) + 1) : '\\' . $class->extends;
80-
}
8177
$class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames());
8278
}
83-
if ($namespace) {
84-
foreach ($class->implements as & $interface) {
85-
$interface = Strings::startsWith($interface, "$namespace\\") ? substr($interface, strlen($namespace) + 1) : '\\' . $interface;
86-
}
87-
}
8879
foreach ($from->getProperties() as $prop) {
8980
if ($prop->getDeclaringClass() == $from) { // intentionally ==
9081
$class->properties[$prop->getName()] = Property::from($prop);

src/PhpGenerator/Method.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public static function from($from)
8383
$method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
8484
$method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
8585
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
86-
$returnType = $from->getReturnType();
87-
$method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType;
86+
$method->returnType = (string) $from->getReturnType();
8887
}
8988
return $method;
9089
}

src/PhpGenerator/Parameter.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,22 @@ public static function from(\ReflectionParameter $from)
3939
$param = new static($from->getName());
4040
$param->reference = $from->isPassedByReference();
4141
if (PHP_VERSION_ID >= 70000) {
42-
$type = $from->getType();
43-
$param->typeHint = $type ? ($type->isBuiltin() ? '' : '\\') . $type : NULL;
42+
$param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
4443
} elseif ($from->isArray() || $from->isCallable()) {
4544
$param->typeHint = $from->isArray() ? 'array' : 'callable';
4645
} else {
4746
try {
48-
$param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL;
47+
$param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL;
4948
} catch (\ReflectionException $e) {
5049
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
51-
$param->typeHint = '\\' . $m[1];
50+
$param->typeHint = $m[1];
5251
} else {
5352
throw $e;
5453
}
5554
}
5655
}
5756
$param->optional = $from->isDefaultValueAvailable();
5857
$param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
59-
60-
$namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL;
61-
$namespace = $namespace ? "\\$namespace\\" : '\\';
62-
if ($param->typeHint !== NULL && Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
63-
$param->typeHint = substr($param->typeHint, strlen($namespace));
64-
}
6558
return $param;
6659
}
6760

tests/PhpGenerator/Method.returnTypes.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace
2222

2323
interface A
2424
{
25-
function testClass() : \A\Foo;
26-
function testScalar() : string;
25+
function testClass(): \A\Foo;
26+
function testScalar(): string;
2727
}
2828

2929
$method = Method::from(A::class .'::testClass');
30-
Assert::same('\A\Foo', $method->getReturnType());
30+
Assert::same('A\Foo', $method->getReturnType());
3131

3232
$method = Method::from(A::class .'::testScalar');
3333
Assert::same('string', $method->getReturnType());

0 commit comments

Comments
 (0)