Skip to content

Commit

Permalink
Printer::setTypeResolving() adds ability to print uresolved code [Closes
Browse files Browse the repository at this point in the history
 #31]
  • Loading branch information
dg committed Jan 30, 2019
1 parent 9de4e09 commit a67be7c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
13 changes: 11 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
---------

Expand Down
19 changes: 16 additions & 3 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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())
: '';
}
}
3 changes: 3 additions & 0 deletions tests/PhpGenerator/PhpNamespace.fqn.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
4 changes: 4 additions & 0 deletions tests/PhpGenerator/Printer.namespace.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpGenerator/expected/Printer.namespace.unresolved.expect
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit a67be7c

Please sign in to comment.