Skip to content

[DependencyInjection] Target Attribute must fail if the target does not exist #48707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public function __construct(string $name)
$this->name = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name))));
}

public static function parseName(\ReflectionParameter $parameter): string
public static function parseName(\ReflectionParameter $parameter, self &$attribute = null): string
{
$attribute = null;
if (!$target = $parameter->getAttributes(self::class)[0] ?? null) {
return $parameter->name;
}

$name = $target->newInstance()->name;
$attribute = $target->newInstance();
$name = $attribute->name;

if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $name)) {
if (($function = $parameter->getDeclaringFunction()) instanceof \ReflectionMethod) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add `RemoveBuildParametersPass`, which removes parameters starting with a dot during compilation
* Add support for nesting autowiring-related attributes into `#[Autowire(...)]`
* Deprecate undefined and numeric keys with `service_locator` config
* Fail if Target attribute does not exist during compilation

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function doProcessValue(mixed $value, bool $isRoot = false): mixed
return $this->processAttribute($attribute, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $value->getInvalidBehavior());
}

$value = new TypedReference($value->getType(), $value->getType(), $value->getInvalidBehavior(), $attribute->name);
$value = new TypedReference($value->getType(), $value->getType(), $value->getInvalidBehavior(), $attribute->name, [$attribute]);
}
if ($ref = $this->getAutowiredReference($value, true)) {
return $ref;
Expand Down Expand Up @@ -332,7 +332,8 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
}

$getValue = function () use ($type, $parameter, $class, $method) {
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, Target::parseName($parameter)), false)) {
$name = Target::parseName($parameter, $target);
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, $name, $target ? [$target] : []), false)) {
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));

if ($parameter->isDefaultValueAvailable()) {
Expand Down Expand Up @@ -420,6 +421,10 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
}
}
}

if ($reference->getAttributes()) {
return null;
}
}

if ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract()) {
Expand Down Expand Up @@ -544,6 +549,9 @@ private function createTypeNotFoundMessage(TypedReference $reference, string $la
}

$message = sprintf('has type "%s" but this class %s.', $type, $parentMsg ? sprintf('is missing a parent class (%s)', $parentMsg) : 'was not found');
} elseif ($reference->getAttributes()) {
$message = $label;
$label = sprintf('"#[Target(\'%s\')" on', $reference->getName());
} else {
$alternatives = $this->createTypeAlternatives($this->container, $reference);
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,21 @@ public function testArgumentWithTarget()
$this->assertSame(BarInterface::class.' $imageStorage', (string) $container->getDefinition('with_target')->getArgument(0));
}

public function testArgumentWithTypoTarget()
{
$container = new ContainerBuilder();

$container->register(BarInterface::class, BarInterface::class);
$container->register(BarInterface::class.' $iamgeStorage', BarInterface::class);
$container->register('with_target', WithTarget::class)
->setAutowired(true);

$this->expectException(AutowiringFailedException::class);
$this->expectExceptionMessage('Cannot autowire service "with_target": "#[Target(\'imageStorage\')" on argument "$bar" of method "Symfony\Component\DependencyInjection\Tests\Fixtures\WithTarget::__construct()"');

(new AutowirePass())->process($container);
}

public function testDecorationWithServiceAndAliasedInterface()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public static function getSubscribedServices(): array
'autowired.nullable' => new ServiceClosureArgument(new Reference('service.id', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'autowired.parameter' => new ServiceClosureArgument('foobar'),
'map.decorated' => new ServiceClosureArgument(new Reference('.service_locator.oZHAdom.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'someTarget')),
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'someTarget', [new Target('someTarget')])),
];
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
}
Expand Down