bug #29597 [DI] fix reporting bindings on overriden services as unused (nicolas-grekas)
This PR was merged into the 3.4 branch.
Discussion
----------
[DI] fix reporting bindings on overriden services as unused
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #28326
| License | MIT
| Doc PR | -
Commits
-------
e07ad2b477
[DI] fix reporting bindings on overriden services as unused
This commit is contained in:
commit
44e9a91f30
@ -34,6 +34,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
$this->usedBindings = $container->getRemovedBindingIds();
|
||||
|
||||
try {
|
||||
parent::process($container);
|
||||
|
||||
|
@ -123,6 +123,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
|
||||
private $removedIds = array();
|
||||
|
||||
private $removedBindingIds = array();
|
||||
|
||||
private static $internalTypes = array(
|
||||
'int' => true,
|
||||
'float' => true,
|
||||
@ -531,7 +533,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a compiled container is not allowed.', $id));
|
||||
}
|
||||
|
||||
unset($this->definitions[$id], $this->aliasDefinitions[$id], $this->removedIds[$id]);
|
||||
$this->removeId($id);
|
||||
unset($this->removedIds[$id]);
|
||||
|
||||
parent::set($id, $service);
|
||||
}
|
||||
@ -544,8 +547,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
public function removeDefinition($id)
|
||||
{
|
||||
if (isset($this->definitions[$id = $this->normalizeId($id)])) {
|
||||
unset($this->definitions[$id]);
|
||||
$this->removedIds[$id] = true;
|
||||
$this->removeId($id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -876,7 +878,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
|
||||
}
|
||||
|
||||
unset($this->definitions[$alias], $this->removedIds[$alias]);
|
||||
$this->removeId($alias);
|
||||
unset($this->removedIds[$alias]);
|
||||
|
||||
return $this->aliasDefinitions[$alias] = $id;
|
||||
}
|
||||
@ -889,8 +892,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
public function removeAlias($alias)
|
||||
{
|
||||
if (isset($this->aliasDefinitions[$alias = $this->normalizeId($alias)])) {
|
||||
unset($this->aliasDefinitions[$alias]);
|
||||
$this->removedIds[$alias] = true;
|
||||
$this->removeId($alias);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1019,7 +1021,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
|
||||
$id = $this->normalizeId($id);
|
||||
|
||||
unset($this->aliasDefinitions[$id], $this->removedIds[$id]);
|
||||
$this->removeId($id);
|
||||
unset($this->removedIds[$id]);
|
||||
|
||||
return $this->definitions[$id] = $definition;
|
||||
}
|
||||
@ -1552,6 +1555,18 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
return $services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets removed binding ids.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getRemovedBindingIds()
|
||||
{
|
||||
return $this->removedBindingIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a reasonably unique hash of a value.
|
||||
*
|
||||
@ -1656,4 +1671,21 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function removeId($id)
|
||||
{
|
||||
$this->removedIds[$id] = true;
|
||||
unset($this->aliasDefinitions[$id]);
|
||||
|
||||
if (!isset($this->definitions[$id])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->definitions[$id]->getBindings() as $binding) {
|
||||
list(, $identifier) = $binding->getValues();
|
||||
$this->removedBindingIds[$identifier] = true;
|
||||
}
|
||||
|
||||
unset($this->definitions[$id]);
|
||||
}
|
||||
}
|
||||
|
@ -111,4 +111,22 @@ class ResolveBindingsPassTest extends TestCase
|
||||
|
||||
$this->assertEquals(array(array('setDefaultLocale', array('fr'))), $definition->getMethodCalls());
|
||||
}
|
||||
|
||||
public function testOverriddenBindings()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$binding = new BoundArgument('bar');
|
||||
|
||||
$container->register('foo', 'stdClass')
|
||||
->setBindings(array('$foo' => clone $binding));
|
||||
$container->register('bar', 'stdClass')
|
||||
->setBindings(array('$foo' => clone $binding));
|
||||
|
||||
$container->register('foo', 'stdClass');
|
||||
|
||||
(new ResolveBindingsPass())->process($container);
|
||||
|
||||
$this->assertInstanceOf('stdClass', $container->get('foo'));
|
||||
}
|
||||
}
|
||||
|
@ -434,7 +434,7 @@ class ResolveChildDefinitionsPassTest extends TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
||||
* @expectedExceptionMessageRegExp /^Circular reference detected for service "c", path: "c -> b -> a -> c"./
|
||||
* @expectedExceptionMessageRegExp /^Circular reference detected for service "a", path: "a -> c -> b -> a"./
|
||||
*/
|
||||
public function testProcessDetectsChildDefinitionIndirectCircularReference()
|
||||
{
|
||||
|
@ -559,7 +559,7 @@ class ContainerBuilderTest extends TestCase
|
||||
$config->setDefinition('baz', new Definition('BazClass'));
|
||||
$config->setAlias('alias_for_foo', 'foo');
|
||||
$container->merge($config);
|
||||
$this->assertEquals(array('service_container', 'foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
|
||||
$this->assertEquals(array('foo', 'bar', 'service_container', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
|
||||
|
||||
$aliases = $container->getAliases();
|
||||
$this->assertArrayHasKey('alias_for_foo', $aliases);
|
||||
|
@ -4,6 +4,9 @@ services:
|
||||
class: Symfony\Component\DependencyInjection\ContainerInterface
|
||||
public: true
|
||||
synthetic: true
|
||||
foo:
|
||||
class: App\FooService
|
||||
public: true
|
||||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo:
|
||||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
|
||||
public: true
|
||||
@ -16,6 +19,3 @@ services:
|
||||
|
||||
shared: false
|
||||
configurator: c
|
||||
foo:
|
||||
class: App\FooService
|
||||
public: true
|
||||
|
@ -4,15 +4,6 @@ services:
|
||||
class: Symfony\Component\DependencyInjection\ContainerInterface
|
||||
public: true
|
||||
synthetic: true
|
||||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo:
|
||||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
|
||||
public: true
|
||||
tags:
|
||||
- { name: foo }
|
||||
- { name: baz }
|
||||
deprecated: '%service_id%'
|
||||
arguments: [1]
|
||||
factory: f
|
||||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar:
|
||||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar
|
||||
public: true
|
||||
@ -23,3 +14,12 @@ services:
|
||||
lazy: true
|
||||
arguments: [1]
|
||||
factory: f
|
||||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo:
|
||||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
|
||||
public: true
|
||||
tags:
|
||||
- { name: foo }
|
||||
- { name: baz }
|
||||
deprecated: '%service_id%'
|
||||
arguments: [1]
|
||||
factory: f
|
||||
|
Reference in New Issue
Block a user