bug #31088 [DI] fix removing non-shared definition while inlining them (nicolas-grekas)

This PR was merged into the 4.2 branch.

Discussion
----------

[DI] fix removing non-shared definition while inlining them

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29628
| License       | MIT
| Doc PR        | -

I didn't manage to create a specific test case but this still has 100% coverage for the added lines and fixed the reproducer (and makes sense also :) )

Commits
-------

317e820694 [DI] fix removing non-shared definition while inlining them
This commit is contained in:
Nicolas Grekas 2019-04-12 16:35:03 +02:00
commit 8297a75eb7

View File

@ -60,6 +60,7 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
$analyzedContainer = $container;
}
try {
$remainingInlinedIds = [];
$this->connectedIds = $this->notInlinedIds = $container->getDefinitions();
do {
if ($this->analyzingPass) {
@ -83,8 +84,10 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
}
}
foreach ($this->inlinedIds as $id => $isPublic) {
if (!$isPublic) {
foreach ($this->inlinedIds as $id => $isPublicOrNotShared) {
if ($isPublicOrNotShared) {
$remainingInlinedIds[$id] = $id;
} else {
$container->removeDefinition($id);
$analyzedContainer->removeDefinition($id);
}
@ -94,6 +97,14 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
if ($this->inlinedIds && $this->repeatedPass) {
$this->repeatedPass->setRepeat();
}
foreach ($remainingInlinedIds as $id) {
$definition = $container->getDefinition($id);
if (!$definition->isShared() && !$definition->isPublic()) {
$container->removeDefinition($id);
}
}
} finally {
$this->container = null;
$this->connectedIds = $this->notInlinedIds = $this->inlinedIds = [];
@ -131,7 +142,7 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
}
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
$this->inlinedIds[$id] = $definition->isPublic();
$this->inlinedIds[$id] = $definition->isPublic() || !$definition->isShared();
$this->notInlinedIds[$this->currentId] = true;
if ($definition->isShared()) {