Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Tweaking class not found autowiring error #24599

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

Closed
wants to merge 3 commits into from

Conversation

weaverryan
Copy link
Member

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.

@@ -485,7 +485,7 @@ private function createAutowiredDefinition($type)
private function createTypeNotFoundMessage(TypedReference $reference, $label)
{
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), false)) {
$message = sprintf('has type "%s" but this class cannot be loaded.', $type);
$message = sprintf('is type-hinted with "%s" but this class was not found.', $type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "cannot be loaded" case is possible when the parent class is not found. Maybe and "or the/a parent class is not found"? (Not sure about the wording but that's explaining the current one.)

Copy link
Member Author

@weaverryan weaverryan Oct 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any issue with addition a conditional?

if (class_exists($type)) {
    // we know a parent class must not exist
} else {
    // we know that the class itself does not exist
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class_exists($type) would trigger a fatal error when the parent is missing. container->getReflectionClass() contains a protection for that situation. Maybe passing "true" as 2nd argument to getReflectionClass and dealing with the exception could help make the message better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also please don't use type "hints". those are not hints like phpdocs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"hint" is used everywhere, in Symfony, in the PHP doc, on google. Technically is a bit more than a "hint" I agree, but that's what ppl are used to now IMHO.

Copy link
Contributor

@Tobion Tobion Oct 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all. It was explicitly avoided to call them hints in the php doc. See http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
And also the RFC about type declarations does not use the term hints for this reason.

@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Oct 19, 2017
@weaverryan weaverryan force-pushed the autowire-error-tweak branch 3 times, most recently from 109a91c to 425e41b Compare December 9, 2017 18:47
@nicolas-grekas
Copy link
Member

it looks like PHP 7.2 may behave differently, that could be the reason for the failure on deps=low

@nicolas-grekas
Copy link
Member

Just tested with php7.2 locally, green.
Which means the lowest version of the Config component might need a bump.
Also, what about putting the caught exception message in the one we construct? Would help I guess.
"...class is missing a parent class (%$e->getMessage()%)"

@nicolas-grekas
Copy link
Member

So, symfony/config must be bumped to ^3.3.7 to account for #23755.

Here is the full patch accounting for my comments:

diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
index d4abf04..58c1a9c 100644
--- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
@@ -491,12 +491,12 @@ class AutowirePass extends AbstractRecursivePass
                 $resource = new ClassExistenceResource($type, false);
                 // isFresh() will explode ONLY if a parent class/trait does not exist
                 $resource->isFresh(0);
-                $classExists = false;
+                $parentMsg = false;
             } catch (\ReflectionException $e) {
-                $classExists = true;
+                $parentMsg = $e->getMessage();
             }
 
-            $message = sprintf('has type "%s" but this class %s.', $type, $classExists ? 'cannot be loaded: its parent class may be missing' : 'was not found');
+            $message = sprintf('has type "%s" but this class %s.', $type, $parentMsg ? sprintf('is missing a parent class (%s)', $parentMsg) : 'was not found');
         } 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));
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
index 6687583..cae761e 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
@@ -374,7 +374,7 @@ class AutowirePassTest extends TestCase
 
     /**
      * @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
-     * @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: its parent class may be missing.
+     * @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).
      */
     public function testParentClassNotFoundThrowsException()
     {
diff --git a/src/Symfony/Component/DependencyInjection/composer.json b/src/Symfony/Component/DependencyInjection/composer.json
index 0ce0ffe..5f79b19 100644
--- a/src/Symfony/Component/DependencyInjection/composer.json
+++ b/src/Symfony/Component/DependencyInjection/composer.json
@@ -32,7 +32,7 @@
         "symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
     },
     "conflict": {
-        "symfony/config": "<3.3.1",
+        "symfony/config": "<3.3.7",
         "symfony/finder": "<3.3",
         "symfony/yaml": "<3.3"
     },

@weaverryan
Copy link
Member Author

@nicolas-grekas That patch looks great - the message is even better!

I've made the changes. Fabbot failure is a false-positive.

@nicolas-grekas
Copy link
Member

Rock-n-Roll :)

@fabpot
Copy link
Member

fabpot commented Dec 11, 2017

Thank you @weaverryan.

fabpot added a commit that referenced this pull request Dec 11, 2017
This PR was squashed before being merged into the 3.3 branch (closes #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
@fabpot fabpot closed this Dec 11, 2017
@weaverryan weaverryan deleted the autowire-error-tweak branch December 12, 2017 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.