diff --git a/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php b/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php index 913ffea..b5521f7 100644 --- a/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php +++ b/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php @@ -7,6 +7,7 @@ use PHPStan\Node\InClassMethodNode; use PHPStan\Rules\Rule; use PHPUnit\Framework\TestCase; +use function array_merge; /** * @implements Rule @@ -44,8 +45,11 @@ public function processNode(Node $node, Scope $scope): array return []; } - return $this->attributeVersionRequirementHelper->checkRequiresPhpVersion( - $reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'), + return $this->attributeVersionRequirementHelper->checkVersionRequirement( + array_merge( + $reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'), + $reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhpunit'), + ), $scope, ); } diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index 22ae5ab..966c2bf 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -61,7 +61,7 @@ public function __construct( * * @return list */ - public function checkRequiresPhpVersion(array $attributes, Scope $scope): array + public function checkVersionRequirement(array $attributes, Scope $scope): array { $phpstanPharIoVersions = $this->getAnalyzedPhpVersions($scope); if ($phpstanPharIoVersions === []) { diff --git a/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php b/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php index 74c1f43..a9e68ff 100644 --- a/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php +++ b/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php @@ -7,6 +7,7 @@ use PHPStan\Node\InClassNode; use PHPStan\Rules\Rule; use PHPUnit\Framework\TestCase; +use function array_merge; /** * @implements Rule @@ -35,8 +36,11 @@ public function processNode(Node $node, Scope $scope): array return []; } - return $this->attributeVersionRequirementHelper->checkRequiresPhpVersion( - $classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'), + return $this->attributeVersionRequirementHelper->checkVersionRequirement( + array_merge( + $classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'), + $classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhpunit'), + ), $scope, ); } diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index 3dd09f8..40d0abd 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -170,6 +170,21 @@ public function testWarnAboutIncompleteVersion(): void ]); } + public function testWarnAboutIncompletePhpunitVersion(): void + { + $this->phpunitMajorVersion = 12; + $this->phpunitMinorVersion = 5; + $this->deprecationRulesInstalled = false; + $this->warnAboutIncompleteVersion = true; + + $this->analyse([__DIR__ . '/data/requires-phpunit-version.php'], [ + [ + 'Version requirement is incomplete.', + 12, + ], + ]); + } + protected function getRule(): Rule { $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); diff --git a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php index 113187f..31814c0 100644 --- a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php @@ -38,6 +38,20 @@ public function testWarnAboutIncompleteVersion(): void ]); } + public function testWarnAboutIncompletePhpunitVersion(): void + { + $this->phpunitMajorVersion = 12; + $this->phpunitMinorVersion = 5; + $this->warnAboutIncompleteVersion = true; + + $this->analyse([__DIR__ . '/data/requires-phpunit-version.php'], [ + [ + 'Version requirement is incomplete.', + 18, + ], + ]); + } + protected function getRule(): Rule { $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); diff --git a/tests/Rules/PHPUnit/data/requires-phpunit-version.php b/tests/Rules/PHPUnit/data/requires-phpunit-version.php new file mode 100644 index 0000000..e40f2b5 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-phpunit-version.php @@ -0,0 +1,40 @@ +=8.0')] +class TwoDigitVersionB extends TestCase +{ + public function testBar(): void { + + } +} + +class CorrectRequirement extends TestCase +{ + #[RequiresPhpunit('>=8.0.0')] + public function testBar(): void { + + } +} + +#[RequiresPhpunit('>=8.0.0')] +class CorrectClassRequirement extends TestCase +{ + public function testBar(): void { + + } +}