Closed hamedmoody closed 6 years ago
Use full class name with namespace, ie $class->setExtends('Yournamespace\Core')
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?
$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;
@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 use
s 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 use
s and leave alone type hints. Make this behavior optional with something like ->allowUnknownClasses(bool $uc)
and trigger_error
when such class encountered.
@AZBosakov I only shown the solution. Answer to you question is, that you are using API of this library in a wrong way.
Auto-resolving can be now turned off this way:
$printer = new Nette\PhpGenerator\Printer; // or PsrPrinter
$printer->setTypeResolving(false);
echo $printer->printNamespace($namespace);
Hi I crate class with namespace and i want to extend from that namespace but not imposible Because it create backslash before extend class: