PHPackages                             brainbits/phpstan-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. brainbits/phpstan-rules

ActivePhpstan-extension[Testing &amp; Quality](/categories/testing)

brainbits/phpstan-rules
=======================

PHPStan extension with opinionated strict rules for better code in tests.

4.0.0(1y ago)4218.6k↓41.4%19MITPHPPHP ^8.3CI passing

Since Feb 12Pushed 10mo ago3 watchersCompare

[ Source](https://github.com/brainbits/phpstan-rules)[ Packagist](https://packagist.org/packages/brainbits/phpstan-rules)[ Docs](https://github.com/brainbits/phpstan-rules)[ RSS](/packages/brainbits-phpstan-rules/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (4)Dependencies (7)Versions (12)Used By (9)

PHPStan Clean Test rules
========================

[](#phpstan-clean-test-rules)

[![Continuous Integration](https://github.com/brainbits/phpstan-rules/workflows/Tests/badge.svg)](https://github.com/brainbits/phpstan-rules/workflows/Tests/badge.svg)[![Coverage Status](https://camo.githubusercontent.com/0bda94036a3c914e14193e87bc27e1ce870f94ef45ae3f4993ddfd00e757cbf0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f627261696e626974732f7068707374616e2d72756c65732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/brainbits/phpstan-rules?branch=master)[![Codacy Badge](https://camo.githubusercontent.com/4ccaf148e0dc17232351acc966aa5dc1854ec42a49afefba90105862c6eeba1c/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3838316263616565616132613435663539623061373736383066313566616665)](https://www.codacy.com/manual/brainbits/phpstan-rules?utm_source=github.com&utm_medium=referral&utm_content=brainbits/phpstan-rules&utm_campaign=Badge_Grade)[![Latest Stable Version](https://camo.githubusercontent.com/5e87401d9f72631d144fd4b7824ba6d0945db8a0ac0e278b2a0d58049dba9c7e/68747470733a2f2f706f7365722e707567782e6f72672f627261696e626974732f7068707374616e2d72756c65732f76657273696f6e)](https://packagist.org/packages/brainbits/phpstan-rules)[![License](https://camo.githubusercontent.com/284f6d7291b8cfbb86554806f027cb05fbeb4794021f59eb27c8e11fdebc9354/68747470733a2f2f706f7365722e707567782e6f72672f627261696e626974732f7068707374616e2d72756c65732f6c6963656e7365)](https://packagist.org/packages/brainbits/phpstan-rules)

- [PHPStan](https://github.com/phpstan/phpstan)
- [PHPStan-PHPUnit extension](https://github.com/phpstan/phpstan-phpunit)

This extension provides highly opinionated and strict rules for test cases for the PHPStan static analysis tool.

Installation
------------

[](#installation)

Run

```
$ composer require --dev brainbits/phpstan-rules
```

Usage
-----

[](#usage)

All of the [rules](https://github.com/brainbits/phpstan-rules#rules) provided by this library are included in [`rules.neon`](rules.neon).

When you are using [`phpstan/extension-installer`](https://github.com/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
-----

[](#rules)

This package provides the following rules for use with [`phpstan/phpstan`](https://github.com/phpstan/phpstan):

- [`Brainbits\PHPStan\Rules\CoversClassExistsRule`](#CoversClassExistsRule)
- [`Brainbits\PHPStan\Rules\CoversClassPresentRule`](#CoversClassPresentRule)

### `CoversClassExistsRule`

[](#coversclassexistsrule)

This rule checks that classes that are covered by `@covers` annotation or `#[CoversClass]` attribute exist.

### `CoversClassPresentRule`

[](#coversclasspresentrule)

This rule forces you to specify either a `@covers` annotation or `#[CoversClass]`, `#[CoversFunction]` or `#[CoversNothing]` attributes in unit tests (default: `PHPUnit\Framework\TestCase`).

**Why:**

1. 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

[](#defaults)

- By default, this rule detects unit tests by checking the namespace (it must contain the string `Unit`) and the class name ending (it must end with the string `Test`).

#### Detecting unit tests namespace

[](#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
```

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance45

Moderate activity, may be stable

Popularity39

Limited adoption so far

Community23

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 70% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~216 days

Recently: every ~201 days

Total

9

Last Release

599d ago

Major Versions

1.0.1 → 2.0.02021-04-26

2.0.0 → 3.0.02021-11-02

3.1.2 → 4.0.02024-11-11

PHP version history (6 changes)1.0.0PHP ^7.2

1.0.1PHP ^7.2|^8.0

2.0.0PHP ^7.3|^8.0

3.0.1PHP ^7.4|^8.0

3.1.0PHP ^8.1

4.0.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/353782?v=4)[brainbits GmbH](/maintainers/brainbits)[@brainbits](https://github.com/brainbits)

---

Top Contributors

[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (7 commits)")[![daFish](https://avatars.githubusercontent.com/u/150416?v=4)](https://github.com/daFish "daFish (1 commits)")[![pl-github](https://avatars.githubusercontent.com/u/1941578?v=4)](https://github.com/pl-github "pl-github (1 commits)")[![temp](https://avatars.githubusercontent.com/u/216128?v=4)](https://github.com/temp "temp (1 commits)")

---

Tags

PHPStantestsphpstan-rules

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brainbits-phpstan-rules/health.svg)

```
[![Health](https://phpackages.com/badges/brainbits-phpstan-rules/health.svg)](https://phpackages.com/packages/brainbits-phpstan-rules)
```

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.5k55.4M8.4k](/packages/larastan-larastan)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136406.3k14](/packages/rector-rector-src)[ssch/typo3-rector

Instant fixes for your TYPO3 PHP code by using Rector.

2603.2M435](/packages/ssch-typo3-rector)[shipmonk/dead-code-detector

Dead code detector to find unused PHP code via PHPStan extension. Can automatically remove dead PHP code. Supports libraries like Symfony, Doctrine, PHPUnit etc. Detects dead cycles. Can detect dead code that is tested.

4853.5M91](/packages/shipmonk-dead-code-detector)[symplify/phpstan-rules

Set of Symplify rules, type extensions and error formatter for PHPStan

26812.7M333](/packages/symplify-phpstan-rules)[orrison/meliorstan

Provides PHPStan rules for improved code quality by detecting code smells and possible issues. In addition to enforcing particular naming and code style conventions to reduce bike-shedding.

1915.9k5](/packages/orrison-meliorstan)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
