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.
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.
prophesize(SomeClass::class): The reveal() result is typed as SomeClass, so PHPStan understands all inherited methods and properties.willImplement(): When combining prophesize()->willImplement(SomeInterface::class), reveal() yields an instance that is SomeInterface, allowing interface-specific analysis.willExtend(): willExtend(BaseClass::class) tells PHPStan the prophecy extends BaseClass, even if no class is directly provided.->method(...)->willReturn(...) on an ObjectProphecy is analyzed correctly — PHPStan validates method names against the prophesized class/interface, catching typos and invalid signatures.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.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().final, private, or final methods, PHPStan may warn on predictions — this is expected Prophecy behavior, not a bug.extension.neon lived at src/extension.neon; ensure your manual include path matches current layout (vendor/jangregor/phpstan-prophecy/extension.neon).testbench or orchestra/testbench isn’t overriding PHPStan config — explicitly include the extension if static analysis behaves oddly in feature tests.How can I help you explore Laravel packages today?