Skip to content

Commit 3162d8d

Browse files
committed
Printer: option $omitEmptyNamespaces is applied by printFile() [Close #147]
1 parent 561dc1a commit 3162d8d

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

src/PhpGenerator/Printer.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ public function printNamespace(PhpNamespace $namespace): string
277277
$items[] = $this->printFunction($function, $namespace);
278278
}
279279

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

@@ -300,7 +296,9 @@ public function printFile(PhpFile $file): string
300296
{
301297
$namespaces = [];
302298
foreach ($file->getNamespaces() as $namespace) {
303-
$namespaces[] = $this->printNamespace($namespace);
299+
if (!$this->omitEmptyNamespaces || $namespace->getClasses() || $namespace->getFunctions()) {
300+
$namespaces[] = $this->printNamespace($namespace);
301+
}
304302
}
305303

306304
return "<?php\n"

tests/PhpGenerator/Printer.use-order.phpt

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

1010

1111
$printer = new Printer;
12-
$printer->omitEmptyNamespaces = false;
1312
$namespace = new PhpNamespace('Foo');
1413
$namespace->addUse('Example\Foo\EmailAlias\Bar');
1514
$namespace->addUse('Example\Foo\Email\Test');

0 commit comments

Comments
 (0)