diff --git a/readme.md b/readme.md index 3c4b8b0f..ddeaba70 100644 --- a/readme.md +++ b/readme.md @@ -413,8 +413,9 @@ $namespace->addUse('Http\Request', 'HttpReq'); // use Http\Request as HttpReq; ``` **IMPORTANT NOTE:** when the class is part of the namespace, it is rendered slightly differently: all types (ie. type hints, return types, parent class name, -implemented interfaces and used traits) are automatically *resolved*. It means that you have to **use full class names** in definitions -and they will be replaced with aliases (according to the use-statements) or fully qualified names in the resulting code: +implemented interfaces and used traits) are automatically *resolved* (unless you turn it off, see below). +It means that you have to **use full class names** in definitions and they will be replaced +with aliases (according to the use-statements) or fully qualified names in the resulting code: ```php $namespace = new Nette\PhpGenerator\PhpNamespace('Foo'); @@ -455,6 +456,14 @@ class Demo implements A } ``` +Auto-resolving can be turned off this way: + +```php +$printer = new Nette\PhpGenerator\Printer; // or PsrPrinter +$printer->setTypeResolving(false); +echo $printer->printNamespace($namespace); +``` + PHP Files --------- diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 580a3f10..657ac250 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -26,6 +26,9 @@ class Printer /** @var int */ protected $linesBetweenMethods = 2; + /** @var bool */ + private $resolveTypes = true; + public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string { @@ -83,7 +86,7 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str public function printClass(ClassType $class, PhpNamespace $namespace = null): string { $class->validate(); - $resolver = $namespace ? [$namespace, 'unresolveName'] : function ($s) { return $s; }; + $resolver = $this->resolveTypes && $namespace ? [$namespace, 'unresolveName'] : function ($s) { return $s; }; $traits = []; foreach ($class->getTraitResolutions() as $trait => $resolutions) { @@ -185,6 +188,16 @@ public function printFile(PhpFile $file): string } + /** + * @return static + */ + public function setTypeResolving(bool $state = true): self + { + $this->resolveTypes = $state; + return $this; + } + + protected function indent(string $s): string { return Strings::indent($s, 1, $this->indentation); @@ -201,7 +214,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string foreach ($list as $param) { $variadic = $function->isVariadic() && $param === end($list); $hint = $param->getTypeHint(); - $params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($namespace ? $namespace->unresolveName($hint) : $hint) . ' ' : '') + $params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($hint) : $hint) . ' ' : '') . ($param->isReference() ? '&' : '') . ($variadic ? '...' : '') . '$' . $param->getName() @@ -220,7 +233,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string protected function printReturnType($function, ?PhpNamespace $namespace): string { return $function->getReturnType() - ? ': ' . ($function->getReturnNullable() ? '?' : '') . ($namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType()) + ? ': ' . ($function->getReturnNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType()) : ''; } } diff --git a/tests/PhpGenerator/PhpNamespace.fqn.phpt b/tests/PhpGenerator/PhpNamespace.fqn.phpt index 890b40fb..de460410 100644 --- a/tests/PhpGenerator/PhpNamespace.fqn.phpt +++ b/tests/PhpGenerator/PhpNamespace.fqn.phpt @@ -64,3 +64,6 @@ $method->addParameter('two') sameFile(__DIR__ . '/expected/PhpNamespace.fqn2.expect', (string) $class); sameFile(__DIR__ . '/expected/PhpNamespace.fqn2.expect', (new Printer)->printClass($class, new PhpNamespace(''))); + +// no resolve +sameFile(__DIR__ . '/expected/PhpNamespace.fqn1.expect', (new Printer)->printClass($class)); diff --git a/tests/PhpGenerator/Printer.namespace.phpt b/tests/PhpGenerator/Printer.namespace.phpt index 062efef2..271bbee9 100644 --- a/tests/PhpGenerator/Printer.namespace.phpt +++ b/tests/PhpGenerator/Printer.namespace.phpt @@ -38,6 +38,10 @@ sameFile(__DIR__ . '/expected/Printer.namespace.class.expect', $printer->printCl sameFile(__DIR__ . '/expected/Printer.namespace.class2.expect', $printer->printClass($class)); sameFile(__DIR__ . '/expected/Printer.namespace.method.expect', $printer->printMethod($class->getMethod('first'))); +$printer2 = new Printer; +$printer2->setTypeResolving(false); +sameFile(__DIR__ . '/expected/Printer.namespace.unresolved.expect', $printer2->printNamespace($namespace)); + $function = new \Nette\PhpGenerator\GlobalFunction('func'); $function diff --git a/tests/PhpGenerator/expected/Printer.namespace.unresolved.expect b/tests/PhpGenerator/expected/Printer.namespace.unresolved.expect new file mode 100644 index 00000000..f671ea04 --- /dev/null +++ b/tests/PhpGenerator/expected/Printer.namespace.unresolved.expect @@ -0,0 +1,21 @@ +namespace Foo; + +use Bar\C; +use Foo\D as E; + +/** + * Description of class. + * This is example + */ +final class A extends ParentClass implements IExample, Foo\IOne +{ + use Foo\ObjectTrait; + + /** + * @return resource + */ + final public function first(Bar\C $var): stdClass + { + return $this->handle; + } +}