Error Identifier: phpunit.coversDuplicate
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
/**
* @covers \App\UserService
*/
class UserServiceTest extends TestCase
{
/**
* @covers \App\UserService
*/
public function testCreate(): void
{
// ...
}
}
Why is it reported? #
There are two cases where this identifier is reported:
- A method-level
@coversannotation references the same class or method that is already covered by a class-level@coversannotation. The method-level annotation is redundant because the class-level annotation already covers it. - The
@coversDefaultClassannotation is defined multiple times on the same class.
This rule is provided by the phpstan-phpunit package.
In the example above, both the class and the method have @covers \App\UserService, making the method-level annotation redundant.
How to fix it #
Remove the redundant method-level @covers annotation:
/**
* @covers \App\UserService
*/
class UserServiceTest extends TestCase
{
- /**
- * @covers \App\UserService
- */
public function testCreate(): void
{
// ...
}
}
If the method is intended to cover a specific method rather than the whole class, be more specific:
/**
- * @covers \App\UserService
+ * @covers \App\UserService::create
*/
public function testCreate(): void
{
// ...
}
How to ignore this error #
You can use the identifier phpunit.coversDuplicate to ignore this error using a comment:
// @phpstan-ignore phpunit.coversDuplicate
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: phpunit.coversDuplicate