8000 minor #31306 [DI] Improve exception message on missing $ of named arg… · symfony/symfony@7cd1bdd · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cd1bdd

Browse files
committed
minor #31306 [DI] Improve exception message on missing $ of named argument (jschaedl)
This PR was squashed before being merged into the 4.3-dev branch (closes #31306). Discussion ---------- [DI] Improve exception message on missing $ of named argument | Q | A | ------------- | --- | Branch? | master | Bug fix? | - | New feature? | - | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #31265 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | not related <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> ### Description As described in #31265, missing the prefix `$` in an argument name: ``` App\Word\WordChecker: arguments: checkers: - '@app\Word\Checker\StaticWordChecker' - '@app\Word\Checker\BannedWorldListChecker' ``` led to the following error: `Invalid service "App\Word\WordChecker": the value of argument "checkers" of method "__construct()" must be null, an instance of Symfony\Component\DependencyInjection\Reference or an instance of Symfony\Component\DependencyInjection\Definition, array given.` As this error message is quite confusing I changed it to: `Invalid service "App\Word\WordChecker": Did you forget to add the "$" prefix to argument checkers` ### Todo - [x] add a unit test Commits ------- d0e4499 [DI] Improve exception message on missing $ of named argument
2 parents 707b1df + d0e4499 commit 7cd1bdd

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected function processValue($value, $isRoot = false)
5353
$parameters = $r->getParameters();
5454
}
5555

56+
if (isset($key[0]) && '$' !== $key[0] && !class_exists($key)) {
57+
throw new InvalidArgumentException(sprintf('Invalid service "%s": did you forget to add the "$" prefix to argument "%s"?', $this->currentId, $key));
58+
}
59+
5660
if (isset($key[0]) && '$' === $key[0]) {
5761
foreach ($parameters as $j => $p) {
5862
if ($key === '$'.$p->name) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ public function testTypedArgument()
149149
$this->assertEquals([new Reference('foo'), '123'], $definition->getArguments());
150150
}
151151

152+
/**
153+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
154+
* @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": did you forget to add the "$" prefix to argument "apiKey"?
155+
*/
156+
public function testTypedArgumentWithMissingDollar()
157+
{
158+
$container = new ContainerBuilder();
159+
160+
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
161+
$definition->setArgument('apiKey', '123');
162+
163+
$pass = new ResolveNamedArgumentsPass();
164+
$pass->process($container);
165+
}
166+
152167
public function testResolvesMultipleArgumentsOfTheSameType()
153168
{
154169
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0