Deleting namespaces from a PhpFile in the PHP Code Generator

jlevers
Member | 1
+
0
-

Hey All,

I've been working with the PHP Code Generator extensively, and it's been awesome!

The one issue I'm running into is that I can't figure out how to delete a namespace from a PhpFile. Is there any way to do this? I've searched through the code and the API docs but haven't found anything.

Thanks!

Martk
Member | 657
+
0
-

There is no official way, but if you look in the source code, you can do something like this:

function removeNamespace(PhpFile $file, string $namespaceName): PhpFile
{
	$clone = new PhpFile();

	foreach ($file->getNamespaces() as $namespace) {
		if ($namespace->getName() !== $namespaceName) {
			$clone->addNamespace($namespace);
		}
	}

	return $clone;
}
David Grudl
Nette Core | 8179
+
0
-

I added the removeNamespace() method