Skip to content

Commit 6faf589

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: [FrameworkBundle] remove messenger cache if not enabled [HttpClient] Fix strict parsing of response status codes [DI] Suggest typed argument when binding fails with untyped argument
2 parents 1d6c4ca + 468bfb6 commit 6faf589

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Compiler/ResolveBindingsPass.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ protected function processValue($value, $isRoot = false)
114114
return parent::processValue($value, $isRoot);
115115
}
116116

117+
$bindingNames = [];
118+
117119
foreach ($bindings as $key => $binding) {
118120
list($bindingValue, $bindingId, $used, $bindingType, $file) = $binding->getValues();
119121
if ($used) {
@@ -123,7 +125,11 @@ protected function processValue($value, $isRoot = false)
123125
$this->unusedBindings[$bindingId] = [$key, $this->currentId, $bindingType, $file];
124126
}
125127

126-
if (preg_match('/^(?:(?:array|bool|float|int|string) )?\$/', $key)) {
128+
if (preg_match('/^(?:(?:array|bool|float|int|string|([^ $]++)) )\$/', $key, $m)) {
129+
$bindingNames[substr($key, \strlen($m[0]))] = $binding;
130+
}
131+
132+
if (!isset($m[1])) {
127133
continue;
128134
}
129135

@@ -184,11 +190,17 @@ protected function processValue($value, $isRoot = false)
184190
continue;
185191
}
186192

187-
if (!$typeHint || '\\' !== $typeHint[0] || !isset($bindings[$typeHint = substr($typeHint, 1)])) {
193+
if ($typeHint && '\\' === $typeHint[0] && isset($bindings[$typeHint = substr($typeHint, 1)])) {
194+
$arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
195+
188196
continue;
189197
}
190198

191-
$arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
199+
if (isset($bindingNames[$parameter->name])) {
200+
$bindingKey = array_search($binding, $bindings, true);
201+
$argumentType = substr($bindingKey, 0, strpos($bindingKey, ' '));
202+
$this->errorMessages[] = sprintf('Did you forget to add the type "%s" to argument "$%s" of method "%s::%s()"?', $argumentType, $parameter->name, $reflectionMethod->class, $reflectionMethod->name);
203+
}
192204
}
193205

194206
if ($arguments !== $call[1]) {

Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
2222
use Symfony\Component\DependencyInjection\Definition;
23+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2324
use Symfony\Component\DependencyInjection\Reference;
2425
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2526
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
@@ -169,4 +170,19 @@ public function testSyntheticServiceWithBind()
169170

170171
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
171172
}
173+
174+
public function testEmptyBindingTypehint()
175+
{
176+
$this->expectException(InvalidArgumentException::class);
177+
$this->expectExceptionMessage('Did you forget to add the type "string" to argument "$apiKey" of method "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy::__construct()"?');
178+
179+
$container = new ContainerBuilder();
180+
$bindings = [
181+
'string $apiKey' => new BoundArgument('foo'),
182+
];
183+
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
184+
$definition->setBindings($bindings);
185+
$pass = new ResolveBindingsPass();
186+
$pass->process($container);
187+
}
172188
}

0 commit comments

Comments
 (0)