-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add AddTestsVoidReturnTypeWhereNoReturnRector (#5611)
- Loading branch information
1 parent
48e8cfa
commit 7de77e5
Showing
9 changed files
with
243 additions
and
2 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
...dTestsVoidReturnTypeWhereNoReturnRector/AddTestsVoidReturnTypeWhereNoReturnRectorTest.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\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddTestsVoidReturnTypeWhereNoReturnRectorTest 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'; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector/Fixture/skip_data_provider.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,20 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SkipDataProvider extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
protected function testSomething() | ||
{ | ||
} | ||
|
||
public function provideData() | ||
{ | ||
|
||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...on/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector/Fixture/skip_no_public.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SkipNoPublic extends TestCase | ||
{ | ||
protected function testSomething() | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector/Fixture/skip_no_test_case.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector\Fixture; | ||
|
||
class SkipNoTestCase | ||
{ | ||
public function testSomething() | ||
{ | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector/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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeClass extends TestCase | ||
{ | ||
public function testSomething() | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeClass extends TestCase | ||
{ | ||
public function testSomething(): void | ||
{ | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...ration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(AddTestsVoidReturnTypeWhereNoReturnRector::class); | ||
}; |
110 changes: 110 additions & 0 deletions
110
rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.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,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt\Class_; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector\AddTestsVoidReturnTypeWhereNoReturnRectorTest | ||
*/ | ||
final class AddTestsVoidReturnTypeWhereNoReturnRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
private readonly SilentVoidResolver $silentVoidResolver, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add void to PHPUnit test methods', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
class SomeClass extends TestCase | ||
{ | ||
public function testSomething() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
class SomeClass extends TestCase | ||
{ | ||
public function testSomething(): void | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
foreach ($node->getMethods() as $classMethod) { | ||
// has type already | ||
if ($classMethod->returnType instanceof Node) { | ||
continue; | ||
} | ||
|
||
if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) { | ||
continue; | ||
} | ||
|
||
if ($classMethod->isAbstract()) { | ||
continue; | ||
} | ||
|
||
if (! $this->silentVoidResolver->hasExclusiveVoid($classMethod)) { | ||
continue; | ||
} | ||
|
||
$classMethod->returnType = new Identifier('void'); | ||
$hasChanged = true; | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::VOID_TYPE; | ||
} | ||
} |
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