Skip to content

Type Symfony container get() results from the compiled container's id → class map #694

Open

Description

@blacktrs

Problem

$container->get(Foo::class) in Symfony apps returns an untyped object. Every call on the result goes through dynamic dispatch. symfony-app has many of these on the request path.

The docblock can't help: ContainerInterface::get declares @return ($id is class-string<C> ? C|object : …). Read faithfully, C|object is object. The template binding from #686 / PR #691 correctly refuses to narrow it. Narrowing it to C would throw TypeError where php accepts any object.

Idea

The compiled container (var/cache/*/Container*.php) knows every service: the id, the factory method and the concrete class it builds. In a full-program build that map is static.

Use it at the call site. When the receiver is the compiled container class (or its interface), the id is a constant string that maps to class X, and the service isn't synthetic or replaced at runtime, type the result obj<X>. Keep the same instanceof + TypeError guard as the template rewrite, so a wrong map can only fail loudly, never silently.

Acceptance

  • symfony-app: get(<const id>) sites that resolve through the map are rewritten. Report the site count, IR lines and binary size against the base.
  • symfony-demo pages stay byte-identical to php.
  • No behaviour change for unknown ids, dynamic ids or set()-replaced services.

Context

Follow-up to #686 (PR #691, P4 of the semantic-core plan).

Activity

  1. added
    enhancement
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions