Installation:
composer require --dev eloquent/phpstan-phony
Ensure phpstan/extension-installer is also installed (recommended for auto-configuration).
Configuration:
extension-installer, no manual config is needed.phpstan.neon:
includes:
- vendor/eloquent/phpstan-phony/phony.neon
First Use Case: Run PHPStan on a test file using Phony mocks:
vendor/bin/phpstan analyse tests/Unit/ExampleTest.php
Verify type hints for mocks (e.g., mock(ClassA::class)->methodA) are recognized.
Static Analysis for Mocks: Use the package to validate mock chains in test files:
$mock = mock(MyService::class);
$mock->expectedMethod()->returns('value'); // PHPStan validates chain
Multi-Type Mocks: Leverage support for arrays of classes:
$mock = mock([UserRepository::class, Cache::class]);
$mock->method()->returns(...); // Validated for both types
Mock Builders:
Integrate with mockBuilder for dynamic mocks:
$builder = mockBuilder(User::class);
$builder->partialWith(['id' => 1])->get(); // Type-checked
Static Method Mocking: Validate static method calls on mocks:
onStatic(mock(Logger::class))->log('test'); // Validated
# .github/workflows/test.yml
- name: PHPStan
run: vendor/bin/phpstan analyse --level=max tests/
phony.neon for project-specific mock behaviors (e.g., custom return types).Unsupported PHPStan Versions:
1.x. Ensure compatibility:
# phpstan.neon
phpstan:
version: ^1.0
0.12.x) may require downgrading PHPStan or using a fork.Broken Release:
0.2.0 is explicitly marked as broken. Avoid using it.PHP Version:
0.7.0).Partial Mocks:
partialWith() may not infer return types for complex objects. Use @var annotations:
/** @var User $user */
$user = mockBuilder(User::class)->partialWith(['id' => 1])->get();
False Positives: If PHPStan flags mock methods as undefined, verify:
methodA vs method_a).Extension Conflicts: Disable other Phony-related extensions temporarily to isolate issues:
# phpstan.neon
extensions:
- PhpStanExtensions\SomeOtherExtension\extension.neon
# - PhpStanExtensions\PhonyExtension\extension.neon # Disable if conflicting
Combine with Other Extensions:
Use alongside phpstan/phpstan-doctrine or phpstan/phpstan-symfony for full-stack type safety:
includes:
- vendor/eloquent/phpstan-phony/phony.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
Custom Mock Types:
Extend phony.neon to add support for custom mock behaviors:
services:
- Eloquent\PhonyPhpStan\PhonyExtension
parameters:
phony:
customMocks:
App\Services\CustomMock: App\Tests\Mocks\CustomMockBuilder
Performance: Exclude slow-to-analyze mock-heavy files from strict checks:
# phpstan.neon
excludeFiles:
- tests/Integration/SlowMockTest.php
Legacy Code: For older PHP/Phony versions, use a fork or downgrade PHPStan to a supported version:
composer require phpstan/phpstan:0.12.99
How can I help you explore Laravel packages today?