diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index fc29d48..203b3fe 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -14,6 +14,7 @@ use PHPStan\ShouldNotHappenException; use function count; use function is_numeric; +use function is_string; use function preg_match; use function sprintf; use function strpos; @@ -74,7 +75,11 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array // the following block is mimicing PHPUnit version parsing // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php - $versionRequirement = $args[0]; + // named arguments are handled just to make sure we don't crash on them. + $versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null; + if (!is_string($versionRequirement)) { + continue; + } if ($this->warnAboutIncompleteVersion($versionRequirement)) { $errors[] = RuleErrorBuilder::message( diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index adad73b..370d0a9 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -192,6 +192,21 @@ public function testWarnAboutIncompletePhpunitVersion(): void ]); } + public function testNonStringArguments(): void + { + $this->phpunitMajorVersion = 12; + $this->phpunitMinorVersion = 5; + $this->deprecationRulesInstalled = false; + $this->warnAboutIncompleteVersion = true; + + $this->analyse([__DIR__ . '/data/requires-php-version-non-string.php'], [ + [ + 'Version requirement >=8.0 is incomplete. Expect a version composed of major, minor and patch.', + 35, + ], + ]); + } + 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 f07b3a6..47e92de 100644 --- a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php @@ -53,6 +53,20 @@ public function testWarnAboutIncompletePhpunitVersion(): void ]); } + public function testNonStringArguments(): void + { + $this->phpunitMajorVersion = 12; + $this->phpunitMinorVersion = 5; + $this->warnAboutIncompleteVersion = true; + + $this->analyse([__DIR__ . '/data/requires-php-version-non-string.php'], [ + [ + 'Version requirement >=11.0 is incomplete. Expect a version composed of major, minor and patch.', + 57, + ], + ]); + } + protected function getRule(): Rule { $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); diff --git a/tests/Rules/PHPUnit/data/requires-php-version-non-string.php b/tests/Rules/PHPUnit/data/requires-php-version-non-string.php new file mode 100644 index 0000000..b817741 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-non-string.php @@ -0,0 +1,63 @@ +=8.0')] + public function testFoo(): void { + + } +} + +class PhpunitIntArgument extends TestCase +{ + #[RequiresPhpunit(11)] + public function testFoo(): void { + + } +} + +#[RequiresPhp(8)] +class ClassIntArgument extends TestCase +{ + public function testFoo(): void { + + } +} + +#[RequiresPhpunit(versionRequirement: '>=11.0')] +class ClassNamedArgument extends TestCase +{ + public function testFoo(): void { + + } +}