Skip to content

[DependencyInjection] ServiceLocator provides unexpected service id #61341

@m1khal3v

Description

@m1khal3v

Symfony version(s) affected

7.3.2

Description

When upgrading from 7.2 to 7.3, it was noticed that the ServiceLocator began to operate with numeric identifiers instead of string ones. At the same time, generics indicate that $id must be a string, so when casting to string we see useless cast from static analyzers.

How to reproduce

DI:

        $container->registerForAutoconfiguration(SomeInterface::class)
            ->addTag('some_tag');

        $container->setDefinition(
            id: SomeRegistry::class,
            definition: new Definition(
                class: SomeRegistry::class,
                arguments: [
                    '$services' => new ServiceLocatorArgument(new TaggedIteratorArgument('some_tag')),
                ],
            ),
        );

Registry:

    // SomeInterface extends \Stringable

    /** @var array<string, string> */
    private array $serviceMap;

    /**
     * @param ServiceCollectionInterface<SomeInterface> $services
     */
    public function __construct(private ServiceCollectionInterface $services)
    {
        foreach ($this->services->getIterator() as $id => $service) {
            $this->serviceMap[(string) $service] = $id;
        }
    }

$serviceMap contains

7.2:

A => Service\A 
B => Service\B

7.3:

A => 0
B => 1

Possible Solution

Problem may be here:
https://github.com/symfony/symfony/pull/60691/files#diff-2a36924e0448b459f1261198e19953b5a8f143a591a7298c18f0bb47ba04b6bbR91
https://github.com/symfony/symfony/pull/60691/files#diff-2a36924e0448b459f1261198e19953b5a8f143a591a7298c18f0bb47ba04b6bbR115
https://github.com/symfony/symfony/pull/60691/files#diff-2a36924e0448b459f1261198e19953b5a8f143a591a7298c18f0bb47ba04b6bbL113

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy