mirror of https://github.com/rectorphp/rector.git
64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?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\ClassStringType;
|
|
use PHPStan\Type\Generic\GenericClassStringType;
|
|
use PHPStan\Type\Type;
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
/**
|
|
* @implements TypeMapperInterface<ClassStringType>
|
|
*/
|
|
final class ClassStringTypeMapper implements TypeMapperInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PHPStanStaticTypeMapper\TypeMapper\GenericClassStringTypeMapper
|
|
*/
|
|
private $genericClassStringTypeMapper;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\Php\PhpVersionProvider
|
|
*/
|
|
private $phpVersionProvider;
|
|
public function __construct(\Rector\PHPStanStaticTypeMapper\TypeMapper\GenericClassStringTypeMapper $genericClassStringTypeMapper, PhpVersionProvider $phpVersionProvider)
|
|
{
|
|
$this->genericClassStringTypeMapper = $genericClassStringTypeMapper;
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
}
|
|
/**
|
|
* @return class-string<Type>
|
|
*/
|
|
public function getNodeClass() : string
|
|
{
|
|
return ClassStringType::class;
|
|
}
|
|
/**
|
|
* @param ClassStringType $type
|
|
*/
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
|
|
{
|
|
if ($type instanceof GenericClassStringType) {
|
|
return $this->genericClassStringTypeMapper->mapToPHPStanPhpDocTypeNode($type);
|
|
}
|
|
return new IdentifierTypeNode('class-string');
|
|
}
|
|
/**
|
|
* @param ClassStringType $type
|
|
*/
|
|
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
|
|
{
|
|
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
|
|
return null;
|
|
}
|
|
return new Identifier('string');
|
|
}
|
|
}
|