2020-01-14 17:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Identifier;
|
2020-01-14 20:14:35 +00:00
|
|
|
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;
|
2020-02-02 18:15:36 +00:00
|
|
|
use PHPStan\Type\IterableType;
|
2020-01-14 17:59:15 +00:00
|
|
|
use PHPStan\Type\NullType;
|
2021-02-17 15:40:29 +00:00
|
|
|
use PHPStan\Type\ObjectType;
|
2020-01-14 17:59:15 +00:00
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\TypeWithClassName;
|
|
|
|
use PHPStan\Type\UnionType;
|
2020-09-01 17:56:30 +00:00
|
|
|
use PHPStan\Type\VoidType;
|
2020-01-14 17:59:15 +00:00
|
|
|
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
|
2021-01-18 19:23:10 +00:00
|
|
|
use Rector\CodeQuality\Tests\Rector\If_\ExplicitBoolCompareRector\Fixture\Nullable;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2020-01-14 17:59:15 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
2020-03-24 20:25:08 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
|
2020-01-14 17:59:15 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2021-01-18 19:23:10 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\BoolUnionTypeAnalyzer;
|
2020-01-14 20:14:35 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
|
2021-02-17 15:40:29 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower;
|
2021-01-20 11:41:35 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
|
2020-01-14 17:59:15 +00:00
|
|
|
|
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;
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
/**
|
|
|
|
* @var UnionTypeAnalyzer
|
|
|
|
*/
|
|
|
|
private $unionTypeAnalyzer;
|
|
|
|
|
2020-03-24 20:25:08 +00:00
|
|
|
/**
|
|
|
|
* @var DoctrineTypeAnalyzer
|
|
|
|
*/
|
|
|
|
private $doctrineTypeAnalyzer;
|
|
|
|
|
2021-01-18 19:23:10 +00:00
|
|
|
/**
|
|
|
|
* @var BoolUnionTypeAnalyzer
|
|
|
|
*/
|
|
|
|
private $boolUnionTypeAnalyzer;
|
|
|
|
|
2021-02-17 15:40:29 +00:00
|
|
|
/**
|
|
|
|
* @var UnionTypeCommonTypeNarrower
|
|
|
|
*/
|
|
|
|
private $unionTypeCommonTypeNarrower;
|
|
|
|
|
2020-03-24 20:25:08 +00:00
|
|
|
public function __construct(
|
2020-07-26 07:49:22 +00:00
|
|
|
DoctrineTypeAnalyzer $doctrineTypeAnalyzer,
|
2020-03-24 20:25:08 +00:00
|
|
|
PhpVersionProvider $phpVersionProvider,
|
2021-01-18 19:23:10 +00:00
|
|
|
UnionTypeAnalyzer $unionTypeAnalyzer,
|
2021-02-17 15:40:29 +00:00
|
|
|
BoolUnionTypeAnalyzer $boolUnionTypeAnalyzer,
|
|
|
|
UnionTypeCommonTypeNarrower $unionTypeCommonTypeNarrower
|
2020-03-24 20:25:08 +00:00
|
|
|
) {
|
2020-01-14 17:59:15 +00:00
|
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
2020-01-14 20:14:35 +00:00
|
|
|
$this->unionTypeAnalyzer = $unionTypeAnalyzer;
|
2020-03-24 20:25:08 +00:00
|
|
|
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
|
2021-01-18 19:23:10 +00:00
|
|
|
$this->boolUnionTypeAnalyzer = $boolUnionTypeAnalyzer;
|
2021-02-17 15:40:29 +00:00
|
|
|
$this->unionTypeCommonTypeNarrower = $unionTypeCommonTypeNarrower;
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:22 +00:00
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
|
|
|
public function autowireUnionTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
|
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
|
|
|
|
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 = [];
|
2020-02-02 18:15:36 +00:00
|
|
|
$skipIterable = $this->shouldSkipIterable($type);
|
2020-01-14 17:59:15 +00:00
|
|
|
|
|
|
|
foreach ($type->getTypes() as $unionedType) {
|
2020-02-02 18:15:36 +00:00
|
|
|
if ($unionedType instanceof IterableType && $skipIterable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:59:15 +00:00
|
|
|
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
|
|
|
|
}
|
|
|
|
|
|
|
|
$unionTypesNodes = array_unique($unionTypesNodes);
|
|
|
|
|
|
|
|
return new AttributeAwareUnionTypeNode($unionTypesNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UnionType $type
|
|
|
|
*/
|
|
|
|
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
|
|
|
|
{
|
|
|
|
$arrayNode = $this->matchArrayTypes($type);
|
|
|
|
if ($arrayNode !== null) {
|
|
|
|
return $arrayNode;
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:23:10 +00:00
|
|
|
if ($this->boolUnionTypeAnalyzer->isNullableBoolUnionType(
|
|
|
|
$type
|
2021-01-18 21:45:13 +00:00
|
|
|
) && ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
|
2021-01-18 19:23:10 +00:00
|
|
|
return new NullableType(new Name('bool'));
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:59:15 +00:00
|
|
|
// special case for nullable
|
|
|
|
$nullabledType = $this->matchTypeForNullableUnionType($type);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $nullabledType instanceof Type) {
|
2020-01-14 17:59:15 +00:00
|
|
|
// use first unioned type in case of unioned object types
|
|
|
|
return $this->matchTypeForUnionedObjectTypes($type);
|
|
|
|
}
|
|
|
|
|
2020-09-01 17:56:30 +00:00
|
|
|
// void cannot be nullable
|
|
|
|
if ($nullabledType instanceof VoidType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
$nullabledTypeNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($nullabledType);
|
2020-01-14 17:59:15 +00:00
|
|
|
if ($nullabledTypeNode === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
if ($nullabledTypeNode instanceof NullableType) {
|
2020-01-14 17:59:15 +00:00
|
|
|
return $nullabledTypeNode;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
if ($nullabledTypeNode instanceof PhpParserUnionType) {
|
2020-01-14 17:59:15 +00:00
|
|
|
throw new ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new NullableType($nullabledTypeNode);
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:22 +00:00
|
|
|
/**
|
|
|
|
* @param UnionType $type
|
|
|
|
*/
|
|
|
|
public function mapToDocString(Type $type, ?Type $parentType = null): string
|
|
|
|
{
|
|
|
|
$docStrings = [];
|
|
|
|
|
|
|
|
foreach ($type->getTypes() as $unionedType) {
|
|
|
|
$docStrings[] = $this->phpStanStaticTypeMapper->mapToDocString($unionedType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove empty values, e.g. void/iterable
|
|
|
|
$docStrings = array_unique($docStrings);
|
|
|
|
$docStrings = array_filter($docStrings);
|
|
|
|
|
|
|
|
return implode('|', $docStrings);
|
|
|
|
}
|
|
|
|
|
2020-04-26 00:57:47 +00:00
|
|
|
private function shouldSkipIterable(UnionType $unionType): bool
|
|
|
|
{
|
|
|
|
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
|
2020-04-26 00:57:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-19 15:24:53 +00:00
|
|
|
if (! $unionTypeAnalysis->hasIterable()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $unionTypeAnalysis->hasArray();
|
2020-04-26 00:57:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 21:02:46 +00:00
|
|
|
/**
|
|
|
|
* @return Name|NullableType|null
|
|
|
|
*/
|
|
|
|
private function matchArrayTypes(UnionType $unionType): ?Node
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2020-01-14 20:14:35 +00:00
|
|
|
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
|
2020-01-14 17:59:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
$type = $unionTypeAnalysis->hasIterable() ? 'iterable' : 'array';
|
|
|
|
if ($unionTypeAnalysis->isNullableType()) {
|
2020-05-10 21:02:46 +00:00
|
|
|
return new NullableType($type);
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 21:02:46 +00:00
|
|
|
return new Name($type);
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-14 20:14:35 +00:00
|
|
|
* @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) {
|
2020-10-20 18:35:35 +00:00
|
|
|
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
|
2021-01-18 19:23:10 +00:00
|
|
|
// maybe all one type?
|
|
|
|
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
|
|
|
|
return new Name('bool');
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:34:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:59:15 +00:00
|
|
|
return $phpParserUnionType;
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:23:10 +00:00
|
|
|
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
|
|
|
|
return new Name('bool');
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
// the type should be compatible with all other types, e.g. A extends B, B
|
2021-02-17 15:40:29 +00:00
|
|
|
$compatibleObjectType = $this->resolveCompatibleObjectCandidate($unionType);
|
|
|
|
if (! $compatibleObjectType instanceof ObjectType) {
|
2020-01-14 20:14:35 +00:00
|
|
|
return null;
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:40:29 +00:00
|
|
|
return new FullyQualified($compatibleObjectType->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
private function matchPhpParserUnionType(UnionType $unionType): ?PhpParserUnionType
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2020-10-20 18:35:35 +00:00
|
|
|
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
|
2020-01-14 17:59:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$phpParserUnionedTypes = [];
|
2020-01-14 20:14:35 +00:00
|
|
|
|
2020-01-14 17:59:15 +00:00
|
|
|
foreach ($unionType->getTypes() as $unionedType) {
|
2020-12-09 18:38:45 +00:00
|
|
|
// void type is not allowed in union
|
|
|
|
if ($unionedType instanceof VoidType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
/** @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;
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:14:35 +00:00
|
|
|
return new PhpParserUnionType($phpParserUnionedTypes);
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:40:29 +00:00
|
|
|
private function resolveCompatibleObjectCandidate(UnionType $unionType): ?TypeWithClassName
|
2020-01-14 20:14:35 +00:00
|
|
|
{
|
2020-03-24 20:25:08 +00:00
|
|
|
if ($this->doctrineTypeAnalyzer->isDoctrineCollectionWithIterableUnionType($unionType)) {
|
2021-02-17 15:40:29 +00:00
|
|
|
return new ObjectType('Doctrine\Common\Collections\Collection');
|
2020-03-24 20:25:08 +00:00
|
|
|
}
|
2020-01-14 20:14:35 +00:00
|
|
|
|
2021-01-18 19:23:10 +00:00
|
|
|
if (! $this->unionTypeAnalyzer->hasTypeClassNameOnly($unionType)) {
|
2020-03-24 20:25:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-01-14 20:14:35 +00:00
|
|
|
|
2021-02-17 15:40:29 +00:00
|
|
|
$sharedTypeWithClassName = $this->matchTwoObjectTypes($unionType);
|
|
|
|
if ($sharedTypeWithClassName instanceof TypeWithClassName) {
|
|
|
|
return $sharedTypeWithClassName;
|
|
|
|
}
|
2020-01-14 20:14:35 +00:00
|
|
|
|
2021-02-17 15:40:29 +00:00
|
|
|
// find least common denominator
|
|
|
|
$sharedObjectType = $this->unionTypeCommonTypeNarrower->narrowToSharedObjectType($unionType);
|
|
|
|
if ($sharedObjectType instanceof ObjectType) {
|
|
|
|
return $sharedObjectType;
|
2020-01-14 20:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-01 16:04:38 +00:00
|
|
|
|
2020-04-26 00:57:47 +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);
|
|
|
|
}
|
2021-02-17 15:40:29 +00:00
|
|
|
|
|
|
|
private function matchTwoObjectTypes(UnionType $unionType): ?TypeWithClassName
|
|
|
|
{
|
|
|
|
/** @var TypeWithClassName $unionedType */
|
|
|
|
foreach ($unionType->getTypes() as $unionedType) {
|
|
|
|
/** @var TypeWithClassName $nestedUnionedType */
|
|
|
|
foreach ($unionType->getTypes() as $nestedUnionedType) {
|
|
|
|
if (! $this->areTypeWithClassNamesRelated($unionedType, $nestedUnionedType)) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $unionedType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|