improve original format in CallableTypeNode

This commit is contained in:
TomasVotruba 2020-02-13 11:32:49 +01:00
parent f8446a60b0
commit daba8df475

View File

@ -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;
}
}