Skip to content

[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder #17719

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

lukaszmakuch
Copy link

[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder

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

The ContainerBuilder class wasn't implementing the ContainerInterface interface as it should according to the Liskov substitution principle.

It caused dependency on implementation instead than on the interface when using an instance of the ContainerBuilder class.

For example this code:

<?php
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/* @var $container ContainerInterface */
try {
    $container->get("wrong_service_key");
} catch (ServiceNotFoundException $e) {
    //action on a wrong key
}

works for correct implementations of the ContainerInterface interface, but the ContainerBuilder class was breaking that by throwing more abstract exceptions.

As the ServiceNotFoundException exceptions inherits from the InvalidArgumentException exception, this change shouldn't break code which catches the InvalidArgumentException exception while fetching values from a ContainerInterface interface implementation.

…nerBuilder

ContainerBuilder was throwing more abstract exceptions than ContainerInterface.
After this change, it's consistent with its interface.
Done to follow the Liskov substitution principle.
Added white space between variable name and description.
$this->fail('->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
} catch (ServiceNotFoundException $e) {
$this->assertEquals('You have requested a non-existent service "baz".', $e->getMessage(), '->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');

Copy link
Contributor

Choose a reason for hiding this comment

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

empty line too much

I removed an unnecessary empty line in the ContainerBuilderTest file.
@lukaszmakuch
Copy link
Author

@xabbuh, the white line has been removed.

@@ -440,7 +442,8 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV

try {
$definition = $this->getDefinition($id);
} catch (InvalidArgumentException $e) {
} catch (ServiceNotFoundException $e) {

Copy link
Member

Choose a reason for hiding this comment

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

This empty line should be removed.

@fabpot
Copy link
Member

fabpot commented Feb 14, 2016

👍

@fabpot
Copy link
Member

fabpot commented Feb 14, 2016

Thank you @lukaszmakuch.

fabpot added a commit that referenced this pull request Feb 14, 2016
…d of ContainerBuilder (lukaszmakuch)

This PR was squashed before being merged into the 2.3 branch (closes #17719).

Discussion
----------

[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder

[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder

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

The ContainerBuilder class wasn't implementing the ContainerInterface interface as it should according to the Liskov substitution principle.

It caused dependency on implementation instead than on the interface when using an instance of the ContainerBuilder class.

For example this code:
```php
<?php
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/* @var $container ContainerInterface */
try {
    $container->get("wrong_service_key");
} catch (ServiceNotFoundException $e) {
    //action on a wrong key
}
```
works for correct implementations of the ContainerInterface interface, but the ContainerBuilder class was breaking that by throwing more abstract exceptions.

As the ServiceNotFoundException exceptions inherits from the InvalidArgumentException exception, this change shouldn't break code which catches the InvalidArgumentException exception while fetching values from a ContainerInterface interface implementation.

Commits
-------

aecb0fa [DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder
@fabpot fabpot closed this Feb 14, 2016
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