Skip to content

Commit a67be7c

Browse files
committed
Printer::setTypeResolving() adds ability to print uresolved code [Closes #31]
1 parent 9de4e09 commit a67be7c

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

readme.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ $namespace->addUse('Http\Request', 'HttpReq'); // use Http\Request as HttpReq;
413413
```
414414

415415
**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,
416-
implemented interfaces and used traits) are automatically *resolved*. It means that you have to **use full class names** in definitions
417-
and they will be replaced with aliases (according to the use-statements) or fully qualified names in the resulting code:
416+
implemented interfaces and used traits) are automatically *resolved* (unless you turn it off, see below).
417+
It means that you have to **use full class names** in definitions and they will be replaced
418+
with aliases (according to the use-statements) or fully qualified names in the resulting code:
418419

419420
```php
420421
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
@@ -455,6 +456,14 @@ class Demo implements A
455456
}
456457
```
457458

459+
Auto-resolving can be turned off this way:
460+
461+
```php
462+
$printer = new Nette\PhpGenerator\Printer; // or PsrPrinter
463+
$printer->setTypeResolving(false);
464+
echo $printer->printNamespace($namespace);
465+
```
466+
458467
PHP Files
459468
---------
460469

src/PhpGenerator/Printer.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Printer
2626
/** @var int */
2727
protected $linesBetweenMethods = 2;
2828

29+
/** @var bool */
30+
private $resolveTypes = true;
31+
2932

3033
public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
3134
{
@@ -83,7 +86,7 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
8386
public function printClass(ClassType $class, PhpNamespace $namespace = null): string
8487
{
8588
$class->validate();
86-
$resolver = $namespace ? [$namespace, 'unresolveName'] : function ($s) { return $s; };
89+
$resolver = $this->resolveTypes && $namespace ? [$namespace, 'unresolveName'] : function ($s) { return $s; };
8790

8891
$traits = [];
8992
foreach ($class->getTraitResolutions() as $trait => $resolutions) {
@@ -185,6 +188,16 @@ public function printFile(PhpFile $file): string
185188
}
186189

187190

191+
/**
192+
* @return static
193+
*/
194+
public function setTypeResolving(bool $state = true): self
195+
{
196+
$this->resolveTypes = $state;
197+
return $this;
198+
}
199+
200+
188201
protected function indent(string $s): string
189202
{
190203
return Strings::indent($s, 1, $this->indentation);
@@ -201,7 +214,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
201214
foreach ($list as $param) {
202215
$variadic = $function->isVariadic() && $param === end($list);
203216
$hint = $param->getTypeHint();
204-
$params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($namespace ? $namespace->unresolveName($hint) : $hint) . ' ' : '')
217+
$params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($hint) : $hint) . ' ' : '')
205218
. ($param->isReference() ? '&' : '')
206219
. ($variadic ? '...' : '')
207220
. '$' . $param->getName()
@@ -220,7 +233,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
220233
protected function printReturnType($function, ?PhpNamespace $namespace): string
221234
{
222235
return $function->getReturnType()
223-
? ': ' . ($function->getReturnNullable() ? '?' : '') . ($namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType())
236+
? ': ' . ($function->getReturnNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType())
224237
: '';
225238
}
226239
}

tests/PhpGenerator/PhpNamespace.fqn.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ $method->addParameter('two')
6464
sameFile(__DIR__ . '/expected/PhpNamespace.fqn2.expect', (string) $class);
6565

6666
sameFile(__DIR__ . '/expected/PhpNamespace.fqn2.expect', (new Printer)->printClass($class, new PhpNamespace('')));
67+
68+
// no resolve
69+
sameFile(__DIR__ . '/expected/PhpNamespace.fqn1.expect', (new Printer)->printClass($class));

tests/PhpGenerator/Printer.namespace.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ sameFile(__DIR__ . '/expected/Printer.namespace.class.expect', $printer->printCl
3838
sameFile(__DIR__ . '/expected/Printer.namespace.class2.expect', $printer->printClass($class));
3939
sameFile(__DIR__ . '/expected/Printer.namespace.method.expect', $printer->printMethod($class->getMethod('first')));
4040

41+
$printer2 = new Printer;
42+
$printer2->setTypeResolving(false);
43+
sameFile(__DIR__ . '/expected/Printer.namespace.unresolved.expect', $printer2->printNamespace($namespace));
44+
4145

4246
$function = new \Nette\PhpGenerator\GlobalFunction('func');
4347
$function
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Foo;
2+
3+
use Bar\C;
4+
use Foo\D as E;
5+
6+
/**
7+
* Description of class.
8+
* This is example
9+
*/
10+
final class A extends ParentClass implements IExample, Foo\IOne
11+
{
12+
use Foo\ObjectTrait;
13+
14+
/**
15+
* @return resource
16+
*/
17+
final public function first(Bar\C $var): stdClass
18+
{
19+
return $this->handle;
20+
}
21+
}

0 commit comments

Comments
 (0)