Skip to content

Commit 4b95961

Browse files
committed
minor symfony#24599 Tweaking class not found autowiring error (weaverryan)
This PR was squashed before being merged into the 3.3 branch (closes symfony#24599). Discussion ---------- Tweaking class not found autowiring error | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | n/a A trainee in my workshop today hit this error, and it struck me as not as clear as it could be - it's really a "Class Not Found" error. Commits ------- 48832e6 Tweaking class not found autowiring error
2 parents e7ccd22 + 48832e6 commit 4b95961

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14+
use Symfony\Component\Config\Resource\ClassExistenceResource;
1415
use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Definition;
@@ -485,7 +486,17 @@ private function createAutowiredDefinition($type)
485486
private function createTypeNotFoundMessage(TypedReference $reference, $label)
486487
{
487488
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), false)) {
488-
$message = sprintf('has type "%s" but this class cannot be loaded.', $type);
489+
// either $type does not exist or a parent class does not exist
490+
try {
491+
$resource = new ClassExistenceResource($type, false);
492+
// isFresh() will explode ONLY if a parent class/trait does not exist
493+
$resource->isFresh(0);
494+
$parentMsg = false;
495+
} catch (\ReflectionException $e) {
496+
$parentMsg = $e->getMessage();
497+
}
498+
499+
$message = sprintf('has type "%s" but this class %s.', $type, $parentMsg ? sprintf('is missing a parent class (%s)', $parentMsg) : 'was not found');
489500
} else {
490501
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
491502
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($reference));

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function testDontTriggerAutowiring()
359359

360360
/**
361361
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
362-
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class cannot be loaded.
362+
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class was not found.
363363
*/
364364
public function testClassNotFoundThrowsException()
365365
{
@@ -374,7 +374,7 @@ public function testClassNotFoundThrowsException()
374374

375375
/**
376376
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
377-
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass" but this class cannot be loaded.
377+
* @expectedExceptionMessage Cannot autowire service "a": argument "$r" of method "Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass" but this class is missing a parent class (Class Symfony\Bug\NotExistClass not found).
378378
*/
379379
public function testParentClassNotFoundThrowsException()
380380
{
@@ -761,7 +761,7 @@ public function testNotWireableCalls($method, $expectedMsg)
761761
public function provideNotWireableCalls()
762762
{
763763
return array(
764-
array('setNotAutowireable', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class cannot be loaded.'),
764+
array('setNotAutowireable', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class was not found.'),
765765
array('setDifferentNamespace', 'Cannot autowire service "foo": argument "$n" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setDifferentNamespace()" references class "stdClass" but no such service exists. It cannot be auto-registered because it is from a different root namespace.'),
766766
array(null, 'Cannot autowire service "foo": method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod()" must be public.'),
767767
);

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3333
},
3434
"conflict": {
35-
"symfony/config": "<3.3.1",
35+
"symfony/config": "<3.3.7",
3636
"symfony/finder": "<3.3",
3737
"symfony/yaml": "<3.3"
3838
},

0 commit comments

Comments
 (0)