You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is not the setting. The problem is getting the parameters from the old code. Because depending on the value of the "force" parameter in the function the rector should work different. But with named arguments I can't garantuee that in the old code the "force" parameter exists or is in the third position. Technically the old code could look like this:
And as far as the DowngradeNamedArgumentRector that sounds like a doable solution. However I think having a service that could just take a list of arguments and convert them into a map, would make things easier. I also found other code which requires named arguments.
If you're interested what solution I went with it's something like this:
/** * Turns a list of parameters into a hash map of named parameters. * If the parameter is not specified, it will use the default value from the definitions array. * * @param array<Arg> $arguments * @param array<string, Expr|null> $definitions * * @return array<string, Expr|null> */privatefunctionarguments(array$arguments, array$definitions): array
{
$currentArgumentPosition = 0;
$names = \array_keys($definitions);
$result = [];
foreach ($argumentsas$argument) {
if (null === $argument->name) {
// Get the argument name based on the position$name = $names[$currentArgumentPosition++];
} else {
// Get the argument name from the named argument$name = $this->getName($argument->name);
}
$result[$name] = $argument->value;
\unset($definitions[$name]);
}
// Concatenate the remaining defaulted argumentsreturn [...$definitions, ...$result];
}
The keys are the parameter names the function should have and the values are the default values of this function. I'm sure this could be generalized with Reflection, but as I said I couldn't figure out how this would work with the phpstan reflections.
Activity
TomasVotruba commentedon Apr 2, 2025
You can set argument name like this:
See https://getrector.com/ast/defc4808206c4161b76f5bbff542c6a7dc90e4ec
mamazu commentedon Apr 2, 2025
The problem is not the setting. The problem is getting the parameters from the old code. Because depending on the value of the "force" parameter in the function the rector should work different. But with named arguments I can't garantuee that in the old code the "force" parameter exists or is in the third position. Technically the old code could look like this:
mamazu commentedon Apr 2, 2025
This could then also allow us to make this work: Removing parameters by name instead of positions: https://getrector.com/demo/a0594762-5100-4e64-a761-74adc9eed071
TomasVotruba commentedon Apr 2, 2025
In that case, you need to do a reflection on
getRequestParameter()method. UsingPHPStan\Reflection\ReflectionProviderservicesamsonasik commentedon Apr 2, 2025
You can enforce to remove named argument first, by using
DowngradeNamedArgumentRectorrule first, then it always sorted argument, seehttps://getrector.com/demo/70dddd06-9db6-442b-a58a-4fc7a49a059f
after it sorted, use your custom rule to remove specific position.
mamazu commentedon Apr 2, 2025
I don't understand how you would use the
ReflectionProvider. If I do I get aMethodReflectionwhere most of the important things are private: https://apiref.phpstan.org/1.8.x/PHPStan.Reflection.Php.PhpMethodReflection.htmlAnd as far as the
DowngradeNamedArgumentRectorthat sounds like a doable solution. However I think having a service that could just take a list of arguments and convert them into a map, would make things easier. I also found other code which requires named arguments.mamazu commentedon Apr 3, 2025
If you're interested what solution I went with it's something like this:
and you can use it like that:
The keys are the parameter names the function should have and the values are the default values of this function. I'm sure this could be generalized with Reflection, but as I said I couldn't figure out how this would work with the phpstan reflections.
staabm commentedon Apr 6, 2025
You might be interested in PHPStan ArgumentsNormalizer which primary job is to re-write a named arguments list into a regular one
https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Analyser/ArgumentsNormalizer.php