10000 bug #21372 [DependencyInjection] Fixed variadic method parameter in a… · symfony/symfony@ed5eb6d · GitHub
[go: up one dir, main page]

Skip to content

Commit ed5eb6d

Browse files
committed
bug #21372 [DependencyInjection] Fixed variadic method parameter in autowired classes (brainexe)
This PR was squashed before being merged into the 3.1 branch (closes #21372). Discussion ---------- [DependencyInjection] Fixed variadic method parameter in autowired classes | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | -- | License | MIT Autowiring classes containing methods with variadic method parameter throws a ReflectionException in compile process: ``` PHP Fatal error: Uncaught ReflectionException: Internal error: Failed to retrieve the default value in /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php:437 Stack trace: #0 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(437): ReflectionParameter->getDefaultValue() #1 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(80): Symfony\Component\DependencyInjection\Compiler\AutowirePass::getResourceMetadataForMethod(Object(ReflectionMethod)) #2 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(105): Symfony\Component\DependencyInjection\Compiler\AutowirePass::createResourceForClass(Object(ReflectionClass)) #3 /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php(48): Symfony\Component\DependencyInjection\Compiler\AutowirePass->completeDefinition('__controller.Sw...', Object(Symfony\Component\DependencyInjection\Definition), Array) #4 /.../vendor/symfony/dependency-injection/Compiler/Compiler in /.../vendor/symfony/dependency-injection/Compiler/AutowirePass.php on line 437 ``` **Example:** ``` <?php class FooVariadic { public function bar(...$arguments) { } } $method = new ReflectionMethod(FooVariadic::class, 'bar'); $parameter = $method->getParameters()[0]; $parameter->getDefaultValue(); // -> ReflectionException: Internal error: Failed to retrieve the default value in ... ``` Commits ------- a7f63de [DependencyInjection] Fixed variadic method parameter in autowired classes
2 parents e3dcde7 + a7f63de commit ed5eb6d

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

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

+2-1
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

+18
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();
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)
0