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

Nette\PhpGenerator\Parameter havent option/method to detect is Method parameter variadic #54

Closed 6562680 closed 4 years ago

6562680 commented 4 years ago

Version: 3.3.3

Bug Description

I tried to copy class using reflection then make some changes with method parameters, calling it through different method. So i join params using php native implode() function and failed, because i had some params variadic with ...$param definition in method signature.

I tried to check, but there's no method, will use php reflection method for that at now

dg commented 4 years ago

This is true, because variadic is not a parameter, but a function.

Try to use Printer::printParameters($function) instead of implode(). I changed it to public method in 3.3.4

6562680 commented 4 years ago

Will do

6562680 commented 4 years ago

I'd tested. printParameters() prints method declaration parameters with types.

it is useless if i need print method body like sprintf('%s->method(%s), $class, $printer->printParameters($method));

Is there a way to do it somehow?

So, i can send to you the generator command class, maybe you promote me with some recomendations, how to do things better... yes, i know time is money, so we can talk about, telegram @gzhegow or somewhere

dg commented 4 years ago

I don't understand well what you want to achieve?

6562680 commented 4 years ago
$method->setBody(sprintf('static::getInstance()->%s%s', $method->getName(), $printer->printParameters($method));

We'll get static::getInstance()->myMethod(string $string, ...$params);, but there's unexpected "string" literal because of non-signature case. Not sure about default values:

The static::getInstance()->myMethod(string $string = 'foo', ...$params); still unexpected result

expected one is: The static::getInstance()->myMethod($string, ...$params);

dg commented 4 years ago

I understand.

What parameters you pass on the functions are not much related to their reflection, it is purely up to you. implode() is here a good way.

6562680 commented 4 years ago

Would be great to see "isVariadic()" on $parameter too, to avoid manual Reflection (because you doing it already)