From daba8df47520608c5bd1dda9dcde63bce9141584 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 13 Feb 2020 11:32:49 +0100 Subject: [PATCH] improve original format in CallableTypeNode --- .../Type/AttributeAwareCallableTypeNode.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php b/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php index dc808a6218e..919ca8102eb 100644 --- a/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php +++ b/packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareCallableTypeNode.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Rector\AttributeAwarePhpDoc\Ast\Type; use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait; use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface; @@ -14,6 +15,28 @@ final class AttributeAwareCallableTypeNode extends CallableTypeNode implements A public function __toString(): string { + // keep original (Psalm?) format, see https://github.com/rectorphp/rector/issues/2841 + if ($this->isExplicitCallable()) { + return $this->createExplicitCallable(); + } + return 'callable'; } + + private function isExplicitCallable(): bool + { + if (! $this->returnType instanceof IdentifierTypeNode) { + return false; + } + + return $this->returnType->name !== 'mixed'; + } + + private function createExplicitCallable(): string + { + /** @var IdentifierTypeNode $returnType */ + $returnType = $this->returnType; + + return $this->identifier->name . '():' . $returnType->name; + } }