This file provides guidance for AI assistants working with the phpstan/phpstan-phpunit repository.
This is a PHPStan extension that provides advanced static analysis support for PHPUnit test suites. It offers:
- Type extensions: Correct return types for
createMock(),getMockForAbstractClass(),getMockFromWsdl(),MockBuilder::getMock(), etc., returning intersection types (e.g.,MockObject&Foo) so both mock and original class methods are available. - PHPDoc interpretation: Converts
Foo|MockObjectunion types in phpDocs to intersection types. - Assert type narrowing: Specifies types of expressions passed to
assertInstanceOf,assertTrue,assertInternalType, etc. - Early terminating methods: Defines
fail(),markTestIncomplete(),markTestSkipped()as early terminating to prevent false positive undefined variable errors. - Strict rules (in
rules.neon): Checks for better assertion usage (e.g., preferassertTrue()overassertSame(true, ...)),@coversvalidation, data provider declaration checks, and more.
This repository supports PHP 7.4+. Do not use language features unavailable in PHP 7.4 (e.g., enums, fibers, readonly properties, intersection types in code — though they appear in stubs/phpDocs).
The extension supports multiple PHPUnit versions: ^9.5, ^10.5, ^11.5, ^12.0. Code must be compatible across all these versions. The CI matrix tests all combinations.
# Install dependencies
composer install
# Run all checks (lint, coding standard, tests, PHPStan)
make check
# Run tests only
make tests
# Run PHPStan analysis
make phpstan
# Run linting
make lint
# Install coding standard tool (first time only)
make cs-install
# Run coding standard checks
make cs
# Fix coding standard issues
make cs-fix
# Generate PHPStan baseline
make phpstan-generate-baselinesrc/
├── PhpDoc/PHPUnit/ # PHPDoc type resolution extensions
├── Rules/PHPUnit/ # Static analysis rules for PHPUnit
└── Type/PHPUnit/ # Type-specifying and dynamic return type extensions
└── Assert/ # Assert method type narrowing
tests/
├── Rules/PHPUnit/ # Rule tests and test data (data/ subdirectory)
├── Rules/Methods/ # Method call rule tests
├── Type/PHPUnit/ # Type extension tests and test data (data/ subdirectory)
└── bootstrap.php # Test bootstrap (loads Composer autoloader)
stubs/ # PHPUnit stub files for type definitions
extension.neon— Main extension configuration registered via phpstan/extension-installer. Defines parameters, services (type extensions, helpers), and stub files.rules.neon— Strict PHPUnit-specific rules. Loaded separately; users opt in by including this file.phpstan.neon— Self-analysis configuration (level 8, with strict rules and deprecation rules).phpstan-baseline.neon— Baseline for known PHPStan errors in the project itself.phpunit.xml— PHPUnit configuration for running the test suite.
These implement PHPStan interfaces to provide correct types:
MockBuilderDynamicReturnTypeExtension— PreservesMockBuilder<T>generic type through chained method calls.MockForIntersectionDynamicReturnTypeExtension— ReturnsMockObject&Tintersection types for mock creation methods.Assert/AssertMethodTypeSpecifyingExtension(and function/static variants) — Narrows types after assert calls (e.g., afterassertInstanceOf(Foo::class, $x),$xis known to beFoo).
These implement PHPStan\Rules\Rule<T> to report errors:
AssertSameBooleanExpectedRule,AssertSameNullExpectedRule— Suggest specific assertions over genericassertSame.AssertSameWithCountRule— SuggestassertCount()overassertSame(count(...), ...).ClassCoversExistsRule,ClassMethodCoversExistsRule— Validate@coversannotations reference existing code.DataProviderDeclarationRule,DataProviderDataRule— Validate data provider declarations and data.MockMethodCallRule— Check mock method calls are valid.ShouldCallParentMethodsRule— VerifysetUp()/tearDown()call parent methods.
PHPStan stub files that provide generic type information for PHPUnit classes (e.g., TestCase::createMock<T>() returns MockObject&T).
- Rule tests extend
PHPStan\Testing\RuleTestCase<T>. They implementgetRule()and call$this->analyse()with a test data file path and expected errors array. Test data files live intests/Rules/PHPUnit/data/. - Type tests extend
PHPStan\Testing\TypeInferenceTestCase. They use@dataProviderwithself::gatherAssertTypes()orself::dataFileAsserts()and call$this->assertFileAsserts(). Test data files live intests/Type/PHPUnit/data/. - Both types override
getAdditionalConfigFiles()to return the path toextension.neon(and sometimesrules.neon).
- Uses tabs for indentation (PHP, XML, NEON files).
- Uses spaces for YAML files (indent size 2).
- Coding standard is enforced via phpstan/build-cs (PHPCS with a custom standard).
- Run
make csto check,make cs-fixto auto-fix.
The GitHub Actions workflow (.github/workflows/build.yml) runs on the 2.0.x branch and pull requests:
- Lint — PHP syntax check across PHP 7.4–8.5.
- Coding Standard — PHPCS checks using build-cs.
- Tests — PHPUnit across PHP 7.4–8.5 × lowest/highest dependencies × PHPUnit 9.5/10.5/11.5/12.0 (with version-appropriate exclusions).
- Static Analysis — PHPStan self-analysis with the same matrix.
- Mutation Testing — Infection framework on PHP 8.2–8.5, requires 100% MSI on changed lines.
The main development branch is 2.0.x.