Skip to content

Commit bcb74f3

Browse files
committed
Printer: added option $omitEmptyNamespaces
1 parent 225c3ec commit bcb74f3

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ class MyPrinter extends Nette\PhpGenerator\Printer
515515
public bool $bracesOnNextLine = true;
516516
// place one parameter in one line, even if it has an attribute or is promoted
517517
public bool $singleParameterOnOneLine = false;
518+
// omits namespaces that do not contain any class or function
519+
public bool $omitEmptyNamespaces = true;
518520
// separator between the right parenthesis and return type of functions and methods
519521
public string $returnTypeColon = ': ';
520522
}

src/PhpGenerator/Printer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Printer
2626
public string $returnTypeColon = ': ';
2727
public bool $bracesOnNextLine = true;
2828
public bool $singleParameterOnOneLine = false;
29+
public bool $omitEmptyNamespaces = true;
2930
protected ?PhpNamespace $namespace = null;
3031
protected ?Dumper $dumper;
3132
private bool $resolveTypes = true;
@@ -276,6 +277,10 @@ public function printNamespace(PhpNamespace $namespace): string
276277
$items[] = $this->printFunction($function, $namespace);
277278
}
278279

280+
if (!$items && $this->omitEmptyNamespaces) {
281+
return '';
282+
}
283+
279284
$body = ($uses ? $uses . "\n" : '')
280285
. implode("\n", $items);
281286

tests/PhpGenerator/Printer.use-order.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require __DIR__ . '/../bootstrap.php';
99

1010

1111
$printer = new Printer;
12+
$printer->omitEmptyNamespaces = false;
1213
$namespace = new PhpNamespace('Foo');
1314
$namespace->addUse('Example\Foo\EmailAlias\Bar');
1415
$namespace->addUse('Example\Foo\Email\Test');

0 commit comments

Comments
 (0)