Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3f494f

Browse files
[DI] Restore skipping logic when autowiring getters
1 parent a001c2e commit b3f494f

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
257257
continue;
258258
}
259259

260-
$typeName = InheritanceProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
260+
$type = InheritanceProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
261261

262-
if (!$typeName) {
262+
if (!$type) {
263263
// no default value? Then fail
264264
if (!$parameter->isOptional()) {
265265
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));
@@ -273,17 +273,17 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
273273
continue;
274274
}
275275

276-
if ($value = $this->getAutowiredReference($typeName)) {
277-
$this->usedTypes[$typeName] = $this->currentId;
276+
if ($value = $this->getAutowiredReference($type)) {
277+
$this->usedTypes[$type] = $this->currentId;
278278
} elseif ($parameter->isDefaultValueAvailable()) {
279279
$value = $parameter->getDefaultValue();
280280
} elseif ($parameter->allowsNull()) {
281281
$value = null;
282282
} else {
283-
if ($classOrInterface = class_exists($typeName, false) ? 'class' : (interface_exists($typeName, false) ? 'interface' : null)) {
284-
$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);
283+
if ($classOrInterface = class_exists($type, false) ? 'class' : (interface_exists($type, false) ? 'interface' : null)) {
284+
$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.', $type, $this->currentId, $classOrInterface);
285285
} else {
286-
$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);
286+
$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, $type);
287287
}
288288

289289
throw new RuntimeException($message);
@@ -313,24 +313,18 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
313313
if (isset($overridenGetters[$lcMethod]) || $reflectionMethod->getNumberOfParameters() || $reflectionMethod->isConstructor()) {
314314
continue;
315315
}
316-
if (!$typeName = InheritanceProxyHelper::getTypeHint($reflectionMethod, null, true)) {
317-
$typeName = InheritanceProxyHelper::getTypeHint($reflectionMethod);
316+
if (!$type = InheritanceProxyHelper::getTypeHint($reflectionMethod, null, true)) {
317+
$type = InheritanceProxyHelper::getTypeHint($reflectionMethod);
318318

319-
throw new RuntimeException(sprintf('Cannot autowire service "%s": getter %s::%s() must%s be given a return value explicitly.', $this->currentId, $reflectionMethod->class, $reflectionMethod->name, $typeName ? '' : ' have a return-type hint or'));
319+
throw new RuntimeException(sprintf('Cannot autowire service "%s": getter %s::%s() must%s have its return value be configured explicitly.', $this->currentId, $reflectionMethod->class, $reflectionMethod->name, $type ? '' : ' have a return-type hint or'));
320320
}
321321

322-
if (!$typeRef = $this->getAutowiredReference($typeName)) {
323-
if ($classOrInterface = class_exists($typeName, false) ? 'class' : (interface_exists($typeName, false) ? 'interface' : null)) {
324-
$message = sprintf('Unable to autowire return type "%s" for service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeName, $this->currentId, $classOrInterface);
325-
} else {
326-
$message = sprintf('Cannot autowire return type of getter %s::%s() for service "%s": Class %s does not exist.', $reflectionMethod->class, $reflectionMethod->name, $this->currentId, $typeName);
327-
}
328-
329-
throw new RuntimeException($message);
322+
if (!$typeRef = $this->getAutowiredReference($type)) {
323+
continue;
330324
}
331325

332326
$overridenGetters[$lcMethod] = $typeRef;
333-
$this->usedTypes[$typeName] = $this->currentId;
327+
$this->usedTypes[$type] = $this->currentId;
334328
}
335329

336330
return $overridenGetters;
@@ -339,21 +333,21 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
339333
/**
340334
* @return Reference|null A reference to the service matching the given type, if any
341335
*/
342-
private function getAutowiredReference($typeName, $autoRegister = true)
336+
private function getAutowiredReference($type, $autoRegister = true)
343337
{
344-
if ($this->container->has($typeName) && !$this->container->findDefinition($typeName)->isAbstract()) {
345-
return new Reference($typeName);
338+
if ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract()) {
339+
return new Reference($type);
346340
}
347341

348342
if (null === $this->types) {
349343
$this->populateAvailableTypes();
350344
}
351345

352-
if (isset($this->types[$typeName])) {
353-
return new Reference($this->types[$typeName]);
346+
if (isset($this->types[$type])) {
347+
return new Reference($this->types[$type]);
354348
}
355349

356-
if ($autoRegister && $class = $this->container->getReflectionClass($typeName, true)) {
350+
if ($autoRegister && $class = $this->container->getReflectionClass($type, true)) {
357351
return $this->createAutowiredDefinition($class);
358352
}
359353
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/GetterOverriding.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,25 @@ protected function getBar(): Bar
3535
// should be called
3636
}
3737

38+
/** @required */
39+
public function getUnknown(): NotExist
40+
{
41+
// should not be called
42+
}
43+
3844
/** @required */
3945
public function getExplicitlyDefined(): B
4046
{
4147
// should be called but not autowired
4248
}
49+
50+
final public function getFinal(): A
51+
{
52+
// should not be called
53+
}
54+
55+
public function &getReference(): A
56+
{
57+
// should not be called
58+
}
4359
}

0 commit comments

Comments
 (0)