brainbits / phpstan-rules
PHPStan extension with opinionated strict rules for better code in tests.
Package info
github.com/brainbits/phpstan-rules
Type:phpstan-extension
pkg:composer/brainbits/phpstan-rules
Requires
- php: ^8.4
- nette/utils: ^4.0
- phpstan/phpstan: ^2.0
Requires (Dev)
- brainbits/phpcs-standard: ^8.0
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-03 19:53:25 UTC
README
This extension provides highly opinionated and strict rules for test cases for the PHPStan static analysis tool.
Installation
Run
$ composer require --dev brainbits/phpstan-rules
Usage
All of the rules provided by this library are included in rules.neon.
When you are using phpstan/extension-installer, rules.neon will be automatically included.
Otherwise you need to include rules.neon in your phpstan.neon:
# phpstan.neon includes: - vendor/brainbits/phpstan-rules/rules.neon
Rules
This package provides the following rules for use with phpstan/phpstan:
CoversClassExistsRule
This rule checks that classes that are covered by @covers annotation or #[CoversClass] attribute exist.
CoversClassPresentRule
This rule forces you to specify either a @covers annotation or #[CoversClass], #[CoversFunction] or #[CoversNothing] attributes in unit tests.
Why:
- It prevents code coverage sums to show higher values than expected.
❌
// tests/ExampleTestCase/Unit/MyInvalidClassTest.php namespace ExampleTestCase\Unit; final class MyInvalidClassTest extends \PHPUnit\Framework\TestCase {}
✅
// tests/ExampleTestCase/Unit/MyClassTest.php namespace ExampleTestCase\Unit; #[\PHPUnit\Framework\Attributes\CoversClass(MyClass::class)] final class MyClassTest extends \PHPUnit\Framework\TestCase {}
Defaults
- By default, this rule treats a class as a unit test if it extends another class, its namespace contains the string
Unitand its name ends withTest. The parent class itself is not checked.
Detecting unit tests namespace
If you want to change the namespace string check described above, you can set your own string to be checked in the unitTestNamespaceContainsString parameter.
# phpstan.neon parameters: brainbits: unitTestNamespaceContainsString: CustomTestPath