add TypeMapper

This commit is contained in:
TomasVotruba 2020-01-14 18:59:15 +01:00
parent 68f06043f9
commit 49b3ca04b8
14 changed files with 758 additions and 128 deletions

View File

@ -325,7 +325,7 @@ final class MyFirstRector extends AbstractRector
public function refactor(Node $node): ?Node
{
// we only care about "set*" method names
if (! $this->isName($node, 'set*')) {
if (! $this->isName($node->name, 'set*')) {
// return null to skip it
return null;
}

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\Contract;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
interface PHPStanStaticTypeMapperAwareInterface
{
public function setPHPStanStaticTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void;
}

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\Contract;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
interface TypeMapperInterface
{
public function getNodeClass(): string;
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode;
/**
* @return Identifier|Name|NullableType|UnionType|null
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node;
}

View File

@ -8,15 +8,15 @@ use Closure;
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;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
@ -34,6 +34,7 @@ use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\Exception\NotImplementedException;
@ -45,6 +46,8 @@ use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PHPStan\Type\ParentStaticType;
use Rector\PHPStan\Type\SelfObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\PHPStanStaticTypeMapper\Contract\PHPStanStaticTypeMapperAwareInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
use Traversable;
@ -55,30 +58,24 @@ final class PHPStanStaticTypeMapper
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
/**
* @var TypeMapperInterface[]
*/
private $typeMappers = [];
/**
* @param TypeMapperInterface[] $typeMappers
*/
public function __construct(PhpVersionProvider $phpVersionProvider, array $typeMappers)
{
$this->phpVersionProvider = $phpVersionProvider;
$this->typeMappers = $typeMappers;
}
public function mapToPHPStanPhpDocTypeNode(Type $phpStanType)
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
if ($phpStanType instanceof MixedType) {
return new IdentifierTypeNode('mixed');
}
if ($phpStanType instanceof UnionType) {
$unionTypesNodes = [];
foreach ($phpStanType->getTypes() as $unionedType) {
$unionTypesNodes[] = $this->mapToPHPStanPhpDocTypeNode($unionedType);
}
$unionTypesNodes = array_unique($unionTypesNodes);
return new AttributeAwareUnionTypeNode($unionTypesNodes);
}
if ($phpStanType instanceof ArrayType || $phpStanType instanceof IterableType) {
$itemTypeNode = $this->mapToPHPStanPhpDocTypeNode($phpStanType->getItemType());
if ($type instanceof ArrayType || $type instanceof IterableType) {
$itemTypeNode = $this->mapToPHPStanPhpDocTypeNode($type->getItemType());
if ($itemTypeNode instanceof UnionTypeNode) {
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
@ -87,39 +84,20 @@ final class PHPStanStaticTypeMapper
return new ArrayTypeNode($itemTypeNode);
}
if ($phpStanType instanceof IntegerType) {
return new IdentifierTypeNode('int');
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
continue;
}
// prevents circular dependency
if ($typeMapper instanceof PHPStanStaticTypeMapperAwareInterface) {
$typeMapper->setPHPStanStaticTypeMapper($this);
}
return $typeMapper->mapToPHPStanPhpDocTypeNode($type);
}
if ($phpStanType instanceof ClassStringType) {
return new IdentifierTypeNode('class-string');
}
if ($phpStanType instanceof StringType) {
return new IdentifierTypeNode('string');
}
if ($phpStanType instanceof BooleanType) {
return new IdentifierTypeNode('bool');
}
if ($phpStanType instanceof FloatType) {
return new IdentifierTypeNode('float');
}
if ($phpStanType instanceof ObjectType) {
return new IdentifierTypeNode('\\' . $phpStanType->getClassName());
}
if ($phpStanType instanceof NullType) {
return new IdentifierTypeNode('null');
}
if ($phpStanType instanceof NeverType) {
return new IdentifierTypeNode('mixed');
}
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($type));
}
/**
@ -144,36 +122,12 @@ final class PHPStanStaticTypeMapper
return new Identifier('self');
}
if ($phpStanType instanceof IntegerType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('int');
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($phpStanType, $typeMapper->getNodeClass(), true)) {
continue;
}
return null;
}
if ($phpStanType instanceof StringType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('string');
}
return null;
}
if ($phpStanType instanceof BooleanType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('bool');
}
return null;
}
if ($phpStanType instanceof FloatType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('float');
}
return null;
return $typeMapper->mapToPhpParserNode($phpStanType);
}
if ($phpStanType instanceof ArrayType) {
@ -204,13 +158,13 @@ final class PHPStanStaticTypeMapper
return new Identifier('callable');
}
if ($phpStanType instanceof ShortenedObjectType) {
return new Name\FullyQualified($phpStanType->getFullyQualifiedName());
}
if ($phpStanType instanceof AliasedObjectType) {
return new Name($phpStanType->getClassName());
}
// if ($phpStanType instanceof ShortenedObjectType) {
// return new FullyQualified($phpStanType->getFullyQualifiedName());
// }
//
// if ($phpStanType instanceof AliasedObjectType) {
// return new Name($phpStanType->getClassName());
// }
if ($phpStanType instanceof TypeWithClassName) {
$lowerCasedClassName = strtolower($phpStanType->getClassName());
@ -230,7 +184,7 @@ final class PHPStanStaticTypeMapper
return null;
}
return new Name\FullyQualified($phpStanType->getClassName());
return new FullyQualified($phpStanType->getClassName());
}
if ($phpStanType instanceof UnionType) {
@ -263,11 +217,8 @@ final class PHPStanStaticTypeMapper
return new NullableType($nullabledTypeNode);
}
if ($phpStanType instanceof NeverType ||
$phpStanType instanceof VoidType ||
$phpStanType instanceof MixedType ||
$phpStanType instanceof ResourceType ||
$phpStanType instanceof NullType
if ($phpStanType instanceof VoidType ||
$phpStanType instanceof ResourceType
) {
return null;
}
@ -331,16 +282,8 @@ final class PHPStanStaticTypeMapper
return '\\' . Closure::class;
}
if ($phpStanType instanceof StringType) {
return 'string';
}
if ($phpStanType instanceof IntegerType) {
return 'int';
}
if ($phpStanType instanceof NullType) {
return 'null';
if ($phpStanType instanceof StringType || $phpStanType instanceof NullType || $phpStanType instanceof IntegerType || $phpStanType instanceof MixedType || $phpStanType instanceof FloatType || $phpStanType instanceof CallableType || $phpStanType instanceof ResourceType) {
return $phpStanType->describe(VerbosityLevel::typeOnly());
}
if ($phpStanType instanceof ArrayType) {
@ -369,16 +312,8 @@ final class PHPStanStaticTypeMapper
return implode('|', $docStringTypes);
}
if ($phpStanType instanceof MixedType) {
return 'mixed';
}
if ($phpStanType instanceof FloatType) {
return 'float';
}
if ($phpStanType instanceof VoidType) {
if ($this->phpVersionProvider->isAtLeast('7.1')) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
// the void type is better done in PHP code
return '';
}
@ -387,12 +322,8 @@ final class PHPStanStaticTypeMapper
return 'void';
}
if ($phpStanType instanceof BooleanType) {
return 'bool';
}
if ($phpStanType instanceof IterableType) {
if ($this->phpVersionProvider->isAtLeast('7.1')) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
// the void type is better done in PHP code
return '';
}
@ -400,18 +331,14 @@ final class PHPStanStaticTypeMapper
return 'iterable';
}
if ($phpStanType instanceof BooleanType) {
return 'bool';
}
if ($phpStanType instanceof NeverType) {
return 'mixed';
}
if ($phpStanType instanceof CallableType) {
return 'callable';
}
if ($phpStanType instanceof ResourceType) {
return 'resource';
}
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
}
@ -456,7 +383,7 @@ final class PHPStanStaticTypeMapper
}
/**
* @return Name|Name\FullyQualified|PhpParserUnionType|null
* @return Name|FullyQualified|PhpParserUnionType|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType): ?Node
{
@ -481,7 +408,7 @@ final class PHPStanStaticTypeMapper
}
}
return new Name\FullyQualified($unionedType->getClassName());
return new FullyQualified($unionedType->getClassName());
}
return null;

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class BooleanTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return BooleanType::class;
}
/**
* @param BooleanType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('bool');
}
/**
* @param BooleanType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('bool');
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class ClassStringTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return ClassStringType::class;
}
/**
* @param ClassStringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('class-string');
}
/**
* @param ClassStringType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\FloatType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class FloatTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return FloatType::class;
}
/**
* @param FloatType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('float');
}
/**
* @param FloatType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('float');
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class IntegerTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return IntegerType::class;
}
/**
* @param IntegerType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('int');
}
/**
* @param IntegerType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('int');
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class MixedTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return MixedType::class;
}
/**
* @param MixedType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('mixed');
}
/**
* @param MixedType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class NeverTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return NeverType::class;
}
/**
* @param NeverType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('mixed');
}
/**
* @param NeverType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class NullTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return NullType::class;
}
/**
* @param NullType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('null');
}
/**
* @param NullType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PHP_CodeSniffer\Reports\Full;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class ObjectTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return ObjectType::class;
}
/**
* @param ObjectType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('\\' . $type->getClassName());
}
/**
* @param ObjectType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if ($type instanceof ShortenedObjectType) {
return new FullyQualified($type->getFullyQualifiedName());
}
if ($type instanceof AliasedObjectType) {
return new Name($type->getClassName());
}
if ($type instanceof FullyQualifiedObjectType) {
return new FullyQualified($type->getClassName());
}
if ($type instanceof GenericObjectType) {
if ($type->getClassName() === 'object') {
return new Identifier('object');
}
return new FullyQualified($type->getClassName());
// if ($type->getClassName()) {
// dump($type);
// die;
// }
//
// if ($type->getClassName() === 'object') {
// dump($type);
// die;
// } else {
// die;
// }
//
// return new Identifier('object');
}
// ...
dump($type->getFullyQualifiedName());
die;
// fallback
return new FullyQualified($type->getFullyQualifiedName());
dump($type);
die;
return null;
}
}

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class StringTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return StringType::class;
}
/**
* @param StringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('string');
}
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('string');
}
}

View File

@ -0,0 +1,222 @@
<?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);
}
}