Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ return static function (RectorConfig $rectorConfig): void {
};
```

`ALLOW_NAMED_VARIADIC_ARGUMENTS` is enabled by default. If you set it to `false`, calls that include variadic arguments are skipped.
`ALLOW_NAMED_VARIADIC_ARGUMENTS` is enabled by default. For user-defined callables, the rule may still synthesize names for variadic entries (for example `values1`, `values2`), because PHP accepts unknown named arguments there and forwards them into the variadic parameter. Internal/native variadic callables such as `sprintf()` are skipped when a variadic tail is actually passed, because PHP rejects unknown named parameters there and named arguments also cannot be followed by positional ones. If you set it to `false`, calls that include variadic arguments are skipped entirely.

#### Implementing Your Own Strategy

Expand Down
21 changes: 17 additions & 4 deletions src/AddNamedArgumentsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,20 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->allowNamedVariadicArguments && $this->hasVariadicArguments($node, $parameters)) {
$hasVariadicArguments = $this->hasVariadicArguments($node, $parameters);
if (! $this->allowNamedVariadicArguments && $hasVariadicArguments) {
return null;
}

$hasChanges = $this->addNamesToArgs(node: $node, parameters: $parameters);
$functionReflection = Reflection::getFunctionReflection(node: $node, classReflection: $classReflection);
if ($hasVariadicArguments && ($functionReflection?->isInternal() ?? false)) {
return null;
}

$hasChanges = $this->addNamesToArgs(
node: $node,
parameters: $parameters,
);

if (! $hasChanges) {
return null;
Expand Down Expand Up @@ -152,9 +161,13 @@ private function addNamesToArgs(
$variadicArgCounters[$variadicParameterName] = $variadicIndex;

$arg->name = new Identifier(name: $variadicParameterName . $variadicIndex);
} else {
$arg->name = new Identifier(name: $parameter->getName());
$namedArgs[] = $arg;
$hasChanges = true;

continue;
}

$arg->name = new Identifier(name: $parameter->getName());
$namedArgs[] = $arg;
$hasChanges = true;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/DefaultStrategy/Fixture/internal_variadic.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

array_merge(['a' => 1], ['b' => 2], ['c' => 3]);

?>
-----
<?php

array_merge(['a' => 1], ['b' => 2], ['c' => 3]);

?>
11 changes: 11 additions & 0 deletions tests/DefaultStrategy/Fixture/userland_variadic_function.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

userlandVariadicFoo('php', '--version', '--ansi');

?>
-----
<?php

userlandVariadicFoo(command: 'php', values1: '--version', values2: '--ansi');

?>
2 changes: 1 addition & 1 deletion tests/DefaultStrategy/Fixture/variadic.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ sprintf('%s/%s', 'foo', 'bar');
-----
<?php

sprintf(format: '%s/%s', values1: 'foo', values2: 'bar');
sprintf('%s/%s', 'foo', 'bar');

?>
5 changes: 5 additions & 0 deletions tests/DefaultStrategy/FixtureFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

function userlandVariadicFoo(string $command, string ...$values): void {}
2 changes: 2 additions & 0 deletions tests/DefaultStrategy/config/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Rector\Config\RectorConfig;
use SavinMikhail\AddNamedArgumentsRector\AddNamedArgumentsRector;

require_once __DIR__ . '/../FixtureFunctions.php';

return RectorConfig::configure()
->withRules(rules: [
AddNamedArgumentsRector::class,
Expand Down
Loading