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

@hamedmoody hamedmoody commented Jul 6, 2018

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

This comment has been minimized.

Copy link
Member

@dg 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 Aug 6, 2018
@AZBosakov

This comment has been minimized.

Copy link

@AZBosakov 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

This comment has been minimized.

Copy link
Member

@milo 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

This comment has been minimized.

Copy link

@AZBosakov 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

This comment has been minimized.

Copy link
Member

@milo 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 added a commit that referenced this issue Jan 30, 2019
dg added a commit that referenced this issue Jan 30, 2019
@dg

This comment has been minimized.

Copy link
Member

@dg 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
Projects
None yet
4 participants
You can’t perform that action at this time.