-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Privatization] Add FinalizeTestCaseClassRector
- Loading branch information
1 parent
3cfdad6
commit c6079f4
Showing
6 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ivatization/Rector/Class_/FinalizeTestCaseClassRector/FinalizeTestCaseClassRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Privatization\Rector\Class_\FinalizeTestCaseClassRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class FinalizeTestCaseClassRectorTest 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'; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...-tests/Privatization/Rector/Class_/FinalizeTestCaseClassRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Privatization\Rector\Class_\FinalizeTestCaseClassRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeClass extends TestCase | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Privatization\Rector\Class_\FinalizeTestCaseClassRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeClass extends TestCase | ||
{ | ||
} | ||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
...-tests/Privatization/Rector/Class_/FinalizeTestCaseClassRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<<<<<<< HEAD | ||
use Rector\Privatization\Rector\Class_\FinalizeTestCaseClassRector; | ||
|
||
use Rector\Config\RectorConfig; | ||
======= | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Privatization\Rector\Class_\FinalizeTestCaseClassRector; | ||
>>>>>>> 3df8dfb163 ([Privatization] Add FinalizeTestCaseClassRector) | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(FinalizeTestCaseClassRector::class); | ||
}; |
86 changes: 86 additions & 0 deletions
86
rules/Privatization/Rector/Class_/FinalizeTestCaseClassRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Privatization\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use Rector\Privatization\NodeManipulator\VisibilityManipulator; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Privatization\Rector\Class_\FinalizeTestCaseClassRector\FinalizeTestCaseClassRectorTest | ||
*/ | ||
final class FinalizeTestCaseClassRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly ReflectionProvider $reflectionProvider, | ||
private readonly VisibilityManipulator $visibilityManipulator, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('PHPUnit test case will be finalized', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
class SomeClass extends TestCase | ||
{ | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final use PHPUnit\Framework\TestCase; | ||
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 | ||
{ | ||
// skip obvious cases | ||
if ($node->isAbstract() || $node->isAnonymous() || $node->isFinal()) { | ||
return null; | ||
} | ||
|
||
$className = $this->getName($node); | ||
if (! is_string($className)) { | ||
return null; | ||
} | ||
|
||
if (! $this->reflectionProvider->hasClass($className)) { | ||
return null; | ||
} | ||
|
||
$classReflection = $this->reflectionProvider->getClass($className); | ||
if (! $classReflection->isSubclassOf('PHPUnit\Framework\TestCase')) { | ||
return null; | ||
} | ||
|
||
$this->visibilityManipulator->makeFinal($node); | ||
|
||
return $node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters