Skip to content

Container

SymfonyPlugin resolves return types for service and parameter lookups. It also validates literal service access against a compiled Symfony container XML dump.

Service return types

ContainerGetReturnTypeProvider targets three methods:

  • Symfony\Component\DependencyInjection\ContainerInterface::get()
  • Symfony\Component\DependencyInjection\Container::get()
  • Psr\Container\ContainerInterface::get()

For a literal string $id, the provider resolves the service type in this order:

  1. A <factory> method or function from the compiled container XML. The provider uses the factory's declared or inferred return type unless that type is mixed, object, void, or never.
  2. A mapped class or interface from the compiled container XML or JSON container map, following multi-hop <alias> chains, when that symbol exists in Mago's codebase.
  3. A fallback class-name lookup when the literal ID contains no . and matches the exact case of a declared class or interface in Mago's codebase.

When no container XML or JSON map is loaded, non-class string IDs such as 'logger' keep Symfony's declared ?object return type.

Invalid-reference behavior flags

When you pass the second argument $invalidBehavior to get(), the provider adjusts nullability to match Symfony's runtime behavior:

Receiver $invalidBehavior value Return type
Any container Omitted or EXCEPTION_ON_INVALID_REFERENCE (1) Service
ContainerBuilder or subclass RUNTIME_EXCEPTION_ON_INVALID_REFERENCE (0) Service
Container, ContainerInterface, ServiceLocator, ServiceProviderInterface RUNTIME_EXCEPTION_ON_INVALID_REFERENCE (0) Service\|null
Any container NULL_ON_INVALID_REFERENCE (2), IGNORE_ON_INVALID_REFERENCE (3), IGNORE_ON_UNINITIALIZED_REFERENCE (4), or a wider int Service\|null

Parameter return types

When loaded container XML or JSON metadata includes parameters, ParameterReturnTypeProvider targets four methods:

  • Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface::get()
  • Symfony\Component\DependencyInjection\ContainerInterface::getParameter()
  • Symfony\Component\DependencyInjection\Container::getParameter()
  • Symfony\Bundle\FrameworkBundle\Controller\AbstractController::getParameter()

Supported parameter shapes include null, literal bool, literal int, literal float, literal string, PHP and class constants, lists, and keyed arrays. Supported %env(...)% processors resolve to int, float, bool, string, non-empty-string, or array<int|string, mixed>.

Container checks

When a compiled container XML dump is loaded, ContainerGetAccessHook inspects throwing get() calls on ContainerInterface and Container.

Code Level Default Summary
symfony/private-service-access error On Reports a throwing get() lookup of a private service or private alias.
symfony/unknown-service-id error Opt-in Reports a throwing get() lookup of a literal ID missing from the compiled XML when MAGO_SYMFONY_STRICT_CONTAINER_SERVICES=1.

Both checks skip ContainerBuilder, ServiceLocator, ServiceProviderInterface, TestContainer, PHPUnit TestCase subclasses, and calls that pass a non-throwing $invalidBehavior flag.

See Configuration for container XML and JSON map loading rules.