# Config TreeBuilder

`TreeBuilderRootTypePlugin` uses plugin identifier `symfony-tree-builder-root`. It types Symfony Config `TreeBuilder` root nodes and fluent node builder chains, filters false-positive Symfony 7 return mismatches, and reports invalid method calls on non-array roots.

## Root node typing

`Symfony\Component\Config\Definition\Builder\TreeBuilder::getRootNode()` declares `NodeDefinition|ArrayNodeDefinition`. Without this plugin, Mago cannot call `children()` on the union.

During `BeforeAnalysisHook`, `TreeBuilderRootNodeIndex` scans source files with PHP's tokenizer to record the constructed root type of each `TreeBuilder::getRootNode()` span. `RootNodeReturnTypeProvider` then returns the matching concrete `*NodeDefinition<TreeBuilder<...>>` class:

| Root type argument | Node definition class |
| --- | --- |
| `'array'` or omitted | `ArrayNodeDefinition` |
| `'variable'` | `VariableNodeDefinition` |
| `'scalar'` | `ScalarNodeDefinition` |
| `'string'` | `StringNodeDefinition` |
| `'boolean'` | `BooleanNodeDefinition` |
| `'integer'` | `IntegerNodeDefinition` |
| `'float'` | `FloatNodeDefinition` |
| `'enum'` | `EnumNodeDefinition` |

When a `TreeBuilder` variable is dynamic, conditionally reassigned to different root types, or ambiguous across colliding spans, the provider falls back instead of guessing.

## Fluent node chains and end()

`ConfigEndReturnTypeProvider` tracks the parent type across fluent builder chains:

- `ArrayNodeDefinition::children()` returns `NodeBuilder` with the `ArrayNodeDefinition` as its parent.
- `ArrayNodeDefinition::prototype($type)` and its typed shortcuts return the concrete `*NodeDefinition` with the array node as parent. Supported shortcuts are `arrayPrototype()`, `scalarPrototype()`, `stringPrototype()`, `booleanPrototype()`, `integerPrototype()`, `floatPrototype()`, `enumPrototype()`, and `variablePrototype()`.
- `NodeBuilder::node()` and its typed methods return the concrete `*NodeDefinition` with the builder as parent. Supported methods are `arrayNode()`, `scalarNode()`, `stringNode()`, `booleanNode()`, `integerNode()`, `floatNode()`, `enumNode()`, and `variableNode()`.
- `NodeDefinition::validate()` and `NodeDefinition::beforeNormalization()` return `ExprBuilder` with the node as parent.
- `end()` on `NodeDefinition`, `NodeBuilder`, `ExprBuilder`, and `MergeBuilder` pops the tracked parent type, or returns `null` past an unparented root node.
- The provider tracks custom `NodeBuilder` subclasses through `setNodeClass()` and `setParent()`.

## Return statement filtering

`TreeBuilderReturnIssueFilter` suppresses two Symfony 7 generic mismatches only when statically proven safe:

- `TreeBuilder<string>` vs `TreeBuilder<'array'>` mismatches when a matching `TreeBuilder` root type dominates every reachable path to the `return` statement.
- `ArrayNodeDefinition<TreeBuilder<...>>` vs `ArrayNodeDefinition<null>` mismatches when the enclosing function has an unparameterized native or `@return` concrete root-node declaration.

Explicit generic mismatches remain reported, such as returning `TreeBuilder<'scalar'>` from a method annotated `@return TreeBuilder<'array'>`. Inherited generic mismatches also remain reported.

## TreeBuilder diagnostic

| Code | Level | Default | Summary |
| --- | --- | --- | --- |
| [`symfony-tree-builder-root/invalid-root-node-method`](<https://friendsofmago.github.io/mago-symfony/analyzer/diagnostics/index.md#symfony-tree-builder-rootinvalid-root-node-method>) | error | On | Reports a method call on `TreeBuilder::getRootNode()` that does not exist on the concrete non-array root node class. |
