bug #16467 Fixing bad type-hint auto-wiring bug (weaverryan)

This PR was squashed before being merged into the 2.8 branch (closes #16467).

Discussion
----------

Fixing bad type-hint auto-wiring bug

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Previously, if you type-hinted a bad class name, we did not set the argument value. In fact, with `continue`, we skipped setting the argument, which meant that if argument 2 was skipped, then argument 3 would be put into argument 2's location.

But really, if I type-hint a non-existent class, this should throw a clear exception. afaik, in the original PR, it was said that we cannot throw an exception for a non-existent class because a later compiler-pass may manipulate those class names and "fix" them to be real classes. But, that's an edge case, and I think if you're using advanced functionality like that, you should not use auto-wiring. Throwing an exception is much more user-friendly. I personally hit this issue and was trying to figure out what was going wrong :).

About the exception message: I would like to tell the user which class is type-hinted correctly, but getting the type-hinted class for a non-existent class is not possible with reflection.

Thanks!

cc @dunglas

Commits
-------

b7b182e Fixing bad type-hint auto-wiring bug
This commit is contained in:
Fabien Potencier 2015-11-05 14:26:35 +01:00
commit bf849efa4b
2 changed files with 23 additions and 1 deletions

View File

@ -104,7 +104,7 @@ class AutowirePass implements CompilerPassInterface
// Typehint against a non-existing class
if (!$parameter->isDefaultValueAvailable()) {
continue;
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $reflectionException->getMessage()), 0, $reflectionException);
}
$value = $parameter->getDefaultValue();

View File

@ -201,6 +201,21 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$this->assertCount(0, $container->getDefinition('bar')->getArguments());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Cannot autowire argument 2 for Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument because the type-hinted class does not exist (Class Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass does not exist).
*/
public function testClassNotFoundThrowsException()
{
$container = new ContainerBuilder();
$aDefinition = $container->register('a', __NAMESPACE__.'\BadTypeHintedArgument');
$aDefinition->setAutowired(true);
$pass = new AutowirePass();
$pass->process($container);
}
}
class Foo
@ -298,3 +313,10 @@ class OptionalParameter
{
}
}
class BadTypeHintedArgument
{
public function __construct(Dunglas $k, NotARealClass $r)
{
}
}