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
- Host: GitHub
- URL: https://github.com/brainbits/phpstan-rules
- Owner: brainbits
- Created: 2020-02-12T13:37:35.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-22T11:04:38.000Z (about 2 years ago)
- Last Synced: 2024-04-24T15:26:39.956Z (almost 2 years ago)
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHPStan Clean Test rules

[](https://coveralls.io/github/brainbits/phpstan-rules?branch=master)
[](https://www.codacy.com/manual/brainbits/phpstan-rules?utm_source=github.com&utm_medium=referral&utm_content=brainbits/phpstan-rules&utm_campaign=Badge_Grade)
[](https://packagist.org/packages/brainbits/phpstan-rules)
[](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
```