Repository: symfony/symfony
File: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ConsoleArgumentValueResolverPass.php (PHP)

Code before:
 *
 * @author Robin Chalas <robin.chalas@gmail.com>
 */
class ConsoleArgumentValueResolverPass implements CompilerPassInterface
{
    use PriorityTaggedServiceTrait;

    public function process(ContainerBuilder $container): void
    {
        if (!$container->hasDefinition('console.argument_resolver')) {
            return;
        }

        $definitions = $container->getDefinitions();
        $namedResolvers = $this->findAndSortTaggedServices(new TaggedIteratorArgument('console.targeted_value_resolver', 'name', needsIndexes: true), $container);
        $resolvers = $this->findAndSortTaggedServices(new TaggedIteratorArgument('console.argument_value_resolver', 'name', needsIndexes: true), $container);

        foreach ($resolvers as $name => $resolver) {
            if ($definitions[(string) $resolver]->hasTag('console.targeted_value_resolver')) {
                unset($resolvers[$name]);
            } else {
                $namedResolvers[$name] ??= clone $resolver;
            }
        }

        if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class) && $container->has('debug.stopwatch')) {
            foreach ($resolvers as $name => $resolver) {
                $resolvers[$name] = new Reference('.debug.console.value_resolver.'.$resolver);
                $container->register('.debug.console.value_resolver.'.$resolver, TraceableValueResolver::class)
                    ->setArguments([$resolver, new Reference('debug.stopwatch')]);
            }
            foreach ($namedResolvers as $name => $resolver) {
                $namedResolvers[$name] = new Reference('.debug.console.value_resolver.'.$resolver);
                $container->register('.debug.console.value_resolver.'.$resolver, TraceableValueResolver::class)
                    ->setArguments([$resolver, new Reference('debug.stopwatch')]);
            }
        }

        $container
            ->getDefinition('console.argument_resolver')
            ->replaceArgument(0, new IteratorArgument(array_values($resolvers)))
            ->setArgument(1, new ServiceLocatorArgument($namedResolvers))
        ;
    }
}


Review comment (bug):
```suggestion
        if ($container->getParameter('kernel.debug') && $container->has('debug.stopwatch')) {
```

This bit can be removed as `debug.stopwatch` declaration is already conditioned by the class existence

Code after:
 * @author Robin Chalas <robin.chalas@gmail.com>
 */
class ConsoleArgumentValueResolverPass implements CompilerPassInterface
{
    use PriorityTaggedServiceTrait;

    public function process(ContainerBuilder $container): void
    {
        if (!$container->hasDefinition('console.argument_resolver')) {
            return;
        }

        $definitions = $container->getDefinitions();
        $namedResolvers = $this->findAndSortTaggedServices(new TaggedIteratorArgument('console.targeted_value_resolver', 'name', needsIndexes: true), $container);
        $resolvers = $this->findAndSortTaggedServices(new TaggedIteratorArgument('console.argument_value_resolver', 'name', needsIndexes: true), $container);

        foreach ($resolvers as $name => $resolver) {
            if ($definitions[(string) $resolver]->hasTag('console.targeted_value_resolver')) {
                unset($resolvers[$name]);
            } else {
                $namedResolvers[$name] ??= clone $resolver;
            }
        }

        if ($container->getParameter('kernel.debug') && $container->has('debug.stopwatch')) {
            foreach ($resolvers as $name => $resolver) {
                $resolvers[$name] = new Reference('.debug.console.value_resolver.'.$resolver);
                $container->register('.debug.console.value_resolver.'.$resolver, TraceableValueResolver::class)
                    ->setArguments([$resolver, new Reference('debug.stopwatch')]);
            }
            foreach ($namedResolvers as $name => $resolver) {
                $namedResolvers[$name] = new Reference('.debug.console.value_resolver.'.$resolver);
                $container->register('.debug.console.value_resolver.'.$resolver, TraceableValueResolver::class)
                    ->setArguments([$resolver, new Reference('debug.stopwatch')]);
            }
        }

        $container
            ->getDefinition('console.argument_resolver')
            ->replaceArgument(0, new IteratorArgument(array_values($resolvers)))
            ->setArgument(1, new ServiceLocatorArgument($namedResolvers))
        ;
    }
}