Skip to content

Named arguments in refactorings #9087

@mamazu

Description

@mamazu

Question

I have the following code I want to refactor:

function getRequestParameter(Request $request, string $parameter, bool $force = false, $default = null);

// Example:
$this->getRequestParameter($request, 'locale', true);
$this->getRequestParameter($request, 'page', false, '1');

This should be converted to Symfony's $request->get function.

The current approach

My current approach is to reorder and reuse the arguments based on their position in the call.=

        /** @var MethodCall $node */
        if ($this->nodeNameResolver->isName($node->var, '$this') || (string) $node->name !== 'getRequestParameter' ){ return; }

        $request = $node->args[0]->value;
        $parameterName = $node->args[1]->value;
        $force = $node->args[2]?->value ?? $this->nodeFactory->createFalse();
        $default = $node->args[3]?->value ?? $this->nodeFactory->createNull();

But this raises the question: What is the best way to cover named arguments. Calls like this:

$this->getRequestParameter($request, 'locale', default: 'en');

Is there already code in rector to handle this better? If not would be happy to provide an implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions