# Symfony Plugin Types

**Types** features improve type inference and variable resolution across Twig templates, controller render calls, includes, embeds, and annotations.

---

## Twig @var type resolving

**Feature ID:** `TwigVarTypeResolving`  
**Date:** 2014-10-26  
**Feature Page:** [Twig @var type resolving](https://espend.de/phpstorm/plugin/symfony#twig-var-type-resolving)  

Resolves Twig type comments into PHP types so template variables can use class-aware completion, navigation, inspections, and member lookup.

Supported formats include variable-first declarations, class-first declarations, array types, multi-line comments, and union types.

### Code Examples

```twig
# Variable-first and class-first declarations:
{# @var user \App\Entity\User #}
{# @var \App\Entity\Product product #}
{# @var items \App\Entity\Item[] #}
```

```twig
# Multi-line declarations:
{#
    @var foo \App\Foo
    @var bar \App\Bar
#}
```

```twig
# Union types:
{# @var item \App\Entity\User|\App\Entity\Admin #}
{{ item.email }}
{{ item.displayName }}
```

---

## Controller render variables for Twig types

**Feature ID:** `TwigControllerRenderVariableTypes`  
**Date:** 2019-08-21  
**Feature Page:** [Controller render variables for Twig types](https://espend.de/phpstorm/plugin/symfony#twig-controller-render-variable-types)  

Collects variables passed from Symfony controllers and render-like calls so Twig templates get typed root variables for completion, navigation, and inspections.

Supported calls include `render()`, `renderView()`, `stream()`, `renderBlock()`, `renderBlockView()`, and `Template` attribute or annotation return arrays.

Supported parameter sources include inline arrays, local array variables, returned arrays, spread array entries, `array_merge()`, `array_merge_recursive()`, `array_replace()`, `array_push()`, array union, and array self-assignment.

### Code Examples

```php
# Controller render context:
final class ProductController
{
    public function show(Product $product): Response
    {
        return $this->render('product/show.html.twig', [
            'product' => $product,
            ...$this->sidebarContext(),
        ]);
    }
}
```

```php
# Render-like methods:
$this->render('product/show.html.twig', ['product' => $product]);
$this->renderView('product/show.html.twig', ['product' => $product]);
$this->stream('product/show.html.twig', ['product' => $product]);
$this->renderBlock('product/show.html.twig', 'content', ['product' => $product]);
$this->renderBlockView('product/show.html.twig', 'content', ['product' => $product]);
```

```php
# Parameter source formats:
$context = ['product' => $product];
$context += ['reviews' => $reviews];

return $this->render('product/show.html.twig', array_merge(
    $context,
    ['sidebar' => $sidebar],
    $this->extraTemplateData(),
));
```

```php
# Template attribute return arrays:
#[Template('product/show.html.twig')]
public function show(Product $product): array
{
    return ['product' => $product];
}
```

```twig
# Resolved Twig variables:
{{ product.name }}
{{ sidebar.items|length }}
```

---

## Twig include and embed context type resolution

**Feature ID:** `TwigIncludeContextParser`  
**Date:** 2026-05-10  
**Feature Page:** [Twig include and embed context type resolution](https://espend.de/phpstorm/plugin/symfony#twig-include-context-parser)  

Resolves Twig template variables from `include` and `embed` context hashes so included templates receive the correct root variables for completion, navigation, and inspections.

Context handling supports explicit keys, inherited parent variables, `only` isolation, `with_context: false`, and array or ternary template targets.

### Code Examples

```twig
# Include and embed context keys:
{% include 'product/_card.html.twig' with {
    product: product,
    currentView: 'grid'
} only %}

{% embed 'product/_panel.html.twig' with { product: product } %}
    {% block body %}{{ product.name }}{% endblock %}
{% endembed %}
```

---

## Twig loop context type resolution

**Feature ID:** `TwigLoopContextTypeResolution`  
**Date:** 2026-05-18  
**Feature Page:** [Twig loop context type resolution](https://espend.de/phpstorm/plugin/symfony#twig-loop-context-type-resolution)  

Resolves loop variables and the surrounding `loop` scope through included templates, so repeated row templates keep type-aware completion and inspections.

This also preserves parent context inheritance when a template is included from inside a `for` block.

### Code Examples

```twig
# Loop context inheritance:
{% for product in category.products %}
    {% include 'product/_row.html.twig' with { product: product } %}
{% endfor %}

{# product/_row.html.twig #}
{{ loop.index }} {{ product.name }}
```

---

## Twig @var annotation highlighting

**Feature ID:** `TwigVarCommentAnnotator`  
**Date:** 2026-02-25  
**Feature Page:** [Twig @var annotation highlighting](https://espend.de/phpstorm/plugin/symfony#twig-var-comment-annotator)  

Highlights Twig `@var` doc comment annotations so the keyword, variable name, class names, array suffixes, and union type separators are easier to read inside templates.

The highlighting supports variable-first declarations, class-first declarations, array types, multi-line comments, and union types.

### Code Examples

```twig
# Variable-first and class-first declarations:
{# @var user \App\Entity\User #}
{# @var \App\Entity\Product product #}
{# @var items \App\Entity\Item[] #}

{# Multiple declarations:
   @var foo \App\Foo
   @var bar \App\Bar
#}
```

```twig
# Union type declarations:
{# @var item \App\Entity\User|\App\Entity\Admin #}
{# @var \App\Dto\SearchResult[] results #}
```

---

