An open API service indexing awesome lists of open source software.

https://github.com/brainbits/phpstan-rules

brainbits phpstan rules
https://github.com/brainbits/phpstan-rules

Last synced: 12 months ago
JSON representation

brainbits phpstan rules

Awesome Lists containing this project

README

          

# PHPStan Clean Test rules

![Continuous Integration](https://github.com/brainbits/phpstan-rules/workflows/Tests/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/brainbits/phpstan-rules/badge.svg?branch=master)](https://coveralls.io/github/brainbits/phpstan-rules?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/881bcaeeaa2a45f59b0a77680f15fafe)](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://poser.pugx.org/brainbits/phpstan-rules/version)](https://packagist.org/packages/brainbits/phpstan-rules)
[![License](https://poser.pugx.org/brainbits/phpstan-rules/license)](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

Run

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

## 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`:

```yaml
# phpstan.neon
includes:
- vendor/brainbits/phpstan-rules/rules.neon
```

## 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`

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 (default: `PHPUnit\Framework\TestCase`).

**Why:**
1. It prevents code coverage sums to show higher values than expected.

:x:

```php
// tests/ExampleTestCase/Unit/MyInvalidClassTest.php
namespace ExampleTestCase\Unit;

final class MyInvalidClassTest extends \PHPUnit\Framework\TestCase {}
```

:white_check_mark:

```php
// 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 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
If you want to change the namespace string check described above, you can set your own string to be checked in the `unitTestNamespaceContainsString` parameter.

```yaml
# phpstan.neon
parameters:
brainbits:
unitTestNamespaceContainsString: CustomTestPath
```