# Mago for Symfony

`friendsofmago/mago-symfony` adds Symfony static analysis to [Mago](<https://github.com/carthage-software/mago>). It resolves types and reports Symfony-specific mistakes across five areas:

- **Container services and parameters.** Resolves `ContainerInterface::get()`, `Container::get()`, and parameter lookups from compiled container XML or a JSON map. Reports private service lookups and optional unknown service IDs. See [Container](<https://friendsofmago.github.io/mago-symfony/analyzer/container/index.md>).
- **Console commands.** Types default helpers from `HelperSet::get()` and `Command::getHelper()`. Checks `configure()` definitions and literal `getArgument()` and `getOption()` calls. See [Console](<https://friendsofmago.github.io/mago-symfony/analyzer/console/index.md>).
- **Config TreeBuilder chains.** Tracks parent and child node types across `TreeBuilder::getRootNode()`, `children()`, prototypes, `validate()`, `beforeNormalization()`, and `end()`. Reports invalid method calls on non-array roots. See [Config TreeBuilder](<https://friendsofmago.github.io/mago-symfony/analyzer/config-tree-builder/index.md>).
- **HTTP, Form, Serializer, Messenger, and Security APIs.** Narrows return types for `HeaderBag`, `ParameterBag`, `InputBag`, `Request`, `ResponseHeaderBag`, `ControllerEvent`, `FormInterface`, `SerializerInterface`, `DenormalizerInterface`, `WrappedExceptionsInterface`, and Security `getUser()`. See [HTTP, Form, Serializer, and Security](<https://friendsofmago.github.io/mago-symfony/analyzer/http-and-serializer/index.md>).
- **Lifecycle initializers and entry points.** Recognizes Symfony lifecycle initializers, public `#[Required]` properties, and framework method entry points. See [Lifecycle and entry points](<https://friendsofmago.github.io/mago-symfony/analyzer/di-attributes/index.md>).

## Quick start

You need PHP 8.1 or newer, Mago `^1.60.2`, and Symfony `^6.4 || ^7.0`.

Install the package with Composer:

```
composer require --dev friendsofmago/mago-symfony
```

Add the worker and Symfony vendor includes to `mago.toml`:

```
[source]
includes = ["vendor/symfony", "vendor/psr"]

[extension-hosts.symfony]
command = ["php", "-d", "xdebug.mode=off", "-d", "pcov.enabled=0", "-d", "memory_limit=512M", "vendor/friendsofmago/mago-symfony/resources/worker.php"]
workers = 1
```

Validate the extension host and run the analyzer:

```
vendor/bin/mago extension validate
vendor/bin/mago analyze
```

## Example diagnostic

Mago reports console typos before the command runs:

```
$this->addArgument('username', InputArgument::REQUIRED);
// ...
$input->getArgument('user_name'); // symfony/unknown-console-argument
```

## Next steps

- Read [Install](<https://friendsofmago.github.io/mago-symfony/setup/install/index.md>) for prerequisites and project layout options.
- Read [Configuration](<https://friendsofmago.github.io/mago-symfony/setup/configuration/index.md>) for container XML, JSON map, Security user, and suppression settings.
- Read [The worker](<https://friendsofmago.github.io/mago-symfony/setup/worker/index.md>) for worker entry points, memory flags, and error output.
- Read [Composing extensions](<https://friendsofmago.github.io/mago-symfony/setup/composition/index.md>) to run `friendsofmago/mago-symfony` alongside other Mago extensions in one worker.
- Browse [Diagnostics](<https://friendsofmago.github.io/mago-symfony/analyzer/diagnostics/index.md>) for every error code the extension reports.
