Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Rules/PHPUnit/AttributeVersionRequirementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't handle other argument types, because phpstan-src will verify types already.
just skip everything invalid

continue;
}

if ($this->warnAboutIncompleteVersion($versionRequirement)) {
$errors[] = RuleErrorBuilder::message(
Expand Down
15 changes: 15 additions & 0 deletions tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions tests/Rules/PHPUnit/data/requires-php-version-non-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace RequiresPhpVersionNonString;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

class IntArgument extends TestCase
{
#[RequiresPhp(8)]
public function testFoo(): void {

}
}

class FloatArgument extends TestCase
{
#[RequiresPhp(8.0)]
public function testFoo(): void {

}
}

class ArrayArgument extends TestCase
{
#[RequiresPhp(['8.0'])]
public function testFoo(): void {

}
}

class NamedArgument extends TestCase
{
#[RequiresPhp(versionRequirement: '>=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 {

}
}