Skip to content

Commit

Permalink
[TypeDeclaration] Add AddTestsVoidReturnTypeWhereNoReturnRector (#5611)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 12, 2024
1 parent 48e8cfa commit 7de77e5
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 2 deletions.
24 changes: 22 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 358 Rules Overview
# 359 Rules Overview

<br>

Expand Down Expand Up @@ -56,7 +56,7 @@

- [Transform](#transform) (23)

- [TypeDeclaration](#typedeclaration) (42)
- [TypeDeclaration](#typedeclaration) (43)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -6495,6 +6495,26 @@ Changes defined return typehint of method and class.

<br>

### AddTestsVoidReturnTypeWhereNoReturnRector

Add void to PHPUnit test methods

- class: [`Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector`](../rules/TypeDeclaration/Rector/Class_/AddTestsVoidReturnTypeWhereNoReturnRector.php)

```diff
use PHPUnit\Framework\TestCase;

class SomeClass extends TestCase
{
- public function testSomething()
+ public function testSomething(): void
{
}
}
```

<br>

### AddVoidReturnTypeWhereNoReturnRector

Add return type void to function like without any return
Expand Down
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';
}
}
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()
{

}
}
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()
{
}
}
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()
{
}
}
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
{
}
}

?>
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);
};
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;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rector\Contract\Rector\RectorInterface;
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector;
use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector;
Expand Down Expand Up @@ -55,6 +56,7 @@ final class TypeDeclarationLevel
// start with closure first, as safest
AddClosureVoidReturnTypeWhereNoReturnRector::class,
AddFunctionVoidReturnTypeWhereNoReturnRector::class,
AddTestsVoidReturnTypeWhereNoReturnRector::class,

AddVoidReturnTypeWhereNoReturnRector::class,

Expand Down

0 comments on commit 7de77e5

Please sign in to comment.