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

Force root namespace #31

Closed hamedmoody closed 6 years ago

hamedmoody commented 6 years ago

Hi I crate class with namespace and i want to extend from that namespace but not imposible Because it create backslash before extend class: image

dg commented 6 years ago

Use full class name with namespace, ie $class->setExtends('Yournamespace\Core')

https://github.com/nette/php-generator#namespace

AZBosakov commented 5 years ago

What about this?

use Psr\Http\Message\ServerRequestInterface;
...
public function assemble(\ServerRequestInterface $request, array $params): \?ServerRequestInterface
    {
    }

WTF? If I wanted those backslashes, I'd type them. Why forcing you to use full class names?

milo commented 5 years ago
$ns = new Nette\PhpGenerator\PhpNamespace('App\Ns');
$class = $ns->addClass('Test');
$method = $class->addMethod('assemble');
$method->addParameter('request')->setTypeHint('Psr\Http\Message\ServerRequestInterface');
$method->setReturnType('Psr\Http\Message\ServerRequestInterface')->setReturnNullable();

$ns->addUse('Psr\Http\Message\ServerRequestInterface');

echo $ns;
AZBosakov commented 5 years ago

@milo That wasn't an answer to my question - why can't I just do ->setTypeHint('ServerRequestInterface') What if I refer to another class in the same namespace? Why overcomplicate things? Yes, it helps correctness I guess, but why not just warn about it instead of forcing it and ending with file containing something different than what I'd write by hand. It's hugely unintuitive and a usability hurdle. Case in point - I made a little CLI tool (php-class) for fast class skeleton creation. Used like: for C in X Y Z; do php-class $C -n V/P/NS -u 'A/B as BB' -m 'public function someFunc(BB $bb): ?BB' -o ./NS/; done Or, rather, I'd like to use it that way - it contains a function parsing the method declaration, written with the idea of parsing what I'd write by hand in the .php file. See the problem? At present, it uses ->setTypeHint('BB') and the result is public function someFunc(\BB $bb): ?\BB. I can modify it to track uses and the current namespace, alright. But it looks like the php generator already tracks this, but only one way: 'use X\Y as Z; ...(X\Y $z...' -> '...(Z $z...' and 'namespace NS; ...(NS\C $c...' -> '...(C $c...'. So, a suggestion / feature request - check uses and leave alone type hints. Make this behavior optional with something like ->allowUnknownClasses(bool $uc) and trigger_error when such class encountered.

milo commented 5 years ago

@AZBosakov I only shown the solution. Answer to you question is, that you are using API of this library in a wrong way.

dg commented 5 years ago

Auto-resolving can be now turned off this way:

$printer = new Nette\PhpGenerator\Printer; // or PsrPrinter
$printer->setTypeResolving(false);
echo $printer->printNamespace($namespace);