phpstan/phpstan-mockery
PHPStan extension for Mockery: infers intersection types for mocks (Foo&MockInterface), understands shouldReceive/shouldHaveReceived/allows/expects, supports partial/alias/overload mocks, multiple interfaces, and proper constructor args for accurate static analysis.
Install the package as a development dependency:
composer require --dev phpstan/phpstan-mockery
If you use phpstan/extension-installer, it’s auto-configured. Otherwise, include extension.neon manually in your PHPStan config. Your first use case: run PHPStan on a test file using Mockery — it will now understand mock types and method expectations correctly. For example, mockery::mock(Foo::class)->shouldReceive('bar') will no longer trigger “Method bar() does not exist” errors.
Mockery::mock() and spy() as usual — the extension ensures PHPStan treats them as Foo&MockInterface, enabling autocompletion and strict type-checking on both the real contract and the mock API.Foo|\Mockery\MockInterface (e.g., in @param PHPDoc). By default, the extension converts this union to an intersection (Foo&MockInterface), which gives precise type inference without losing mock-specific methods.->shouldReceive('foo')->andReturn('bar') — PHPStan recognizes these fluent methods as returning static, preserving type integrity across calls.mockery::mock(Foo::class . '[bar]') or use shouldAllowMockingProtectedMethods() — both are typed and inferred correctly.alias: or overload: prefixes in Mockery::mock(), PHPStan still infers the correct underlying type for safety checks.mockery.convertUnionToIntersectionType: false in your config. Misconfigured, this may cause over-permissive type inference.shouldReceive() et al. only on mocks: calling them on non-mock objects (e.g., real instances or incomplete mocks) won’t be caught at static analysis time unless you annotate parameter types explicitly — rely on strict type hints in constructor/type declarations to prevent misuse.Foo[bar,baz], PHPStan may not distinguish between partial and full mocks in some scenarios — use strict @phpstan-assert MockInterface annotations for extra safety in test assertions.--debug to see how types are interpreted — you’ll see intersection types like App\Entity\User&Mockery\MockInterface which helps verify correct behavior.How can I help you explore Laravel packages today?