From 9afa824b8574b03350fe0ed181bd0ac97c021c38 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 25 Apr 2024 15:22:44 +0200 Subject: [PATCH] added PhpFile::removeNamespace() --- src/PhpGenerator/PhpFile.php | 10 ++++++++++ tests/PhpGenerator/PhpFile.phpt | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 87ce3b33..b10a5550 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -110,6 +110,16 @@ public function addNamespace(string|PhpNamespace $namespace): PhpNamespace } + /** + * Removes the namespace from the file. + */ + public function removeNamespace(string|PhpNamespace $namespace): void + { + $name = $namespace instanceof PhpNamespace ? $namespace->getName() : $namespace; + unset($this->namespaces[$name]); + } + + /** @return PhpNamespace[] */ public function getNamespaces(): array { diff --git a/tests/PhpGenerator/PhpFile.phpt b/tests/PhpGenerator/PhpFile.phpt index 9142aa45..04a024d9 100644 --- a/tests/PhpGenerator/PhpFile.phpt +++ b/tests/PhpGenerator/PhpFile.phpt @@ -18,6 +18,10 @@ $file->addComment('This file is auto-generated. DO NOT EDIT!'); $file->addComment('Hey there, I\'m here to document things.'); +$namespace = $file->addNamespace('Deleted'); +$namespace->addClass('Foo'); +$file->removeNamespace('Deleted'); + $namespaceFoo = $file->addNamespace('Foo'); $classA = $namespaceFoo->addClass('A');