8000 [DI] Remove skipping magic for autowired methods · symfony/symfony@3fe448f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fe448f

Browse files
[DI] Remove skipping magic for autowired methods
1 parent eb6f3f7 commit 3fe448f

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@
2626
*/
2727
class AutowirePass extends AbstractRecursivePass
2828
{
29-
/**
30-
* @internal
31-
*/
32-
const MODE_REQUIRED = 1;
33-
34-
/**
35-
* @internal
36-
*/
37-
const MODE_OPTIONAL = 2;
38-
3929
private $definedTypes = array();
4030
private $types;
4131
private $ambiguousServiceTypes = array();
@@ -225,15 +215,15 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
225215
}
226216
}
227217

228-
$arguments = $this->autowireMethod($reflectionMethod, $arguments, self::MODE_REQUIRED);
218+
$arguments = $this->autowireMethod($reflectionMethod, $arguments);
229219

230220
if ($arguments !== $call[1]) {
231221
8000 $methodCalls[$i][1] = $arguments;
232222
}
233223
}
234224

235225
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
236-
if ($reflectionMethod->isPublic() && $arguments = $this->autowireMethod($reflectionMethod, array(), self::MODE_OPTIONAL)) {
226+
if ($reflectionMethod->isPublic() && $arguments = $this->autowireMethod($reflectionMethod, array())) {
237227
$methodCalls[] = array($reflectionMethod->name, $arguments);
238228
}
239229
}
@@ -246,20 +236,19 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
246236
*
247237
* @param \ReflectionMethod $reflectionMethod
248238
* @param array $arguments
249-
* @param int $mode
250239
*
251240
* @return array The autowired arguments
252241
*
253242
* @throws RuntimeException
254243
*/
255-
private function autowireMethod(\ReflectionMethod $reflectionMethod, array $arguments, $mode)
244+
private function autowireMethod(\ReflectionMethod $reflectionMethod, array $arguments)
256245
{
257-
$didAutowire = false; // Whether any arguments have been autowired or not
246+
$isConstructor = $reflectionMethod->isConstructor();
258247
foreach ($reflectionMethod->getParameters() as $index => $parameter) {
259248
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
260249
continue;
261250
}
262-
if (self::MODE_OPTIONAL === $mode && $parameter->isOptional() && !array_key_exists($index, $arguments)) {
251+
if (!$isConstructor && $parameter->isOptional() && !array_key_exists($index, $arguments)) {
263252
break;
264253
}
265254
if (method_exists($parameter, 'isVariadic') && $parameter->isVariadic()) {
@@ -271,11 +260,7 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
271260
if (!$typeName) {
272261
// no default value? Then fail
273262
if (!$parameter->isOptional()) {
274-
if (self::MODE_REQUIRED === $mode) {
275-
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
276-
}
277-
278-
return array();
263+
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
279264
}
280265

281266
if (!array_key_exists($index, $arguments)) {
@@ -287,31 +272,24 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
287272
}
288273

289274
if ($value = $this->getAutowiredReference($typeName)) {
290-
$didAutowire = true;
291275
$this->usedTypes[$typeName] = $this->currentId;
292276
} elseif ($parameter->isDefaultValueAvailable()) {
293277
$value = $parameter->getDefaultValue();
294278
} elseif ($parameter->allowsNull()) {
295279
$value = null;
296-
} elseif (self::MODE_REQUIRED === $mode) {
280+
} else {
297281
if ($classOrInterface = class_exists($typeName, false) ? 'class' : (interface_exists($typeName, false) ? 'interface' : null)) {
298282
$message = sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeName, $this->currentId, $classOrInterface);
299283
} else {
300284
$message = sprintf('Cannot autowire argument $%s of method %s::%s() for service "%s": Class %s does not exist.', $parameter->name, $reflectionMethod->class, $reflectionMethod->name, $this->currentId, $typeName);
301285
}
302286

303287
throw new RuntimeException($message);
304-
} else {
305-
return array();
306288
}
307289

308290
$arguments[$index] = $value;
309291
}
310292

311-
if (self::MODE_REQUIRED !== $mode && !$didAutowire) {
312-
return array();
313-
}
314-
315293
// it's possible index 1 was set, then index 0, then 2, etc
316294
// make sure that we re-order so they're injected as expected
317295
ksort($arguments);

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,20 @@ public function provideAutodiscoveredAutowiringOrder()
707707
array('CannotBeAutowiredReverseOrder'),
708708
);
709709
}
710+
711+
/**
712+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
713+
* @expectedExceptionMessage Cannot autowire argument $n of method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable() for service "foo": Class Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass does not exist.
714+
*/
715+
public function testNotwireableThrowsException()
716+
{
717+
$container = new ContainerBuilder();
718+
719+
$container->register('foo', NotWireable::class)->setAutowired(true);
720+
721+
$pass = new AutowirePass();
722+
$pass->process($container);
723+
}
710724
}
711725

712726
class Foo
@@ -925,18 +939,6 @@ public function setBar()
925939
}
926940

927941
/** @required <tab> prefix is on purpose */
928-
public function setNotAutowireable(NotARealClass $n)
929-
{
930-
// should not be called
931-
}
932-
933-
/** @required */
934-
public function setArgCannotAutowire($foo)
935-
{
936-
// should not be called
937-
}
938-
939-
/** @required */
940942
public function setOptionalNotAutowireable(NotARealClass $n = null)
941943
{
942944
// should not be called
@@ -1012,3 +1014,11 @@ public function setMultipleInstancesForOneArg(CollisionInterface $collision)
10121014
// should throw an exception
10131015
}
10141016
}
1017+
1018+
class NotWireable
1019+
{
1020+
/** @required */
1021+
public function setNotAutowireable(NotARealClass $n)
1022+
{
1023+
}
1024+
}

0 commit comments

Comments
 (0)
0