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

219 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 PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
2020-01-14 17:59:15 +00:00
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NullType;
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\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
2020-01-14 17:59:15 +00:00
use Rector\ValueObject\PhpVersionFeature;
2020-01-15 02:11:11 +00:00
final class UnionTypeMapper implements TypeMapperInterface
2020-01-14 17:59:15 +00:00
{
/**
* @var PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @var UnionTypeAnalyzer
*/
private $unionTypeAnalyzer;
public function __construct(PhpVersionProvider $phpVersionProvider, UnionTypeAnalyzer $unionTypeAnalyzer)
2020-01-14 17:59:15 +00:00
{
$this->phpVersionProvider = $phpVersionProvider;
$this->unionTypeAnalyzer = $unionTypeAnalyzer;
2020-01-14 17:59:15 +00:00
}
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);
}
/**
* @required
*/
2020-01-15 02:11:11 +00:00
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
2020-01-14 17:59:15 +00:00
{
$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->phpStanStaticTypeMapper->mapToPhpParserNode($nullabledType);
2020-01-14 17:59:15 +00:00
if ($nullabledTypeNode === null) {
return null;
}
if ($nullabledTypeNode instanceof NullableType) {
2020-01-14 17:59:15 +00:00
return $nullabledTypeNode;
}
if ($nullabledTypeNode instanceof PhpParserUnionType) {
2020-01-14 17:59:15 +00:00
throw new ShouldNotHappenException();
}
return new NullableType($nullabledTypeNode);
}
private function matchArrayTypes(UnionType $unionType): ?Identifier
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if ($unionTypeAnalysis === null) {
2020-01-14 17:59:15 +00:00
return null;
}
$type = $unionTypeAnalysis->hasIterable() ? 'iterable' : 'array';
if ($unionTypeAnalysis->isNullableType()) {
2020-01-14 17:59:15 +00:00
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 Name|FullyQualified|PhpParserUnionType|null
2020-01-14 17:59:15 +00:00
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($unionType);
if ($phpParserUnionType !== null) {
return $phpParserUnionType;
}
// the type should be compatible with all other types, e.g. A extends B, B
$compatibleObjectCandidate = $this->resolveCompatibleObjectCandidate($unionType);
if ($compatibleObjectCandidate === null) {
return null;
2020-01-14 17:59:15 +00:00
}
return new FullyQualified($compatibleObjectCandidate);
2020-01-14 17:59:15 +00:00
}
private function matchPhpParserUnionType(UnionType $unionType): ?PhpParserUnionType
2020-01-14 17:59:15 +00:00
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::UNION_TYPES)) {
return null;
}
$phpParserUnionedTypes = [];
2020-01-14 17:59:15 +00:00
foreach ($unionType->getTypes() as $unionedType) {
/** @var Identifier|Name|null $phpParserNode */
$phpParserNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($unionedType);
2020-01-14 17:59:15 +00:00
if ($phpParserNode === null) {
return null;
}
$phpParserUnionedTypes[] = $phpParserNode;
}
return new PhpParserUnionType($phpParserUnionedTypes);
2020-01-14 17:59:15 +00:00
}
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);
}
private function resolveCompatibleObjectCandidate(UnionType $unionType): ?string
{
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 $unionedType->getClassName();
}
return null;
}
2020-01-14 17:59:15 +00:00
}