Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpstan Symfony Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Type-safe service access: Use $this->get(Service::class) in controllers or services — PHPStan now knows the exact return type instead of mixed|object|null.
  • Form error inspection: Access $form->getErrors($deep, $flatten) with confidence — PHPStan infers Error[] vs IteratorAggregate|Error[] based on parameters.
  • Console command validation: Provide a 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 query bus typing: Configure messenger.handleTraitWrappers for custom buses that use HandleTrait, enabling accurate inference like $queryBus->dispatch(new GetProductQuery())Product.
  • Parameter safety: Ensure $container->hasParameter('some.string') and $container->getParameter('some.string') return correct bool and string|int|bool|float|array|null types respectively.

Gotchas and Tips

  • Container path must be accurate: If your app uses custom cache paths (e.g., Docker volumes), ensure containerXmlPath points to the current XML. Outdated XML leads to stale types (e.g., missing services, wrong types).
  • Constant hassers may mask bugs: Setting constantHassers: false disables warnings like if ($this->has('nonexistent')), which might hide typos. Use only when dealing with optional dependencies.
  • BC break in config naming: Pre-2.x allowed keys like container_xml_path. Post-2.x requires kebab-case (containerXmlPath) — refactor old configs.
  • PHP config files need extra scanning: For Symfony 5.3+, if you define services in config/packages/*.php, add scanDirectories and scanFiles (see README) to resolve service(), env() helpers and config classes.
  • Console env quirk: If PHPStan crashes with Cannot Declare interface PhpParser\NodeVisitor, disable inlining in a dedicated PHPStan env (container.dumper.inline_class_loader: false) and point consoleApplicationLoader to it.
  • Missing service detection requires 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport