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 Prophecy Laravel Package

jangregor/phpstan-prophecy

PHPStan extension for phpspec/prophecy that improves type inference in tests. It teaches PHPStan the real types returned from Prophecy workflows like prophesize(), willExtend(), and reveal(), so your revealed doubles are understood as the mocked class.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package as a dev dependency:

composer require --dev jangregor/phpstan-prophecy

If you use phpstan/extension-installer, it auto-configures the extension. Otherwise, add this line to your phpstan.neon:

includes:
    - vendor/jangregor/phpstan-prophecy/extension.neon

The core benefit is type inference for Prophecy test doubles — after calling reveal(), PHPStan now correctly infers the object’s real class/interface type, enabling accurate static analysis of method calls, return types, and type checks.

Implementation Patterns

  • Class prophesies with prophesize(SomeClass::class): The reveal() result is typed as SomeClass, so PHPStan understands all inherited methods and properties.
  • Interface prophesies via willImplement(): When combining prophesize()->willImplement(SomeInterface::class), reveal() yields an instance that is SomeInterface, allowing interface-specific analysis.
  • Inheritance prophesies via willExtend(): willExtend(BaseClass::class) tells PHPStan the prophecy extends BaseClass, even if no class is directly provided.
  • Method predictions: Calling ->method(...)->willReturn(...) on an ObjectProphecy is analyzed correctly — PHPStan validates method names against the prophesized class/interface, catching typos and invalid signatures.
  • Integration in setUp() or helpers: Prophecy creation in shared test setup (e.g., setUp()) works as expected — PHPStan still resolves types for variables used across test methods.

Gotchas and Tips

  • No argument validation yet: PHPStan does not validate预言 arguments (e.g., Argument::is(), Argument::type()) — only method existence/signature on the prophecy object.
  • prophesize() without class/interface: Using prophesize() alone (no willExtend()/willImplement()) yields a generic ObjectProphecy with no useful type — avoid unless combined with explicit willExtend()/willImplement().
  • Internal classes may break inference: If the prophesized class uses final, private, or final methods, PHPStan may warn on predictions — this is expected Prophecy behavior, not a bug.
  • Extension path changes matter: Prior to v0.7.0, extension.neon lived at src/extension.neon; ensure your manual include path matches current layout (vendor/jangregor/phpstan-prophecy/extension.neon).
  • Laravel tests: In Pest or PHPUnit tests, ensure testbench or orchestra/testbench isn’t overriding PHPStan config — explicitly include the extension if static analysis behaves oddly in feature tests.
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