diff --git a/packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareMethodTagValueNodeFactory.php b/packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareMethodTagValueNodeFactory.php index f4ac31ecc10..51a66cc894a 100644 --- a/packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareMethodTagValueNodeFactory.php +++ b/packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareMethodTagValueNodeFactory.php @@ -6,12 +6,20 @@ namespace Rector\AttributeAwarePhpDoc\AttributeAwareNodeFactory\PhpDoc; use PHPStan\PhpDocParser\Ast\Node; use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode; +use PHPStan\PhpDocParser\Ast\Type\TypeNode; use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareMethodTagValueNode; +use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeAwareNodeFactoryAwareInterface; use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeNodeAwareFactoryInterface; +use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory; use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface; -final class AttributeAwareMethodTagValueNodeFactory implements AttributeNodeAwareFactoryInterface +final class AttributeAwareMethodTagValueNodeFactory implements AttributeNodeAwareFactoryInterface, AttributeAwareNodeFactoryAwareInterface { + /** + * @var AttributeAwareNodeFactory + */ + private $attributeAwareNodeFactory; + public function getOriginalNodeClass(): string { return MethodTagValueNode::class; @@ -27,12 +35,31 @@ final class AttributeAwareMethodTagValueNodeFactory implements AttributeNodeAwar */ public function create(Node $node): AttributeAwareNodeInterface { + $returnType = $this->createAttributeAwareReturnType($node); + return new AttributeAwareMethodTagValueNode( $node->isStatic, - $node->returnType, + $returnType, $node->methodName, $node->parameters, $node->description ); } + + public function setAttributeAwareNodeFactory(AttributeAwareNodeFactory $attributeAwareNodeFactory): void + { + $this->attributeAwareNodeFactory = $attributeAwareNodeFactory; + } + + /** + * @return TypeNode&AttributeAwareNodeInterface + */ + private function createAttributeAwareReturnType(MethodTagValueNode $methodTagValueNode) + { + if ($methodTagValueNode->returnType !== null) { + return $this->attributeAwareNodeFactory->createFromNode($methodTagValueNode->returnType); + } + + return $methodTagValueNode->returnType; + } } diff --git a/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureBasic/method_union.txt b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureBasic/method_union.txt new file mode 100644 index 00000000000..24f159f354f --- /dev/null +++ b/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureBasic/method_union.txt @@ -0,0 +1,3 @@ +/** + * @method int|bool|null|void main(...$args) + */