rector/packages/PHPStanStaticTypeMapper/src/TypeMapper/UnionTypeMapper.php

223 lines
6.4 KiB
PHP
Raw Normal View History

2020-01-14 17:59:15 +00:00
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\Exception\ShouldNotHappenException;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\PHPStanStaticTypeMapperAwareInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\ValueObject\PhpVersionFeature;
use Traversable;
final class UnionTypeMapper implements TypeMapperInterface, PHPStanStaticTypeMapperAwareInterface
{
/**
* @var PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return UnionType::class;
}
/**
* @param UnionType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
$unionTypesNodes = [];
foreach ($type->getTypes() as $unionedType) {
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
}
$unionTypesNodes = array_unique($unionTypesNodes);
return new AttributeAwareUnionTypeNode($unionTypesNodes);
}
public function setPHPStanStaticTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
/**
* @param UnionType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
// match array types
$arrayNode = $this->matchArrayTypes($type);
if ($arrayNode !== null) {
return $arrayNode;
}
// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
if ($nullabledType === null) {
// use first unioned type in case of unioned object types
return $this->matchTypeForUnionedObjectTypes($type);
}
$nullabledTypeNode = $this->mapToPhpParserNode($nullabledType);
if ($nullabledTypeNode === null) {
return null;
}
if ($nullabledTypeNode instanceof Node\NullableType) {
return $nullabledTypeNode;
}
if ($nullabledTypeNode instanceof Node\UnionType) {
throw new ShouldNotHappenException();
}
return new NullableType($nullabledTypeNode);
}
private function matchArrayTypes(UnionType $unionType): ?Identifier
{
$isNullableType = false;
$hasIterable = false;
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof IterableType) {
$hasIterable = true;
continue;
}
if ($unionedType instanceof ArrayType) {
continue;
}
if ($unionedType instanceof NullType) {
$isNullableType = true;
continue;
}
if ($unionedType instanceof ObjectType) {
if ($unionedType->getClassName() === Traversable::class) {
$hasIterable = true;
continue;
}
}
return null;
}
$type = $hasIterable ? 'iterable' : 'array';
if ($isNullableType) {
return new Identifier('?' . $type);
}
return new Identifier($type);
}
private function matchTypeForNullableUnionType(UnionType $unionType): ?Type
{
if (count($unionType->getTypes()) !== 2) {
return null;
}
$firstType = $unionType->getTypes()[0];
$secondType = $unionType->getTypes()[1];
if ($firstType instanceof NullType) {
return $secondType;
}
if ($secondType instanceof NullType) {
return $firstType;
}
return null;
}
/**
* @return Node\Name|Node\Name\FullyQualified|Node\UnionType|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($unionType);
if ($phpParserUnionType !== null) {
return $phpParserUnionType;
}
// do the type should be compatible with all other types, e.g. A extends B, B
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
return null;
}
foreach ($unionType->getTypes() as $nestedUnionedType) {
if (! $nestedUnionedType instanceof TypeWithClassName) {
return null;
}
if (! $this->areTypeWithClassNamesRelated($unionedType, $nestedUnionedType)) {
continue 2;
}
}
return new Node\Name\FullyQualified($unionedType->getClassName());
}
return null;
}
private function matchPhpParserUnionType(UnionType $unionType): ?Node\UnionType
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::UNION_TYPES)) {
return null;
}
$phpParserUnionedTypes = [];
foreach ($unionType->getTypes() as $unionedType) {
/** @var Identifier|Node\Name|null $phpParserNode */
$phpParserNode = $this->mapToPhpParserNode($unionedType);
if ($phpParserNode === null) {
return null;
}
$phpParserUnionedTypes[] = $phpParserNode;
}
return new Node\UnionType($phpParserUnionedTypes);
}
private function areTypeWithClassNamesRelated(TypeWithClassName $firstType, TypeWithClassName $secondType): bool
{
if (is_a($firstType->getClassName(), $secondType->getClassName(), true)) {
return true;
}
return is_a($secondType->getClassName(), $firstType->getClassName(), true);
}
}