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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;

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

#[Depends('test')]
public function testWithDepends($value)
{
}
}

?>
-----
<?php

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

use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;

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

#[Depends('test')]
public function testWithDepends(\stdClass $value)
{
}
}

?>
39 changes: 29 additions & 10 deletions rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPUnit\Framework\Attributes\Depends;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -23,6 +27,7 @@ final class AddParamTypeFromDependsRector extends AbstractRector
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly AttrinationFinder $attrinationFinder
) {
}

Expand Down Expand Up @@ -128,6 +133,29 @@ public function refactor(Node $node): ?Node

private function resolveReturnTypeOfDependsMethod(ClassMethod $classMethod, Class_ $class): ?Node
{
$dependsMethodName = $this->resolveDependsAnnotationOrAttributeMethod($classMethod);

$dependsClassMethod = $class->getMethod($dependsMethodName);

if (! $dependsClassMethod instanceof ClassMethod) {
return null;
}

// resolve return type here
return $dependsClassMethod->returnType;
}

private function resolveDependsAnnotationOrAttributeMethod(ClassMethod $classMethod): ?string
{
$dependsAttribute = $this->attrinationFinder->getByOne($classMethod, Depends::class);
if ($dependsAttribute instanceof Attribute) {
$firstArg = $dependsAttribute->args[0];
if ($firstArg->value instanceof String_) {
$dependsMethodName = $firstArg->value->value;
return trim($dependsMethodName, '()');
}
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
Expand All @@ -139,15 +167,6 @@ private function resolveReturnTypeOfDependsMethod(ClassMethod $classMethod, Clas
}

$dependsMethodName = (string) $dependsTagValueNode->value;
$dependsMethodName = trim($dependsMethodName, '()');

$dependsClassMethod = $class->getMethod($dependsMethodName);

if (! $dependsClassMethod instanceof ClassMethod) {
return null;
}

// resolve return type here
return $dependsClassMethod->returnType;
return trim($dependsMethodName, '()');
}
}