phpstan/phpstan-symfony
PHPStan extension for Symfony that improves static analysis with precise return types and framework-specific rules. Understands container/services, parameters, controllers, request/headers, serializer, forms, messenger handlers, cache callbacks, config tree builders, and more.
Install the package via Composer in development mode:
composer require --dev phpstan/phpstan-symfony
The package auto-registers if you use phpstan/extension-installer. Otherwise, manually include extension.neon (and optionally rules.neon) in your PHPStan config.
First-time use: Run PHPStan as usual — it will now infer more precise types for Symfony’s service container ($this->get(), $this->has()), parameter bags, request/input methods, and Messenger message handlers if a compiled container XML is available.
Start by generating the container XML in your build pipeline (e.g., bin/console debug:container --xml), or ensure it exists at var/cache/*/App_Kernel*Container.xml. Provide the correct path in your PHPStan config under parameters.symfony.containerXmlPath.
$this->get(Service::class) in controllers or services — PHPStan now knows the exact return type instead of mixed|object|null.$form->getErrors($deep, $flatten) with confidence — PHPStan infers Error[] vs IteratorAggregate|Error[] based on parameters.consoleApplicationLoader file (e.g., tests/console-application.php) to enable strict type inference for $input->getArgument('id') → int|string|null, and detect invalid command option names or defaults.messenger.handleTraitWrappers for custom buses that use HandleTrait, enabling accurate inference like $queryBus->dispatch(new GetProductQuery()) → Product.$container->hasParameter('some.string') and $container->getParameter('some.string') return correct bool and string|int|bool|float|array|null types respectively.containerXmlPath points to the current XML. Outdated XML leads to stale types (e.g., missing services, wrong types).constantHassers: false disables warnings like if ($this->has('nonexistent')), which might hide typos. Use only when dealing with optional dependencies.container_xml_path. Post-2.x requires kebab-case (containerXmlPath) — refactor old configs.config/packages/*.php, add scanDirectories and scanFiles (see README) to resolve service(), env() helpers and config classes.Cannot Declare interface PhpParser\NodeVisitor, disable inlining in a dedicated PHPStan env (container.dumper.inline_class_loader: false) and point consoleApplicationLoader to it.rules.neon: Include it explicitly to enable checks like “Try to get unregistered service” or “Accessing private service” warnings — these are not part of extension.neon.How can I help you explore Laravel packages today?