2020-01-14 17:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
2020-01-14 17:59:15 +00:00
|
|
|
|
2023-02-02 09:24:12 +00:00
|
|
|
use RectorPrefix202302\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\Generic\GenericObjectType;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedGenericObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
|
2023-02-02 09:24:12 +00:00
|
|
|
use RectorPrefix202302\Symfony\Contracts\Service\Attribute\Required;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<ObjectType>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ObjectTypeMapper implements TypeMapperInterface
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2020-08-12 23:04:23 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
2020-08-12 23:04:23 +00:00
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
2021-03-06 21:16:18 +00:00
|
|
|
/**
|
|
|
|
* @return class-string<Type>
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeClass() : string
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return ObjectType::class;
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ObjectType $type
|
2020-01-14 17:59:15 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof ShortenedObjectType) {
|
|
|
|
return new IdentifierTypeNode($type->getClassName());
|
2020-02-02 18:15:36 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof AliasedObjectType) {
|
|
|
|
return new IdentifierTypeNode($type->getClassName());
|
2020-02-02 18:15:36 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof GenericObjectType) {
|
2021-07-04 13:13:54 +00:00
|
|
|
return $this->mapGenericObjectType($type, $typeKind);
|
2020-08-12 23:04:23 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof NonExistingObjectType) {
|
2021-12-30 16:07:15 +00:00
|
|
|
// possibly generic type
|
2022-06-07 08:22:29 +00:00
|
|
|
return new IdentifierTypeNode($type->getClassName());
|
2021-12-30 16:07:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof FullyQualifiedObjectType && \strncmp($type->getClassName(), '\\', \strlen('\\')) === 0) {
|
|
|
|
return new IdentifierTypeNode($type->getClassName());
|
2022-02-24 13:30:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new IdentifierTypeNode('\\' . $type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ObjectType $type
|
2020-01-14 17:59:15 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof SelfObjectType) {
|
|
|
|
return new Name('self');
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof ShortenedObjectType) {
|
|
|
|
return new FullyQualified($type->getFullyQualifiedName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof AliasedObjectType) {
|
|
|
|
return new Name($type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof FullyQualifiedObjectType) {
|
2022-02-24 13:30:15 +00:00
|
|
|
$className = $type->getClassName();
|
|
|
|
if (\strncmp($className, '\\', \strlen('\\')) === 0) {
|
|
|
|
// skip leading \
|
2022-06-07 08:22:29 +00:00
|
|
|
return new FullyQualified(Strings::substring($className, 1));
|
2022-02-24 13:30:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new FullyQualified($className);
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2022-10-14 09:48:08 +00:00
|
|
|
if ($type instanceof NonExistingObjectType) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$type instanceof GenericObjectType) {
|
2021-02-21 09:32:45 +00:00
|
|
|
// fallback
|
2022-06-07 08:22:29 +00:00
|
|
|
return new FullyQualified($type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-03-08 14:52:36 +00:00
|
|
|
if ($type->getClassName() === 'iterable') {
|
|
|
|
// fallback
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Name('iterable');
|
2021-03-08 14:52:36 +00:00
|
|
|
}
|
2021-02-21 09:32:45 +00:00
|
|
|
if ($type->getClassName() !== 'object') {
|
|
|
|
// fallback
|
2022-06-07 08:22:29 +00:00
|
|
|
return new FullyQualified($type->getClassName());
|
2021-02-21 09:32:45 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Name('object');
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-05-12 13:15:45 +00:00
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
2020-08-12 23:04:23 +00:00
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
2022-05-29 22:41:07 +00:00
|
|
|
/**
|
|
|
|
* @param TypeKind::* $typeKind
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function mapGenericObjectType(GenericObjectType $genericObjectType, string $typeKind) : TypeNode
|
2021-03-13 15:15:10 +00:00
|
|
|
{
|
|
|
|
$name = $this->resolveGenericObjectTypeName($genericObjectType);
|
2022-06-07 08:22:29 +00:00
|
|
|
$identifierTypeNode = new IdentifierTypeNode($name);
|
2021-03-13 15:15:10 +00:00
|
|
|
$genericTypeNodes = [];
|
|
|
|
foreach ($genericObjectType->getTypes() as $key => $genericType) {
|
|
|
|
// mixed type on 1st item in iterator has no value
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($name === 'Iterator' && $genericType instanceof MixedType && $key === 0) {
|
2021-03-13 15:15:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-04 13:13:54 +00:00
|
|
|
$typeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericType, $typeKind);
|
2021-03-13 15:15:10 +00:00
|
|
|
$genericTypeNodes[] = $typeNode;
|
|
|
|
}
|
2021-03-19 14:33:58 +00:00
|
|
|
if ($genericTypeNodes === []) {
|
|
|
|
return $identifierTypeNode;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new GenericTypeNode($identifierTypeNode, $genericTypeNodes);
|
2021-03-13 15:15:10 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveGenericObjectTypeName(GenericObjectType $genericObjectType) : string
|
2021-03-13 15:15:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($genericObjectType instanceof FullyQualifiedGenericObjectType) {
|
2021-03-13 15:15:10 +00:00
|
|
|
return '\\' . $genericObjectType->getClassName();
|
|
|
|
}
|
2021-05-29 22:10:59 +00:00
|
|
|
if (\strpos($genericObjectType->getClassName(), '\\') !== \false) {
|
2021-03-13 15:15:10 +00:00
|
|
|
return '\\' . $genericObjectType->getClassName();
|
|
|
|
}
|
|
|
|
return $genericObjectType->getClassName();
|
|
|
|
}
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|