Skip to content

Commit a7f63de

Browse files
brainexefabpot
authored andcommitted
[DependencyInjection] Fixed variadic method parameter in autowired classes
1 parent e3dcde7 commit a7f63de

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,11 @@ private static function getResourceMetadataForMethod(\ReflectionMethod $method)
338338
$class = false;
339339
}
340340

341+
$isVariadic = method_exists($parameter, 'isVariadic') && $parameter->isVariadic();
341342
$methodArgumentsMetadata[] = array(
342343
'class' => $class,
343344
'isOptional' => $parameter->isOptional(),
344-
'defaultValue' => $parameter->isOptional() ? $parameter->getDefaultValue() : null,
345+
'defaultValue' => ($parameter->isOptional() && !$isVariadic) ? $parameter->getDefaultValue() : null,
345346
);
346347
}
347348

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
1718

1819
/**
1920
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -35,6 +36,23 @@ public function testProcess()
3536
$this->assertEquals('foo', (string) $container->getDefinition('bar')->getArgument(0));
3637
}
3738

39+
/**
40+
* @requires PHP 5.6
41+
*/
42+
public function testProcessVariadic()
43+
{
44+
$container = new ContainerBuilder();
45+
$container->register('foo', Foo::class);
46+
$definition = $container->register('fooVariadic', FooVariadic::class);
47+
$definition->setAutowired(true);
48+
49+
$pass = new AutowirePass();
50+
$pass->process($container);
51+
52+
$this->assertCount(1, $container->getDefinition('fooVariadic')->getArguments());
53+
$this->assertEquals('foo', (string) $container->getDefinition('fooVariadic')->getArgument(0));
54+
}
55+
3856
public function testProcessAutowireParent()
3957
{
4058
$container = new ContainerBuilder();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\includes;
4+
5+
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
6+
7+
class FooVariadic
8+
{
9+
public function __construct(Foo $foo)
10+
{
11+
}
12+
13+
public function bar(...$arguments)
14+
{
15+
}
16+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy