2022-10-29 10:54:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
2022-12-20 20:40:25 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-10-29 10:54:11 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\Generic\GenericClassStringType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Symfony\Contracts\Service\Attribute\Required;
|
2022-10-29 10:54:11 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<GenericClassStringType>
|
|
|
|
*/
|
|
|
|
final class GenericClassStringTypeMapper implements TypeMapperInterface
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-10-29 10:54:11 +00:00
|
|
|
* @var \Rector\Core\Php\PhpVersionProvider
|
|
|
|
*/
|
|
|
|
private $phpVersionProvider;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
2022-10-29 10:54:11 +00:00
|
|
|
public function __construct(PhpVersionProvider $phpVersionProvider)
|
|
|
|
{
|
|
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
|
|
|
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return class-string<Type>
|
|
|
|
*/
|
|
|
|
public function getNodeClass() : string
|
|
|
|
{
|
|
|
|
return GenericClassStringType::class;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param GenericClassStringType $type
|
|
|
|
*/
|
2023-06-16 03:34:15 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
|
2022-10-29 10:54:11 +00:00
|
|
|
{
|
|
|
|
$attributeAwareIdentifierTypeNode = new IdentifierTypeNode('class-string');
|
|
|
|
$genericType = $this->resolveGenericObjectType($type);
|
2023-06-16 03:34:15 +00:00
|
|
|
$genericTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericType);
|
2022-10-29 10:54:11 +00:00
|
|
|
return new GenericTypeNode($attributeAwareIdentifierTypeNode, [$genericTypeNode]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param GenericClassStringType $type
|
|
|
|
*/
|
|
|
|
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
|
|
|
|
{
|
|
|
|
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-12-20 20:40:25 +00:00
|
|
|
return new Identifier('string');
|
2022-10-29 10:54:11 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return \PHPStan\Type\ObjectType|\PHPStan\Type\Type
|
|
|
|
*/
|
|
|
|
private function resolveGenericObjectType(GenericClassStringType $genericClassStringType)
|
|
|
|
{
|
|
|
|
$genericType = $genericClassStringType->getGenericType();
|
|
|
|
if (!$genericType instanceof ObjectType) {
|
|
|
|
return $genericType;
|
|
|
|
}
|
|
|
|
$className = $genericType->getClassName();
|
|
|
|
$className = $this->normalizeType($className);
|
|
|
|
return new ObjectType($className);
|
|
|
|
}
|
|
|
|
private function normalizeType(string $classType) : string
|
|
|
|
{
|
|
|
|
if (\is_a($classType, Expr::class, \true)) {
|
|
|
|
return Expr::class;
|
|
|
|
}
|
|
|
|
if (\is_a($classType, Node::class, \true)) {
|
|
|
|
return Node::class;
|
|
|
|
}
|
|
|
|
return $classType;
|
|
|
|
}
|
|
|
|
}
|