rector/packages/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php

58 lines
2.0 KiB
PHP
Raw Normal View History

2020-01-15 02:16:22 +00:00
<?php
declare (strict_types=1);
2020-01-15 02:16:22 +00:00
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;
use RectorPrefix20210703\Symfony\Contracts\Service\Attribute\Required;
/**
* @implements TypeMapperInterface<IntersectionType>
*/
final class IntersectionTypeMapper implements \Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface
2020-01-15 02:16:22 +00:00
{
/**
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
2020-01-15 02:16:22 +00:00
*/
private $phpStanStaticTypeMapper;
/**
* @required
*/
public function autowireIntersectionTypeMapper(\Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
2020-01-15 02:16:22 +00:00
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
/**
* @return class-string<Type>
*/
public function getNodeClass() : string
2020-01-15 02:16:22 +00:00
{
return \PHPStan\Type\IntersectionType::class;
2020-01-15 02:16:22 +00:00
}
/**
* @param IntersectionType $type
*/
public function mapToPHPStanPhpDocTypeNode(\PHPStan\Type\Type $type, ?string $kind = null) : \PHPStan\PhpDocParser\Ast\Type\TypeNode
2020-01-15 02:16:22 +00:00
{
$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 \Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode($intersectionTypesNodes);
2020-01-15 02:16:22 +00:00
}
/**
* @param IntersectionType $type
*/
public function mapToPhpParserNode(\PHPStan\Type\Type $type, ?string $kind = null) : ?\PhpParser\Node
2020-01-15 02:16:22 +00:00
{
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
}
}