Skip to content

Commit

Permalink
bug #25547 [DX][DependencyInjection] Suggest to write an implementati…
Browse files Browse the repository at this point in the history
…on if the interface cannot be autowired (sroze)

This PR was merged into the 3.4 branch.

Discussion
----------

[DX][DependencyInjection] Suggest to write an implementation if the interface cannot be autowired

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

This would add a hint for the developers when the interface cannot be wired. This suggests creating the implementation of the interface.

**Note:** this is 3.4 because I believe DX should be treated as bugs.
**Note 2:** fabbot issue is false positive

Commits
-------

961e3e7 Suggest to write an implementation if the interface cannot be autowired
  • Loading branch information
nicolas-grekas committed Dec 29, 2017
2 parents 3b8ca1a + 961e3e7 commit 0422471
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Expand Up @@ -457,6 +457,10 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
} else {
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));

if ($r->isInterface()) {
$message .= ' Did you create a class that implements this interface?';
}
}

$message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message);
Expand Down
Expand Up @@ -703,6 +703,23 @@ public function testSetterInjectionCollisionThrowsException()
$pass->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
* @expectedExceptionMessage Cannot autowire service "my_service": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\K::__construct()" references interface "Symfony\Component\DependencyInjection\Tests\Compiler\IInterface" but no such service exists. Did you create a class that implements this interface?
*/
public function testInterfaceWithNoImplementationSuggestToWriteOne()
{
$container = new ContainerBuilder();

$aDefinition = $container->register('my_service', K::class);
$aDefinition->setAutowired(true);

(new AutowireRequiredMethodsPass())->process($container);

$pass = new AutowirePass();
$pass->process($container);
}

/**
* @group legacy
* @expectedDeprecation Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "foo" service to "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" instead.
Expand Down
Expand Up @@ -90,6 +90,13 @@ public function __construct(I $i)
}
}

class K
{
public function __construct(IInterface $i)
{
}
}

interface CollisionInterface
{
}
Expand Down

0 comments on commit 0422471

Please sign in to comment.