Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Loader/FileLoader.php (PHP)

Code before:
                $this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
                $interfaces[] = $interface;
            }

            if (!$autoconfigureAttributes) {
                continue;
            }
            $r = $this->container->getReflectionClass($class);
            $defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
            foreach ($r->getAttributes(AsAlias::class, \ReflectionAttribute::IS_INSTANCEOF) as $attr) {
                /** @var AsAlias $attribute */
                $attribute = $attr->newInstance();
                $alias = $attribute->id ?? $defaultAlias;
                $public = $attribute->public;
                if (null === $alias) {
                    throw new LogicException(\sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class));
                }

                if (!$attribute->when || \in_array($this->env, $attribute->when, true)) {
                    if ($attribute->target) {
                        $parsedTarget = (new Target($attribute->target))->getParsedName();
                        $targetAlias = $alias.' $'.$parsedTarget;
                        if ($this->container->hasAlias($targetAlias)) {
                            $existingAlias = $this->container->getAlias($targetAlias);
                            $existingClass = $this->container->hasDefinition((string) $existingAlias) ? $this->container->getDefinition((string) $existingAlias)->getClass() : (string) $existingAlias;
                            throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $targetAlias, $existingClass));
                        }
                        $this->container->registerAliasForArgument($class, $alias, $attribute->target);
                    } else {
                        if (isset($this->aliases[$alias])) {
                            throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
                        }

                        $this->aliases[$alias] = new Alias($class, $public);
                    }
                }
            }
        }

        foreach ($this->aliases as $alias => $aliasDefinition) {
            $this->container->setAlias($alias, $aliasDefinition);
        }

        if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
            $this->registerAliasesForSinglyImplementedInterfaces();
        }
    }

    public function registerAliasesForSinglyImplementedInterfaces(): void
    {
        foreach ($this->interfaces as $interface) {

Review comment (refactor):
checking `$this->container->hasAlias` means you don't only detect duplicate `#[AsAlias]` attribute (which is what your error message mentions). It also prevents overriding any existing container alias. This is inconsistent with the behavior for non-targeted aliases.

Code after:
            foreach (class_implements($class, false) as $interface) {
                $this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
                $interfaces[] = $interface;
            }

            if (!$autoconfigureAttributes) {
                continue;
            }
            $r = $this->container->getReflectionClass($class);
            $defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
            foreach ($r->getAttributes(AsAlias::class, \ReflectionAttribute::IS_INSTANCEOF) as $attr) {
                /** @var AsAlias $attribute */
                $attribute = $attr->newInstance();
                $alias = $attribute->id ?? $defaultAlias;
                $public = $attribute->public;
                if (null === $alias) {
                    throw new LogicException(\sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class));
                }

                if ($attribute->when && !\in_array($this->env, $attribute->when, true)) {
                    continue;
                }
                if (!$attribute->target) {
                    if (isset($this->aliases[$alias])) {
                        throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
                    }

                    $this->aliases[$alias] = new Alias($class, $public);
                    continue;
                }
                if ($public) {
                    throw new LogicException(\sprintf('#[AsAlias] attributes with a target cannot be public in "%s".', $class));
                }
                $this->container->registerAliasForArgument($class, $alias, $attribute->target);
                $alias = array_key_last($this->container->getAliases());
                if (isset($this->aliasedTargets[$alias])) {
                    throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $class));
                }
                $this->aliasedTargets[$alias] = $class;
            }
        }

        foreach ($this->aliases as $alias => $aliasDefinition) {
            $this->container->setAlias($alias, $aliasDefinition);
        }

        if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
            $this->registerAliasesForSinglyImplementedInterfaces();
        }
    }