rector/packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2020-01-15 02:16:22 +00:00
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
2020-01-15 02:16:22 +00:00
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
final class IntersectionTypeMapper implements TypeMapperInterface
{
/**
* @var PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @required
*/
public function autowireIntersectionTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
/**
* @return class-string<Type>
*/
2020-01-15 02:16:22 +00:00
public function getNodeClass(): string
{
return IntersectionType::class;
}
/**
* @param IntersectionType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
$intersectionTypesNodes = [];
foreach ($type->getTypes() as $intersectionedType) {
$intersectionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($intersectionedType);
2020-01-15 02:16:22 +00:00
}
$intersectionTypesNodes = array_unique($intersectionTypesNodes);
return new BracketsAwareIntersectionTypeNode($intersectionTypesNodes);
2020-01-15 02:16:22 +00:00
}
/**
* @param IntersectionType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
2020-01-22 00:44:45 +00:00
// intersection types in PHP are not yet supported
return null;
2020-01-15 02:16:22 +00:00
}
}