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
8 changes: 4 additions & 4 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ services:
class: PHPStan\Rules\PHPUnit\AnnotationHelper

-
class: PHPStan\Rules\PHPUnit\PHPUnitVersionDetector
class: PHPStan\Rules\PHPUnit\TestMethodsHelper

-
class: PHPStan\Rules\PHPUnit\TestMethodsHelper
factory: @PHPStan\Rules\PHPUnit\TestMethodsHelperFactory::create()
class: PHPStan\Rules\PHPUnit\PHPUnitVersion
factory: @PHPStan\Rules\PHPUnit\PHPUnitVersionDetector::createPHPUnitVersion()
-
class: PHPStan\Rules\PHPUnit\TestMethodsHelperFactory
class: PHPStan\Rules\PHPUnit\PHPUnitVersionDetector

-
class: PHPStan\Rules\PHPUnit\DataProviderHelper
Expand Down
14 changes: 9 additions & 5 deletions src/Rules/PHPUnit/DataProviderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class DataProviderHelper

private Parser $parser;

private bool $phpunit10OrNewer;
private PHPUnitVersion $PHPUnitVersion;

public function __construct(
ReflectionProvider $reflectionProvider,
FileTypeMapper $fileTypeMapper,
Parser $parser,
bool $phpunit10OrNewer
PHPUnitVersion $PHPUnitVersion
)
{
$this->reflectionProvider = $reflectionProvider;
$this->fileTypeMapper = $fileTypeMapper;
$this->parser = $parser;
$this->phpunit10OrNewer = $phpunit10OrNewer;
$this->PHPUnitVersion = $PHPUnitVersion;
}

