nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Do not add use if has same namespace than current namespace #96

Closed maximecaze closed 2 years ago

maximecaze commented 2 years ago

Do not print uses for namespace uses issued from the same namespace

dg commented 2 years ago

Can you add a test to see what it does?

maximecaze commented 2 years ago

After consideration, I am not sure this change is legitimate, but I am curious of this change https://github.com/nette/php-generator/commit/0e283d971b31b10167bd9807c64a43e0aac29ba9#diff-d20ef32e1a8b64805a08e16ce09ecafc0eb6487be1c47b8bd0766857b596244b. Before that, when we add uses of the same namespace than the generated class, the use does not appear, because there is no need.

Ex with a generated class FooBar:

$extends = Foo\Bar::class;
$file = new \Nette\PhpGenerator\PhpFile();
$namespace = $file->addNamespace('Foo')->addUse($extends);
$class = $namespace->addClass('FooBar');
$class->setExtends($extends);
...
<?php 

namespace Foo;

use Foo/Bar; // no need of that because of Bar class has same namespace than FooBar class

class FooBar extends Bar {}

What do you think ? I am sorry, I am not confortable with these tests yet, I did not succeed to write the correct test...

dg commented 2 years ago

Fixed