-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Remove inaccurate @throws
on Container::get()
#51835
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
Conversation
Wouldn't we violate PSR-11 if we upcast |
Sorry, I'm not sure to understand ; can you develop ? Currently PSR\Container\ContainerInterface have the following phpdoc:
So, to be PSR, Symfony should only throw NotFoundExceptionInterface and ContainerExceptionInterface. Currently, since Symfony allows to throws The introduced |
@@ -199,10 +201,6 @@ public function has(string $id): bool | |||
/** | |||
* Gets a service. | |||
* | |||
* @throws ServiceCircularReferenceException When a circular reference is detected | |||
* @throws ServiceNotFoundException When the service is not defined | |||
* @throws \Exception if an exception has been thrown when the service has been resolved |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simply remove this line and nothing else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would kinda hide the problem.
Currently there is a reproducer in the Symfony tests:
symfony/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Lines 334 to 348 in ce48fbe
public function testGetThrowsException() | |
{ | |
$this->expectException(\Exception::class); | |
$this->expectExceptionMessage('Something went terribly wrong!'); | |
$c = new ProjectServiceContainer(); | |
try { | |
$c->get('throw_exception'); | |
} catch (\Exception $e) { | |
// Do nothing. | |
} | |
// Retry, to make sure that get*Service() will be called. | |
$c->get('throw_exception'); | |
} |
symfony/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Lines 491 to 494 in ce48fbe
protected function getThrowExceptionService() | |
{ | |
throw new \Exception('Something went terribly wrong!'); | |
} |
Should it be Symfony responsibility to try/catch and rethrow a compliant exception or should we rely on the developer to throw the right exception ?
The first solution seemed safer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@throws
is to document exceptions that can be thrown under the responbility of the called method. Re-thrown exceptions don't fit this definition so this @throws
is just wrong. Wrapping them all won't provide anything more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me the purpose of the @throws
tag is to know which Exceptions I should catch when doing
$container->get()
That's also how static analysis like PHPStan works.
But I'm ok removing the \Exception
line, it solves my issue.
Pr is update
@throws
on Container::get()
a3d6359
to
5edc2b1
Compare
Thank you @VincentLanglet. |
This would solve the inconsistency between ContainerInterface and Container described in #51834 because currently the Container phpdoc doesn't respect the ContainerInterface one.