2020-01-14 17:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
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;
|
2020-01-15 02:16:22 +00:00
|
|
|
use PHPStan\Type\VerbosityLevel;
|
|
|
|
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
|
2020-01-14 17:59:15 +00:00
|
|
|
use Rector\PHPStan\Type\AliasedObjectType;
|
|
|
|
use Rector\PHPStan\Type\FullyQualifiedObjectType;
|
2020-01-15 02:16:22 +00:00
|
|
|
use Rector\PHPStan\Type\SelfObjectType;
|
2020-01-14 17:59:15 +00:00
|
|
|
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
|
|
|
|
{
|
2020-02-02 18:15:36 +00:00
|
|
|
if ($type instanceof ShortenedObjectType) {
|
|
|
|
return new IdentifierTypeNode($type->getClassName());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof AliasedObjectType) {
|
|
|
|
return new IdentifierTypeNode($type->getClassName());
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:59:15 +00:00
|
|
|
return new IdentifierTypeNode('\\' . $type->getClassName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ObjectType $type
|
|
|
|
*/
|
|
|
|
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
|
|
|
|
{
|
2020-01-15 02:16:22 +00:00
|
|
|
if ($type instanceof SelfObjectType) {
|
2020-05-10 21:02:46 +00:00
|
|
|
return new Name('self');
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:59:15 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-01-19 19:45:01 +00:00
|
|
|
if ($type instanceof GenericObjectType && $type->getClassName() === 'object') {
|
2020-05-10 21:02:46 +00:00
|
|
|
return new Name('object');
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2020-01-14 20:14:35 +00:00
|
|
|
return new FullyQualified($type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2020-01-15 02:16:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ObjectType $type
|
|
|
|
*/
|
|
|
|
public function mapToDocString(Type $type, ?Type $parentType = null): string
|
|
|
|
{
|
|
|
|
if ($type instanceof AliasedObjectType) {
|
|
|
|
// no preslash for alias
|
|
|
|
return $type->getClassName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof ShortenedObjectType) {
|
|
|
|
return '\\' . $type->getFullyQualifiedName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof FullyQualifiedObjectType) {
|
|
|
|
// always prefixed with \\
|
|
|
|
return '\\' . $type->getClassName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassExistenceStaticHelper::doesClassLikeExist($type->getClassName())) {
|
|
|
|
// FQN by default
|
|
|
|
return '\\' . $type->describe(VerbosityLevel::typeOnly());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type->getClassName();
|
|
|
|
}
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|