Skip to content

Commit

Permalink
bug #36519 [FrameworkBundle] debug:autowiring: Fix wrong display when…
Browse files Browse the repository at this point in the history
… using class_alias (weaverryan)

This PR was merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] debug:autowiring: Fix wrong display when using class_alias

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | None
| License       | MIT
| Doc PR        | not needed

Imagine that `FooInterface` is an alias, but it is deprecated and so has a `class_alias` to `BarInterface`. Currently, `debug:autowiring` will actually print that's the autowiring alias is `BarInterface`, despite there being no such id in the container.

@nicolas-grekas originally (on purpose) made the 2nd argument to `Descriptor::getClassDescription()` be passed by reference *for* this exact feature - 56aab09 - but I can't figure out why. This change (which effectively removes the by-reference modifying) made no existing tests fail.

Discovered this because the whole deprecated`Doctrine\Common\Persistence\ManagerRegistry` vs newer `Doctrine\Persistence\ManagerRegistry` causes the issue.

Thanks!

Commits
-------

d34b437 Fixing a bug where class_alias would cause incorrect items in debug:autowiring
  • Loading branch information
nicolas-grekas committed Apr 23, 2020
2 parents bbbf80f + d34b437 commit 2d7b0b8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
Expand Up @@ -103,9 +103,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$serviceIdsNb = 0;
foreach ($serviceIds as $serviceId) {
$text = [];
$resolvedServiceId = $serviceId;
if (0 !== strpos($serviceId, $previousId)) {
$text[] = '';
if ('' !== $description = Descriptor::getClassDescription($serviceId, $serviceId)) {
if ('' !== $description = Descriptor::getClassDescription($serviceId, $resolvedServiceId)) {
if (isset($hasAlias[$serviceId])) {
continue;
}
Expand Down
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures;

class_alias(
ClassAliasTargetClass::class,
__NAMESPACE__ . '\ClassAliasExampleClass'
);

if (false) {
class ClassAliasExampleClass
{
}
}
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures;

class ClassAliasTargetClass
{
}
Expand Up @@ -97,4 +97,16 @@ public function testSearchNotAliasedServiceWithAll()
$tester->run(['command' => 'debug:autowiring', 'search' => 'redirect', '--all' => true]);
$this->assertStringContainsString('Pro-tip: use interfaces in your type-hints instead of classes to benefit from the dependency inversion principle.', $tester->getDisplay());
}

public function testNotConfusedByClassAliases()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);

$application = new Application(static::$kernel);
$application->setAutoExit(false);

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:autowiring', 'search' => 'ClassAlias']);
$this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ClassAliasExampleClass (public)', $tester->getDisplay());
}
}
Expand Up @@ -13,6 +13,7 @@ services:
public: false
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass:
class: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ClassAliasExampleClass: '@public'
env:
class: stdClass
arguments:
Expand Down

0 comments on commit 2d7b0b8

Please sign in to comment.