Error Identifier: phpunit.attributeRequiresPhpVersion
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;
use PHPUnit\Framework\Attributes\RequiresPhp;
class MyTest extends TestCase
{
#[RequiresPhp('8.1')]
public function testFeature(): void
{
// ...
}
}
This rule is provided by the phpstan-phpunit extension.
Why is it reported? #
The #[RequiresPhp] attribute specifies a bare version number like '8.1' without a comparison operator. Depending on the PHPUnit version, this is either required to include an operator or the bare version syntax is deprecated.
In newer PHPUnit versions, the version requirement must include an explicit comparison operator (e.g. >= 8.1). Passing only a numeric version string is ambiguous and may not behave as expected.
How to fix it #
Add a comparison operator to the version requirement:
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
class MyTest extends TestCase
{
- #[RequiresPhp('8.1')]
+ #[RequiresPhp('>= 8.1')]
public function testFeature(): void
{
// ...
}
}
How to ignore this error #
You can use the identifier phpunit.attributeRequiresPhpVersion to ignore this error using a comment:
// @phpstan-ignore phpunit.attributeRequiresPhpVersion
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.attributeRequiresPhpVersion