From ea1bd0ef312feebcbec6843e272cb419d0b9f7af Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 1 Sep 2025 07:51:32 +0200 Subject: [PATCH] add attribute --- .../Fixture/include_depends_attribute.php.inc | 43 +++++++++++++++++++ .../Class_/AddParamTypeFromDependsRector.php | 39 ++++++++++++----- 2 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector/Fixture/include_depends_attribute.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector/Fixture/include_depends_attribute.php.inc b/rules-tests/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector/Fixture/include_depends_attribute.php.inc new file mode 100644 index 00000000..88adba84 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector/Fixture/include_depends_attribute.php.inc @@ -0,0 +1,43 @@ + +----- + diff --git a/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php b/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php index e57f8925..947f94d0 100644 --- a/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php +++ b/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php @@ -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; @@ -23,6 +27,7 @@ final class AddParamTypeFromDependsRector extends AbstractRector public function __construct( private readonly TestsNodeAnalyzer $testsNodeAnalyzer, private readonly PhpDocInfoFactory $phpDocInfoFactory, + private readonly AttrinationFinder $attrinationFinder ) { } @@ -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; @@ -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, '()'); } }