/**
Expand All @@ -65,7 +65,7 @@ public function getDataProviderMethods(
{
yield from $this->yieldDataProviderAnnotations($testMethod, $scope, $classReflection);

if (!$this->phpunit10OrNewer) {
if (!$this->PHPUnitVersion->supportsDataProviderAttribute()->yes()) {
return;
}

Expand Down Expand Up @@ -156,7 +156,11 @@ public function processDataProvider(
->build();
}

if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
if (
$deprecationRulesInstalled
&& $this->PHPUnitVersion->requiresStaticDataProviders()->yes()
&& !$dataProviderMethodReflection->isStatic()
) {
$errorBuilder = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
$dataProviderValue,
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/PHPUnit/DataProviderHelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ class DataProviderHelperFactory

private Parser $parser;

private PHPUnitVersionDetector $PHPUnitVersionDetector;
private PHPUnitVersion $PHPUnitVersion;

public function __construct(
ReflectionProvider $reflectionProvider,
FileTypeMapper $fileTypeMapper,
Parser $parser,
PHPUnitVersionDetector $PHPUnitVersionDetector
PHPUnitVersion $PHPUnitVersion
)
{
$this->reflectionProvider = $reflectionProvider;
$this->fileTypeMapper = $fileTypeMapper;
$this->parser = $parser;
$this->PHPUnitVersionDetector = $PHPUnitVersionDetector;
$this->PHPUnitVersion = $PHPUnitVersion;
}

public function create(): DataProviderHelper
{
return new DataProviderHelper($this->reflectionProvider, $this->fileTypeMapper, $this->parser, $this->PHPUnitVersionDetector->isPHPUnit10OrNewer());
return new DataProviderHelper($this->reflectionProvider, $this->fileTypeMapper, $this->parser, $this->PHPUnitVersion);
}

}
41 changes: 41 additions & 0 deletions src/Rules/PHPUnit/PHPUnitVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PHPStan\TrinaryLogic;

class PHPUnitVersion
{

private ?int $majorVersion;

public function __construct(?int $majorVersion)
{
$this->majorVersion = $majorVersion;
}

public function supportsDataProviderAttribute(): TrinaryLogic
{
if ($this->majorVersion === null) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
}

public function supportsTestAttribute(): TrinaryLogic
{
if ($this->majorVersion === null) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
}

public function requiresStaticDataProviders(): TrinaryLogic
{
if ($this->majorVersion === null) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
}

}
15 changes: 3 additions & 12 deletions src/Rules/PHPUnit/PHPUnitVersionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@
class PHPUnitVersionDetector
{

private ?bool $is10OrNewer = null;

private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function isPHPUnit10OrNewer(): bool
public function createPHPUnitVersion(): PHPUnitVersion
{
if ($this->is10OrNewer !== null) {
return $this->is10OrNewer;
}

$this->is10OrNewer = false;
$majorVersion = null;
if ($this->reflectionProvider->hasClass(TestCase::class)) {
$testCase = $this->reflectionProvider->getClass(TestCase::class);
$file = $testCase->getFileName();
Expand All @@ -42,16 +36,13 @@ public function isPHPUnit10OrNewer(): bool
$version = $json['extra']['branch-alias']['dev-main'] ?? null;
if ($version !== null) {
$majorVersion = (int) explode('.', $version)[0];
if ($majorVersion >= 10) {
$this->is10OrNewer = true;
}
}
}
}
}
}

return $this->is10OrNewer;
return new PHPUnitVersion($majorVersion);
}

}
8 changes: 4 additions & 4 deletions src/Rules/PHPUnit/TestMethodsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ final class TestMethodsHelper

private FileTypeMapper $fileTypeMapper;

private bool $phpunit10OrNewer;
private PHPUnitVersion $PHPUnitVersion;

public function __construct(
FileTypeMapper $fileTypeMapper,
bool $phpunit10OrNewer
PHPUnitVersion $PHPUnitVersion
)
{
$this->fileTypeMapper = $fileTypeMapper;
$this->phpunit10OrNewer = $phpunit10OrNewer;
$this->PHPUnitVersion = $PHPUnitVersion;
}

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ public function getTestMethods(ClassReflection $classReflection, Scope $scope):
}
}

if (!$this->phpunit10OrNewer) {
if ($this->PHPUnitVersion->supportsTestAttribute()->no()) {
continue;
}

Expand Down
28 changes: 0 additions & 28 deletions src/Rules/PHPUnit/TestMethodsHelperFactory.php

This file was deleted.

13 changes: 11 additions & 2 deletions tests/Rules/PHPUnit/DataProviderDataRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
class DataProviderDataRuleTest extends RuleTestCase
{
private int $phpunitVersion;

protected function getRule(): Rule
{
Expand All @@ -24,13 +25,13 @@ protected function getRule(): Rule
new DataProviderDataRule(
new TestMethodsHelper(
self::getContainer()->getByType(FileTypeMapper::class),
true
new PHPUnitVersion($this->phpunitVersion)
),
new DataProviderHelper(
$reflectionProvider,
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getService('defaultAnalysisParser'),
true
new PHPUnitVersion($this->phpunitVersion)
),

),
Expand All @@ -42,6 +43,8 @@ protected function getRule(): Rule

public function testRule(): void
{
$this->phpunitVersion = 10;

$this->analyse([__DIR__ . '/data/data-provider-data.php'], [
[
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, int given.',
Expand Down Expand Up @@ -176,6 +179,8 @@ public function testRulePhp8(): void
self::markTestSkipped();
}

$this->phpunitVersion = 10;

$this->analyse([__DIR__ . '/data/data-provider-data-named.php'], [
[
'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.',
Expand Down Expand Up @@ -203,6 +208,8 @@ public function testRulePhp8(): void

public function testVariadicMethod(): void
{
$this->phpunitVersion = 10;

$this->analyse([__DIR__ . '/data/data-provider-variadic-method.php'], [
[
'Method DataProviderVariadicMethod\FooTest::testProvide2() invoked with 1 parameter, at least 2 required.',
Expand Down Expand Up @@ -241,6 +248,8 @@ public function testVariadicMethod(): void

public function testTrimmingArgs(): void
{
$this->phpunitVersion = 10;

$this->analyse([__DIR__ . '/data/data-provider-trimming-args.php'], [
[
'Method DataProviderTrimmingArgs\FooTest::testProvide() invoked with 2 parameters, 1 required.',
Expand Down
Loading
Loading