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 Psr Container Laravel Package

bnf/phpstan-psr-container

PHPStan dynamic return type extension for PSR-11 ContainerInterface. Improves type inference for $container->get() so services are typed correctly during static analysis. Install via Composer and include extension.neon (or use phpstan/extension-installer).

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer: composer require --dev bnf/phpstan-psr-container. For seamless integration, use the phpstan/extension-installer (recommended for PHPStan 2.x+). If using manual setup, enable it in your phpstan.neon:

includes:
    - vendor/bnf/phpstan-psr-container/extension.neon

No further configuration is needed for basic use. Start with running phpstan analyze—this extension now supports PHPStan 2.x (previously 1.x only). If your code uses ContainerInterface::get() or has() (e.g., Symfony, Zend, or custom PSR-11 implementations), PHPStan will infer return types more accurately. For example, get(MyService::class) will resolve to MyService instead of mixed, reducing “Method X called on mixed” errors.


Implementation Patterns

  • Tag-based autoconfiguration: Combine with docblock hints (/** @return MyClass */) on factory methods to improve inferred types for dynamically resolved services, especially in Symfony-like containers.
  • Custom Container implementations: Extend your PSR-11 container with @method annotations or PHPDoc @return in get()/has() methods. The extension consumes these to refine type inference—critical for non-standard containers.
  • Dynamic service resolution: In factories or services relying on __invoke + container, use string-literal service IDs (e.g., MyService::class) or ::class constants for static analysis. Avoid variables (e.g., get($dynamicId)) unless annotated with @phpstan-param string $id.
  • Gradual adoption: Enable only specific rules in phpstan.neon initially (e.g., level: 5), then incrementally increase strictness. Use --exclude-analyse for legacy code until refactored.
  • PHPStan 2.x compatibility: Leverage new PHPStan 2.x features like better autoloading and improved extension discovery via extension-installer. Example phpstan.neon for auto-install:
    includes:
        - vendor/phpstan/phpstan/src/Rules/*.neon
        - vendor/phpstan/phpstan-extension-installer/extension.neon
    

Gotchas and Tips

  • PHPStan 1.x deprecation: This release drops PHPStan 1.x support. Ensure your project uses PHPStan 2.x (composer require phpstan/phpstan:^2). Downgrade the extension to 1.0.1 if stuck on PHPStan 1.x.
  • Runtime vs. static IDs: Dynamic service IDs (e.g., get($variable)) still fall back to mixed. Mitigate with:
    • @phpstan-param string $id on container methods.
    • @phpstan-assert for runtime-validated IDs.
  • ContainerInterface mismatch: If your container deviates from PSR-11 (e.g., missing type hints in get()), add a custom stub in phpstan.neon:
    parameters:
        container:
            getReturnTypeExpr: 'string($id) ? MyService::class : mixed'
    
  • Extension conflicts: Disable other extensions (e.g., laravel-console-emulator) temporarily to isolate behavior. PHPStan 2.x’s improved extension system may resolve some conflicts automatically.
  • Missing types: For services defined in YAML/XML (e.g., Symfony DI), use:
    /** @phpstan-type ServiceAlias string */
    const ServiceAlias = 'app.my_service';
    
    Or import types via @phpstan-import-type.
  • Unreachable services: The extension now better handles PHPStan 2.x’s stricter analysis. False positives in DI bootstraps can be silenced with:
    // In a factory or service:
    /** @phpstan-ignore-next-line */
    $container->has('unreachable_service');
    
    Or use @phpstan-assert for conditional checks.
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