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
{
// ...
}
}
The #[RequiresPhp] attribute is also checked when placed on the test class itself, not just on individual test methods:
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
#[RequiresPhp('< 7.0')]
class MyTest extends TestCase
{
public function testFeature(): void
{
// ...
}
}
The same checks apply to the #[RequiresPhpunit] attribute, which restricts the PHPUnit version a test runs on:
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
class MyTest extends TestCase
{
#[RequiresPhpunit('11.0')]
public function testFeature(): void
{
// ...
}
}
This rule is provided by the phpstan-phpunit extension.
Why is it reported? #
The #[RequiresPhp] and #[RequiresPhpunit] attributes control whether a test (or every test in a class) runs on the current PHP or PHPUnit version. PHPStan reports several problems with their version requirement:
- Missing operator — a bare version number like
'8.1'is given without a comparison operator. In newer PHPUnit versions the requirement must include an explicit operator (e.g.>= 8.1); a bare numeric version is ambiguous. On older PHPUnit versions where it is merely deprecated, the error is reported only when phpstan-deprecation-rules is installed. - Always false — the constraint can never match any analysed PHP version (e.g.
'< 7.0'while analysing PHP 8.x), so the test would always be skipped. The analysed PHP versions come from thephpVersionsetting or fromcomposer.json. For#[RequiresPhpunit], the constraint is compared with the PHPUnit versions allowed bycomposer.json. This check runs on bleeding edge. - Incomplete version — the version is not a full
major.minor.patchtriple. PHPUnit may interpret it in surprising ways. This warning is reported on bleeding edge. - Invalid constraint — the version string is not a valid version constraint at all. This check runs on bleeding edge.
How to fix it #
Use a full major.minor.patch version together with an operator that can actually be satisfied by the analysed PHP (or PHPUnit) version.
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
{
// ...
}
}
Use a complete major.minor.patch version:
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
class MyTest extends TestCase
{
- #[RequiresPhpunit('11.0')]
+ #[RequiresPhpunit('>= 11.0.0')]
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