PHPStan <=> PHPStan doc <=> string type nodes between all possible formats * @see \Rector\Tests\NodeTypeResolver\StaticTypeMapper\StaticTypeMapperTest */ final class StaticTypeMapper { /** * @readonly * @var \Rector\StaticTypeMapper\Naming\NameScopeFactory */ private $nameScopeFactory; /** * @readonly * @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper */ private $phpStanStaticTypeMapper; /** * @readonly * @var \Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper */ private $phpDocTypeMapper; /** * @readonly * @var \Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper */ private $phpParserNodeMapper; public function __construct(NameScopeFactory $nameScopeFactory, PHPStanStaticTypeMapper $phpStanStaticTypeMapper, PhpDocTypeMapper $phpDocTypeMapper, PhpParserNodeMapper $phpParserNodeMapper) { $this->nameScopeFactory = $nameScopeFactory; $this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper; $this->phpDocTypeMapper = $phpDocTypeMapper; $this->phpParserNodeMapper = $phpParserNodeMapper; } public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType) : TypeNode { return $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($phpStanType); } /** * @param TypeKind::* $typeKind * @return Name|ComplexType|Identifier|null */ public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, string $typeKind) : ?Node { return $this->phpStanStaticTypeMapper->mapToPhpParserNode($phpStanType, $typeKind); } public function mapPhpParserNodePHPStanType(Node $node) : Type { return $this->phpParserNodeMapper->mapToPHPStanType($node); } public function mapPHPStanPhpDocTypeToPHPStanType(PhpDocTagValueNode $phpDocTagValueNode, Node $node) : Type { if ($phpDocTagValueNode instanceof TemplateTagValueNode) { // special case if (!$phpDocTagValueNode->bound instanceof TypeNode) { return new MixedType(); } $nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node); return $this->phpDocTypeMapper->mapToPHPStanType($phpDocTagValueNode->bound, $node, $nameScope); } if ($phpDocTagValueNode instanceof ReturnTagValueNode || $phpDocTagValueNode instanceof ParamTagValueNode || $phpDocTagValueNode instanceof VarTagValueNode || $phpDocTagValueNode instanceof ThrowsTagValueNode) { return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node); } throw new NotImplementedYetException(__METHOD__ . ' for ' . \get_class($phpDocTagValueNode)); } public function mapPHPStanPhpDocTypeNodeToPHPStanType(TypeNode $typeNode, Node $node) : Type { $nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node); return $this->phpDocTypeMapper->mapToPHPStanType($typeNode, $node, $nameScope); } }