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
2 changes: 2 additions & 0 deletions config/sets/phpunit110.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit110\Rector\Class_\NamedArgumentForDataProviderRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(NamedArgumentForDataProviderRector::class);
};
10 changes: 10 additions & 0 deletions config/sets/phpunit120.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveOverrideFinalConstructTestCaseRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;

use PHPUnit\Framework\TestCase;

final class Fixture extends TestCase
{
public function __construct()
{
parent::__construct(static::class);
}
}
?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;

use PHPUnit\Framework\TestCase;

final class Fixture extends TestCase
{
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveOverrideFinalConstructTestCaseRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveOverrideFinalConstructTestCaseRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ private function shouldSkipClass(Class_ $class): bool
return true;
}

return str_ends_with((string) $className, 'TestCase');
if (str_ends_with((string) $className, 'TestCase')) {
return true;
}

return (bool) $class->getAttribute('hasRemovedFinalConstruct');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit120\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector\RemoveOverrideFinalConstructTestCaseRectorTest
*/
final class RemoveOverrideFinalConstructTestCaseRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove override final construct test case',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
public function __construct()
{
parent::__construct(static::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
}
CODE_SAMPLE
,
),
],
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): Node|null
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);

if ($constructClassMethod instanceof ClassMethod) {
foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof ClassMethod && $this->isName($stmt, MethodName::CONSTRUCT)) {
unset($node->stmts[$key]);

$node->setAttribute('hasRemovedFinalConstruct', true);
return $node;
}
}
}

return null;
}
}
5 changes: 5 additions & 0 deletions src/Set/PHPUnitSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class PHPUnitSetList
*/
public const PHPUNIT_110 = __DIR__ . '/../../config/sets/phpunit110.php';

/**
* @var string
*/
public const PHPUNIT_120 = __DIR__ . '/../../config/sets/phpunit120.php';

/**
* @var string
*/
Expand Down
Loading