8000 bug #21602 [DI] Always consider abstract getters as autowiring candid… · symfony/symfony@e37bff9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e37bff9

Browse files
committed
bug #21602 [DI] Always consider abstract getters as autowiring candidates (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Always consider abstract getters as autowiring candidates | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes (a missing part of getter autowiring really) | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - When a definition is set to be autowired with no method explicitly configured, we already wire the constructor. We should also autowire abstract getters - with the same reasoning that makes us autowire the constructor: without concrete getters, the class is unusable. This just makes it usable again. Commits ------- 8f246bd [DI] Always consider abstract getters as autowiring candidates
2 parents f1f982d + 8f246bd commit e37bff9

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
176176
continue 2;
177177
}
178178
}
179+
180+
if ($reflectionMethod->isAbstract() && !$reflectionMethod->getNumberOfParameters()) {
181+
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod;
182+
}
179183
}
180184

181185
if ($notFound = array_diff($autowiredMethods, $found)) {
@@ -501,7 +505,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint)
501505
$this->populateAvailableType($argumentId, $argumentDefinition);
502506

503507
try {
504-
$this->processValue($argumentDefinition, true);
508+
$this->processValue($argumentDefinition);
505509
$this->currentId = $currentId;
506510
} catch (RuntimeException $e) {
507511
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractGetterOverriding;
1819
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
1920
use Symfony\Component\DependencyInjection\Tests\Fixtures\GetterOverriding;
2021

@@ -519,6 +520,27 @@ public function testExplicitMethodInjection()
519520
);
520521
}
521522

523+
/**
524+
* @requires PHP 7.0
525+
*/
526+
public function testAbstractGetterOverriding()
527+
{
528+
$container = new ContainerBuilder();
529+
530+
$container
531+
->register('getter_overriding', AbstractGetterOverriding::class)
532+
->setAutowired(true)
533+
;
534+
535+
$pass = new AutowirePass();
536+
$pass->process($container);
537+
538+
$overridenGetters = $container->getDefinition('getter_overriding')->getOverriddenGetters();
539+
$this->assertEquals(array(
540+
'abstractgetfoo' => new Reference('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Foo'),
541+
), $overridenGetters);
542+
}
543+
522544
/**
523545
* @requires PHP 7.1
524546
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
15+
16+
abstract class AbstractGetterOverriding
17+
{
18+
abstract public function abstractGetFoo(): Foo;
19+
abstract public function abstractDoFoo($arg = null): Foo;
20+
}

0 commit comments

Comments
 (0)
{"resolvedServerColorMode":"day"}
0