Skip to content

Commit f08fdc0

Browse files
Benjamin Georgeaultfabpot
authored andcommitted
[DependencyInjection] Update PhpDumper generated Container to throw ParameterNotFoundException instead of InvalidArgumentException (more appropriate and follow throw in phpdoc)
The method `getParameter` in the container generated by the `PhpDumper` throw `InvalidArgumentException` instead of `ParameterNotFoundException` if the parameter is not defined. This commit fix it + update tests. Also update the basic `Container` and `ContainerInterface` throw phpdoc with the right exception `ParameterNotFoundException`.
1 parent 2876495 commit f08fdc0

File tree

59 files changed

+106
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+106
-105
lines changed

Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
1717
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1818
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
19+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1920
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2021
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2122
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
@@ -105,7 +106,7 @@ public function getParameterBag(): ParameterBagInterface
105106
*
106107
* @return array|bool|string|int|float|\UnitEnum|null
107108
*
108-
* @throws InvalidArgumentException if the parameter is not defined
109+
* @throws ParameterNotFoundException if the parameter is not defined
109110
*/
110111
public function getParameter(string $name)
111112
{

ContainerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection;
1313

1414
use Psr\Container\ContainerInterface as PsrContainerInterface;
15-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1616
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1717
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1818

@@ -50,7 +50,7 @@ public function initialized(string $id): bool;
5050
/**
5151
* @return array|bool|string|int|float|\UnitEnum|null
5252
*
53-
* @throws InvalidArgumentException if the parameter is not defined
53+
* @throws ParameterNotFoundException if the parameter is not defined
5454
*/
5555
public function getParameter(string $name);
5656

Dumper/PhpDumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,8 @@ private function startClass(string $class, string $baseClass, bool $hasProxyClas
12211221
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
12221222
use Symfony\Component\DependencyInjection\ContainerInterface;
12231223
use Symfony\Component\DependencyInjection\Container;
1224-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
12251224
use Symfony\Component\DependencyInjection\Exception\LogicException;
1225+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
12261226
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
12271227
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
12281228
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
@@ -1566,7 +1566,7 @@ public function getParameter(string $name): array|bool|string|int|float|\UnitEnu
15661566
}
15671567
15681568
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
1569-
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
1569+
throw new ParameterNotFoundException($name);
15701570
}
15711571
if (isset($this->loadedDynamicParameters[$name])) {
15721572
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
@@ -1615,7 +1615,7 @@ public function getParameterBag(): ParameterBagInterface
16151615
$getDynamicParameter = <<<'EOF'
16161616
$value = match ($name) {
16171617
%s
1618-
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%%s" must be defined.', $name)),
1618+
default => throw new ParameterNotFoundException($name),
16191619
};
16201620
$this->loadedDynamicParameters[$name] = true;
16211621
@@ -1624,7 +1624,7 @@ public function getParameterBag(): ParameterBagInterface
16241624
$getDynamicParameter = sprintf($getDynamicParameter, implode("\n", $dynamicPhp));
16251625
} else {
16261626
$loadedDynamicParameters = '[]';
1627-
$getDynamicParameter = str_repeat(' ', 8).'throw new InvalidArgumentException(sprintf(\'The dynamic parameter "%s" must be defined.\', $name));';
1627+
$getDynamicParameter = str_repeat(' ', 8).'throw new ParameterNotFoundException($name);';
16281628
}
16291629

16301630
$code .= <<<EOF

Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ private function getDynamicParameter(string $name)
12641264
0 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
12651265
1 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO,
12661266
],
1267-
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
1267+
default => throw new ParameterNotFoundException($name),
12681268
};
12691269
%A
12701270
PHP

Tests/Fixtures/php/closure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
44
use Symfony\Component\DependencyInjection\ContainerInterface;
55
use Symfony\Component\DependencyInjection\Container;
6-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
76
use Symfony\Component\DependencyInjection\Exception\LogicException;
7+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
88
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
99
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1010
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
66
use Symfony\Component\DependencyInjection\ContainerInterface;
77
use Symfony\Component\DependencyInjection\Container;
8-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
98
use Symfony\Component\DependencyInjection\Exception\LogicException;
9+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1010
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1111
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1212
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
66
use Symfony\Component\DependencyInjection\ContainerInterface;
77
use Symfony\Component\DependencyInjection\Container;
8-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
98
use Symfony\Component\DependencyInjection\Exception\LogicException;
9+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1010
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1111
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1212
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
66
use Symfony\Component\DependencyInjection\ContainerInterface;
77
use Symfony\Component\DependencyInjection\Container;
8-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
98
use Symfony\Component\DependencyInjection\Exception\LogicException;
9+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1010
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1111
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1212
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

Tests/Fixtures/php/custom_container_class_without_constructor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
66
use Symfony\Component\DependencyInjection\ContainerInterface;
77
use Symfony\Component\DependencyInjection\Container;
8-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
98
use Symfony\Component\DependencyInjection\Exception\LogicException;
9+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1010
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1111
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1212
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

Tests/Fixtures/php/services1-1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
66
use Symfony\Component\DependencyInjection\ContainerInterface;
77
use Symfony\Component\DependencyInjection\Container;
8-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
98
use Symfony\Component\DependencyInjection\Exception\LogicException;
9+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1010
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1111
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1212
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

0 commit comments

Comments
 (0)