From a781ebf1eac163a1237d34b1cd2a451e999df8e2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 20 May 2025 18:25:32 +0700 Subject: [PATCH 1/2] [PHPUnit 12] Add RemoveOverrideFinalConstructTestCaseRector --- config/sets/phpunit110.php | 2 + config/sets/phpunit120.php | 8 ++ .../Fixture/fixture.php.inc | 25 +++++ ...errideFinalConstructTestCaseRectorTest.php | 28 ++++++ .../config/configured_rule.php | 10 ++ .../ParentTestClassConstructorRector.php | 6 +- ...veOverrideFinalConstructTestCaseRector.php | 96 +++++++++++++++++++ src/Set/PHPUnitSetList.php | 5 + 8 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 config/sets/phpunit120.php create mode 100644 rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/Fixture/fixture.php.inc create mode 100644 rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/RemoveOverrideFinalConstructTestCaseRectorTest.php create mode 100644 rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/config/configured_rule.php create mode 100644 rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php diff --git a/config/sets/phpunit110.php b/config/sets/phpunit110.php index 74a42ef4..576dd372 100644 --- a/config/sets/phpunit110.php +++ b/config/sets/phpunit110.php @@ -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); }; diff --git a/config/sets/phpunit120.php b/config/sets/phpunit120.php new file mode 100644 index 00000000..74a42ef4 --- /dev/null +++ b/config/sets/phpunit120.php @@ -0,0 +1,8 @@ + +----- + diff --git a/rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/RemoveOverrideFinalConstructTestCaseRectorTest.php b/rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/RemoveOverrideFinalConstructTestCaseRectorTest.php new file mode 100644 index 00000000..524c4e6b --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/RemoveOverrideFinalConstructTestCaseRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/config/configured_rule.php b/rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/config/configured_rule.php new file mode 100644 index 00000000..5a1fc60b --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(RemoveOverrideFinalConstructTestCaseRector::class); +}; diff --git a/rules/PHPUnit100/Rector/Class_/ParentTestClassConstructorRector.php b/rules/PHPUnit100/Rector/Class_/ParentTestClassConstructorRector.php index 528e727a..870ef270 100644 --- a/rules/PHPUnit100/Rector/Class_/ParentTestClassConstructorRector.php +++ b/rules/PHPUnit100/Rector/Class_/ParentTestClassConstructorRector.php @@ -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'); } } diff --git a/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php b/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php new file mode 100644 index 00000000..7bd78cea --- /dev/null +++ b/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php @@ -0,0 +1,96 @@ +> + */ + 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; + } +} diff --git a/src/Set/PHPUnitSetList.php b/src/Set/PHPUnitSetList.php index 271601f3..c1b20fcb 100644 --- a/src/Set/PHPUnitSetList.php +++ b/src/Set/PHPUnitSetList.php @@ -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 */ From f9bec3751cac5bb527699ce078e5b51658c36ef9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 20 May 2025 18:25:56 +0700 Subject: [PATCH 2/2] [PHPUnit 12] Add RemoveOverrideFinalConstructTestCaseRector --- config/sets/phpunit120.php | 2 ++ .../Class_/RemoveOverrideFinalConstructTestCaseRector.php | 7 ------- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/config/sets/phpunit120.php b/config/sets/phpunit120.php index 74a42ef4..5a1fc60b 100644 --- a/config/sets/phpunit120.php +++ b/config/sets/phpunit120.php @@ -3,6 +3,8 @@ 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); }; diff --git a/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php b/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php index 7bd78cea..f7938a82 100644 --- a/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php +++ b/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php @@ -5,15 +5,8 @@ namespace Rector\PHPUnit\PHPUnit120\Rector\Class_; use PhpParser\Node; -use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Expr\Yield_; -use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Expression; -use PhpParser\Node\Stmt\Return_; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; use Rector\ValueObject\MethodName;