rector/packages/phpstan-static-type-mapper/src/TypeMapper/CallableTypeMapper.php

66 lines
1.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
2020-05-10 21:02:46 +00:00
use PhpParser\Node\Name;
2020-02-02 18:15:36 +00:00
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Type;
2020-01-15 02:16:22 +00:00
use PHPStan\Type\VerbosityLevel;
2020-02-02 18:15:36 +00:00
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareCallableTypeNode;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
2020-02-02 18:15:36 +00:00
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
final class CallableTypeMapper implements TypeMapperInterface
{
2020-02-02 18:15:36 +00:00
/**
* @var PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @required
*/
public function autowireCallableTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
2020-02-02 18:15:36 +00:00
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
public function getNodeClass(): string
{
return CallableType::class;
}
/**
* @param CallableType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
2020-02-02 18:15:36 +00:00
$returnTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType());
return new AttributeAwareCallableTypeNode(new IdentifierTypeNode('callable'), [], $returnTypeNode);
}
/**
* @param CallableType|ClosureType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if ($kind === 'property') {
return null;
}
2020-05-10 21:02:46 +00:00
return new Name('callable');
}
2020-01-15 02:16:22 +00:00
public function mapToDocString(Type $type, ?Type $parentType = null): string
{
return $type->describe(VerbosityLevel::typeOnly());
}
}