Skip to content

Commit 047c2a4

Browse files
authored
add attribute (#526)
1 parent 7a8f6f9 commit 047c2a4

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector\Fixture;
4+
5+
use PHPUnit\Framework\Attributes\Depends;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class SomeFileTest extends TestCase
9+
{
10+
public function test(): \stdClass
11+
{
12+
return new \stdClass();
13+
}
14+
15+
#[Depends('test')]
16+
public function testWithDepends($value)
17+
{
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector\Fixture;
26+
27+
use PHPUnit\Framework\Attributes\Depends;
28+
use PHPUnit\Framework\TestCase;
29+
30+
final class SomeFileTest extends TestCase
31+
{
32+
public function test(): \stdClass
33+
{
34+
return new \stdClass();
35+
}
36+
37+
#[Depends('test')]
38+
public function testWithDepends(\stdClass $value)
39+
{
40+
}
41+
}
42+
43+
?>

rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Attribute;
9+
use PhpParser\Node\Scalar\String_;
810
use PhpParser\Node\Stmt\Class_;
911
use PhpParser\Node\Stmt\ClassMethod;
1012
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
13+
use PHPUnit\Framework\Attributes\Depends;
1114
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1215
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
16+
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder;
1317
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1418
use Rector\Rector\AbstractRector;
1519
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -23,6 +27,7 @@ final class AddParamTypeFromDependsRector extends AbstractRector
2327
public function __construct(
2428
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
2529
private readonly PhpDocInfoFactory $phpDocInfoFactory,
30+
private readonly AttrinationFinder $attrinationFinder
2631
) {
2732
}
2833

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

129134
private function resolveReturnTypeOfDependsMethod(ClassMethod $classMethod, Class_ $class): ?Node
130135
{
136+
$dependsMethodName = $this->resolveDependsAnnotationOrAttributeMethod($classMethod);
137+
138+
$dependsClassMethod = $class->getMethod($dependsMethodName);
139+
140+
if (! $dependsClassMethod instanceof ClassMethod) {
141+
return null;
142+
}
143+
144+
// resolve return type here
145+
return $dependsClassMethod->returnType;
146+
}
147+
148+
private function resolveDependsAnnotationOrAttributeMethod(ClassMethod $classMethod): ?string
149+
{
150+
$dependsAttribute = $this->attrinationFinder->getByOne($classMethod, Depends::class);
151+
if ($dependsAttribute instanceof Attribute) {
152+
$firstArg = $dependsAttribute->args[0];
153+
if ($firstArg->value instanceof String_) {
154+
$dependsMethodName = $firstArg->value->value;
155+
return trim($dependsMethodName, '()');
156+
}
157+
}
158+
131159
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
132160
if (! $phpDocInfo instanceof PhpDocInfo) {
133161
return null;
@@ -139,15 +167,6 @@ private function resolveReturnTypeOfDependsMethod(ClassMethod $classMethod, Clas
139167
}
140168

141169
$dependsMethodName = (string) $dependsTagValueNode->value;
142-
$dependsMethodName = trim($dependsMethodName, '()');
143-
144-
$dependsClassMethod = $class->getMethod($dependsMethodName);
145-
146-
if (! $dependsClassMethod instanceof ClassMethod) {
147-
return null;
148-
}
149-
150-
// resolve return type here
151-
return $dependsClassMethod->returnType;
170+
return trim($dependsMethodName, '()');
152171
}
153172
}

0 commit comments

Comments
 (0)