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/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
Expand Down Expand Up @@ -72,6 +73,7 @@
TypeWillReturnCallableArrowFunctionRector::class,
StringCastAssertStringContainsStringRector::class,
AddParamTypeFromDependsRector::class,
AddReturnTypeToDependedRector::class,

NarrowUnusedSetUpDefinedPropertyRector::class,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector;

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

final class AddReturnTypeToDependedRectorTest 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,18 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipNonTestMethods extends TestCase
{
public function nonTestMethod()
{
return new \stdClass();
}

private function testNotMethod()
{
return new \stdClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipSilentVoid extends TestCase
{
public function test()
{
if (rand(0, 1)) {
return;
}

return new \stdClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeFileTest extends TestCase
{
public function test()
{
return new \stdClass();
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeFileTest extends TestCase
{
public function test(): \stdClass
{
return new \stdClass();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture;

use PHPUnit\Framework\TestCase;

final class VaribleAssigned extends TestCase
{
public function test()
{
$someValue = rand(0, 1);

return $someValue;
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture;

use PHPUnit\Framework\TestCase;

final class VaribleAssigned extends TestCase
{
public function test(): int
{
$someValue = rand(0, 1);

return $someValue;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector;

return RectorConfig::configure()
->withRules([AddReturnTypeToDependedRector::class]);
150 changes: 150 additions & 0 deletions rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\AddReturnTypeToDependedRectorTest
*/
final class AddReturnTypeToDependedRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ReturnAnalyzer $returnAnalyzer,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly BetterNodeFinder $betterNodeFinder
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add return type declaration to a test method that returns type',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test()
{
$value = new \stdClass();

return $value;
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test(): \stdClass
{
$value = new \stdClass();

return $value;
}
}
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) {
if (! $classMethod->isPublic()) {
continue;
}

if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) {
continue;
}

if (! $this->haveAllReturnsExpr($classMethod)) {
continue;
}
Comment on lines +100 to +102
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be removed.

Suggested change
if (! $this->haveAllReturnsExpr($classMethod)) {
continue;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #529


// already known return type
if ($classMethod->returnType instanceof Node) {
continue;
}

$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) {
return null;
}

if (count($returns) !== 1) {
continue;
}

$soleReturnExpr = $returns[0]->expr;

// does return a type?
$returnedExprType = $this->getType($soleReturnExpr);
$returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnedExprType, TypeKind::RETURN);

if (! $returnType instanceof Node) {
continue;
}

$classMethod->returnType = $returnType;
$hasChanged = true;
}

if ($hasChanged === false) {
return null;
}

return $node;
}

private function haveAllReturnsExpr(ClassMethod $classMethod): bool
{
$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);
foreach ($returns as $return) {
if (! $return->expr instanceof Expr) {
return false;
}
}

return true;
}
}