Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force root namespace #31

Closed
hamedmoody opened this issue Jul 6, 2018 · 6 comments
Closed

Force root namespace #31

hamedmoody opened this issue Jul 6, 2018 · 6 comments

Comments

@hamedmoody
Copy link

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
Copy link
Member

dg commented Aug 6, 2018

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

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

@dg dg closed this as completed Aug 6, 2018
@AZBosakov
Copy link

AZBosakov commented Dec 6, 2018

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
Copy link
Member

milo commented Dec 7, 2018

$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
Copy link

AZBosakov commented Jan 24, 2019

@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
Copy link
Member

milo commented Jan 30, 2019

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

@dg
Copy link
Member

dg commented Jan 30, 2019

Auto-resolving can be now turned off this way:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants