8000 [DX][DependencyInjection] Suggest to write an implementation if the interface cannot be autowired by sroze · Pull Request #25547 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Suggest to write an implementation if the interface cannot be autowired
  • Loading branch information
sroze committed Dec 24, 2017
commit 961e3e719ce82a10ecefa8aef94d7ac969d7f835
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public function __construct(I $i)
}
}

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

interface CollisionInterface
{
}
Expand Down
0