Replies: 8 comments
-
|
You can set argument name like this: $node->args[1]->name = '...'See https://getrector.com/ast/defc4808206c4161b76f5bbff542c6a7dc90e4ec |
Beta Was this translation helpful? Give feedback.
-
|
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: $this->getRequestParameter(force: true, parameter: 'locale', default: 'en', request: $request); |
Beta Was this translation helpful? Give feedback.
-
|
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 |
Beta Was this translation helpful? Give feedback.
-
|
In that case, you need to do a reflection on |
Beta Was this translation helpful? Give feedback.
-
|
You can enforce to remove named argument first, by using https://getrector.com/demo/70dddd06-9db6-442b-a58a-4fc7a49a059f after it sorted, use your custom rule to remove specific position. |
Beta Was this translation helpful? Give feedback.
-
|
I don't understand how you would use the And as far as the |
Beta Was this translation helpful? Give feedback.
-
|
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>
*/
private function arguments(array $arguments, array $definitions): array
{
$currentArgumentPosition = 0;
$names = \array_keys($definitions);
$result = [];
foreach ($arguments as $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 arguments
return [...$definitions, ...$result];
}and you can use it like that: $arguments = $this->arguments($args, [
'request' => null,
'parameterName' => null,
'force' => $this->nodeFactory->createFalse(),
'default' => null,
]);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. |
Beta Was this translation helpful? Give feedback.
-
|
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 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Question
I have the following code I want to refactor:
This should be converted to Symfony's
$request->getfunction.The current approach
My current approach is to reorder and reuse the arguments based on their position in the call.=
But this raises the question: What is the best way to cover named arguments. Calls like this:
Is there already code in rector to handle this better? If not would be happy to provide an implementation.
Beta Was this translation helpful? Give feedback.
All reactions