From 4f9fa070461f912e2e27981a6cda96b54ad05064 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 07:45:40 +0100 Subject: [PATCH 1/3] Don't crash analysis on wrongly typed argument values --- .../AttributeVersionRequirementHelper.php | 7 +- .../AttributeRequiresPhpVersionRuleTest.php | 18 ++++++ ...assAttributeRequiresPhpVersionRuleTest.php | 17 +++++ .../data/requires-php-version-non-string.php | 64 +++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-non-string.php diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index fc29d481..203b3fe7 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 adad73b4..3c69df44 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -192,6 +192,24 @@ public function testWarnAboutIncompletePhpunitVersion(): void ]); } + /** + * @see https://github.com/phpstan/phpstan-phpunit/issues/364 + */ + 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.', + 36, + ], + ]); + } + 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 f07b3a69..ce8d5175 100644 --- a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php @@ -53,6 +53,23 @@ public function testWarnAboutIncompletePhpunitVersion(): void ]); } + /** + * @see https://github.com/phpstan/phpstan-phpunit/issues/364 + */ + 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.', + 58, + ], + ]); + } + 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 00000000..4350a0b8 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-non-string.php @@ -0,0 +1,64 @@ +=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 { + + } +} From 7ec4d1e73e2854f08324421dbb67c99b5eef1938 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 07:58:23 +0100 Subject: [PATCH 2/3] remove useless comments --- tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php | 3 --- .../Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php | 3 --- tests/Rules/PHPUnit/data/requires-php-version-non-string.php | 1 - 3 files changed, 7 deletions(-) diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index 3c69df44..f8c1f656 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -192,9 +192,6 @@ public function testWarnAboutIncompletePhpunitVersion(): void ]); } - /** - * @see https://github.com/phpstan/phpstan-phpunit/issues/364 - */ public function testNonStringArguments(): void { $this->phpunitMajorVersion = 12; diff --git a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php index ce8d5175..f410f7cc 100644 --- a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php @@ -53,9 +53,6 @@ public function testWarnAboutIncompletePhpunitVersion(): void ]); } - /** - * @see https://github.com/phpstan/phpstan-phpunit/issues/364 - */ public function testNonStringArguments(): void { $this->phpunitMajorVersion = 12; diff --git a/tests/Rules/PHPUnit/data/requires-php-version-non-string.php b/tests/Rules/PHPUnit/data/requires-php-version-non-string.php index 4350a0b8..b8177416 100644 --- a/tests/Rules/PHPUnit/data/requires-php-version-non-string.php +++ b/tests/Rules/PHPUnit/data/requires-php-version-non-string.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\RequiresPhp; use PHPUnit\Framework\Attributes\RequiresPhpunit; -// https://github.com/phpstan/phpstan-phpunit/issues/364 class IntArgument extends TestCase { #[RequiresPhp(8)] From 1ec184950a482da24cd17573213e724a9df31b76 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 08:13:12 +0100 Subject: [PATCH 3/3] fix build --- tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php | 2 +- .../Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index f8c1f656..370d0a93 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -202,7 +202,7 @@ public function testNonStringArguments(): void $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.', - 36, + 35, ], ]); } diff --git a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php index f410f7cc..47e92dec 100644 --- a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php @@ -62,7 +62,7 @@ public function testNonStringArguments(): void $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.', - 58, + 57, ], ]); }