8000 [DependencyInjection] Added support for variadics in named arguments by PabloKowalczyk · Pull Request #24937 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Added support for variadics in named arguments #24937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[DependencyInjection] Added support for variadics in named arguments
  • Loading branch information
pkowalczyk authored and PabloKowalczyk committed Nov 20, 2017
commit 9961ea561b543feffd7dcca990d493583863a3d3
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
use Symfony\Component\DependencyInjection\Reference;

Expand Down Expand Up @@ -87,6 +88,25 @@ protected function processValue($value, $isRoot = false)
}
}

$lastResolvedArgument = \end($resolvedArguments);

if (\is_array($lastResolvedArgument) && !empty($lastResolvedArgument)) {
try {
$reflection = $this->getReflectionMethod($value, $method);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better implementation would hook into the existing foreach loop on line 56-57.
We just need a if (\PHP_VERSION_ID >= 50600 && $p->isVariadic() check inside the loop.
No need for enforcing this is the "last argument", PHP already did so.
And the key of the added params must be explicitly set ($resolvedArguments[$j++] instead of $resolvedArguments[], which can be wrong)

$parameters = $reflection->getParameters();
$lastParam = \end($parameters);

if ($lastParam->isVariadic()) {
\array_pop($resolvedArguments);

foreach ($lastResolvedArgument as $argument) {
$resolvedArguments[] = $argument;
}
}
} catch (RuntimeException $runtimeException) {
}
}

if ($resolvedArguments !== $call[1]) {
ksort($resolvedArguments);
$calls[$i][1] = $resolvedArguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class NamedArgumentsDummy
{
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName)
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ...$variadics)
{
}

Expand Down
0