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
12 changes: 10 additions & 2 deletions src/Rules/PHPUnit/AttributeVersionRequirementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function is_numeric;
use function preg_match;
use function sprintf;
use function strpos;
use function substr_count;
use function version_compare;

Expand Down Expand Up @@ -96,11 +97,18 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
continue;
}

$pharIoVersions = strpos($attr->getName(), 'RequiresPhpunit') !== false
? $this->PHPUnitVersion->getPharIoVersions()
: $phpstanPharIoVersions;
if ($pharIoVersions === []) {
continue;
}

try {
// check composer like version constraints, e.g. ^1 or ~2
$testPhpVersionConstraint = $parser->parse($versionRequirement);

foreach ($phpstanPharIoVersions as $pharIoVersion) {
foreach ($pharIoVersions as $pharIoVersion) {
if ($testPhpVersionConstraint->complies($pharIoVersion)) {
// one of the versions within range matched, check next attribute
continue 2;
Expand All @@ -120,7 +128,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array

$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';

foreach ($phpstanPharIoVersions as $pharIoVersion) {
foreach ($pharIoVersions as $pharIoVersion) {
if (version_compare($pharIoVersion->getVersionString(), $matches['version'], $operator)) {
// one of the versions within range matched, check next attribute
continue 2;
Expand Down
17 changes: 17 additions & 0 deletions src/Rules/PHPUnit/PHPUnitVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace PHPStan\Rules\PHPUnit;

use PharIo\Version\Version;
use PHPStan\TrinaryLogic;
use function sprintf;

class PHPUnitVersion
{
Expand All @@ -17,6 +19,21 @@ public function __construct(?int $majorVersion, ?int $minorVersion)
$this->minorVersion = $minorVersion;
}

/**
* @return array{}|array{Version, Version}
*/
public function getPharIoVersions(): array
{
if ($this->majorVersion === null || $this->minorVersion === null) {
return [];
}

return [
new Version(sprintf('%d.%d.0', $this->majorVersion, $this->minorVersion)),
new Version(sprintf('%d.%d.99', $this->majorVersion, $this->minorVersion)),
];
}

public function supportsDataProviderAttribute(): TrinaryLogic
{
if ($this->majorVersion === null) {
Expand Down
13 changes: 9 additions & 4 deletions tests/Rules/PHPUnit/data/requires-phpunit-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

class TwoDigitVersionA extends TestCase
{
#[RequiresPhpunit('8.0')]
#[RequiresPhpunit('11.0')]
public function testFoo(): void {

}
}

#[RequiresPhpunit('>=8.0')]
#[RequiresPhpunit('>=11.0')]
class TwoDigitVersionB extends TestCase
{
public function testBar(): void {
Expand All @@ -25,13 +25,18 @@ public function testBar(): void {

class CorrectRequirement extends TestCase
{
#[RequiresPhpunit('>=8.0.0')]
#[RequiresPhpunit('>=11.0.0')]
public function testBar(): void {

}

#[RequiresPhpunit('^12.0.0')]
public function testBaz(): void {

}
}

#[RequiresPhpunit('>=8.0.0')]
#[RequiresPhpunit('>=11.0.0')]
class CorrectClassRequirement extends TestCase
{
public function testBar(): void {
Expand Down
Loading