Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f690258

Browse filesBrowse files
committed
Fix issues raised by @weaverryan
1 parent 5b3ed9a commit f690258
Copy full SHA for f690258

File tree

Expand file treeCollapse file tree

2 files changed

+57
-23
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+57
-23
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+27-5Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,16 @@ private function completeDefinition($id, Definition $definition)
9696
*/
9797
private function autowireMethod($id, Definition $definition, \ReflectionMethod $reflectionMethod, $constructor)
9898
{
99-
$arguments = $constructor ? $definition->getArguments() : array();
99+
if ($constructor) {
100+
$arguments = $definition->getArguments();
101+
} elseif (0 === $reflectionMethod->getNumberOfParameters()) {
102+
return;
103+
} else {
104+
$arguments = array();
105+
}
100106

101-
foreach ($constructor->getParameters() as $index => $parameter) {
107+
$addMethodCall = false;
108+
foreach ($reflectionMethod->getParameters() as $index => $parameter) {
102109
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
103110
continue;
104111
}
@@ -107,7 +114,11 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
107114
if (!$typeHint = $parameter->getClass()) {
108115
// no default value? Then fail
109116
if (!$parameter->isOptional()) {
110-
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
117+
if ($constructor) {
118+
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
119+
}
120+
121+
return;
111122
}
112123

113124
// specifically pass the default value
@@ -122,16 +133,22 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
122133

123134
if (isset($this->types[$typeHint->name])) {
124135
$value = new Reference($this->types[$typeHint->name]);
136+
$addMethodCall = true;
125137
} else {
126138
try {
127139
$value = $this->createAutowiredDefinition($typeHint, $id);
140+
$addMethodCall = true;
128141
} catch (RuntimeException $e) {
129142
if ($parameter->allowsNull()) {
130143
$value = null;
131144
} elseif ($parameter->isDefaultValueAvailable()) {
132145
$value = $parameter->getDefaultValue();
133146
} else {
134-
throw $e;
147+
if ($constructor) {
148+
throw $e;
149+
}
150+
151+
return;
135152
}
136153
}
137154
}
@@ -155,7 +172,12 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
155172
// it's possible index 1 was set, then index 0, then 2, etc
156173
// make sure that we re-order so they're injected as expected
157174
ksort($arguments);
158-
$constructor ? $definition->setArguments($arguments) : $definition->addMethodCall($reflectionMethod->name, $arguments);
175+
176+
if ($constructor) {
177+
$definition->setArguments($arguments);
178+
} elseif ($addMethodCall) {
179+
$definition->addMethodCall($reflectionMethod->name, $arguments);
180+
}
159181
}
160182

161183
/**

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+30-18Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -367,24 +367,10 @@ public function testOptionalScalarArgsDontMessUpOrder()
367367
{
368368
$container = new ContainerBuilder();
369369

370-
$container->register('a', __NAMESPACE__ . '\A');
371-
$container->register('lille', __NAMESPACE__ . '\Lille');
372-
$container->register('with_optional_scalar', __NAMESPACE__ . '\MultipleArgumentsOptionalScalar')
370+
$container->register('a', __NAMESPACE__.'\A');
371+
$container->register('lille', __NAMESPACE__.'\Lille');
372+
$container->register('with_optional_scalar', __NAMESPACE__.'\MultipleArgumentsOptionalScalar')
373373
->setAutowired(true);
374-
}
375-
376-
public function testSetterInjection()
377-
{
378-
$container = new ContainerBuilder();
379-
$container->register('a', A::class);
380-
381-
$aArguments = array(new Reference('a'));
382-
383-
$container
384-
->register('setter_injection', SetterInjection::class)
385-
->setAutowired(true)
386-
->addMethodCall('setA', $aArguments)
387-
;
388374

389375
$pass = new AutowirePass();
390376
$pass->process($container);
@@ -425,6 +411,21 @@ public function testOptionalScalarArgsNotPassedIfLast()
425411
),
426412
$definition->getArguments()
427413
);
414+
}
415+
416+
public function testSetterInjection()
417+
{
418+
$container = new ContainerBuilder();
419+
$container->register('a', A::class);
420+
$aArguments = array(new Reference('a'));
421+
$container
422+
->register('setter_injection', SetterInjection::class)
423+
->setAutowired(true)
424+
->addMethodCall('setA', $aArguments)
425+
;
426+
427+
$pass = new AutowirePass();
428+
$pass->process($container);
428429

429430
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
430431

@@ -560,7 +561,6 @@ public function __construct(A $k)
560561
{
561562
}
562563
}
563-
564564
class MultipleArguments
565565
{
566566
public function __construct(A $k, $foo, Dunglas $dunglas)
@@ -600,4 +600,16 @@ public function setA(A $a)
600600
public function setDependencies(Foo $foo, A $a)
601601
{
602602
}
603+
604+
public function setBar()
605+
{
606+
}
607+
608+
public function setNotAutowireable(NotARealClass $n)
609+
{
610+
}
611+
612+
public function setOptionalNotAutowireable(NotARealClass $n = null)
613+
{
614+
}
603615
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.