Do not resolve ServiceLocator get()/has() against the global container#503
Draft
janedbal wants to merge 1 commit into
Draft
Do not resolve ServiceLocator get()/has() against the global container#503janedbal wants to merge 1 commit into
janedbal wants to merge 1 commit into
Conversation
The ServiceDynamicReturnTypeExtension and ContainerInterfaceUnknownServiceRule
are registered for Psr\Container\ContainerInterface, which also matches an
injected Symfony\Component\DependencyInjection\ServiceLocator (e.g. from
#[AutowireLocator]). Their keys are local index keys (tag index attributes),
not global container service ids, but both looked the key up in the global
ServiceMap.
For a locator that meant has('key') collapsed to a constant `false` (the key is
not a global service id), so a `if (!$locator->has('key')) { return; }` guard
became "always true" and the rest of the method turned unreachable. get('key')
was also wrongly flagged with symfonyContainer.serviceNotFound.
Skip ServiceLocator receivers in both places, leaving get()/has() to native
(generic) inference. The global container, controllers and Psr-typed
ServiceSubscriber locators are unaffected (ServiceLocator is not their type).
Reported via shipmonk-rnd/dead-code-detector#376, where the resulting
unreachable code caused false unused-method reports.
Co-Authored-By: Claude Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ServiceDynamicReturnTypeExtensionandContainerInterfaceUnknownServiceRuleare registered forPsr\Container\ContainerInterface, which also matches an injectedSymfony\Component\DependencyInjection\ServiceLocator(e.g. from#[AutowireLocator]). A locator's keys are local index keys (tag index attributes), not global container service ids — but both consulted the globalServiceMap.Consequences for a
ServiceLocator:has('key')collapsed to a constantfalse(the key isn't a global service id), soif (!$locator->has('key')) { return; }became "always true" and the rest of the method unreachable;get('key')was wrongly reported withsymfonyContainer.serviceNotFound.This PR skips
ServiceLocatorreceivers in both places, leavingget()/has()to native (generic) inference. The global container, controllers, and Psr-typedServiceSubscriberlocators are unaffected (aServiceLocatoris not their static type; the existingtestGetPrivateServiceInLegacyServiceSubscriberstill passes).Discovered via shipmonk-rnd/dead-code-detector#376 — the unreachable code led a dead-code detector to report the handler methods behind the locator as unused. Repro & analysis: shipmonk-rnd/dead-code-detector#376 (comment)