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
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2022-12-20 20:40:25 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\StringType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<StringType>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class StringTypeMapper implements TypeMapperInterface
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\Php\PhpVersionProvider
|
2020-01-14 17:59:15 +00:00
|
|
|
*/
|
|
|
|
private $phpVersionProvider;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(PhpVersionProvider $phpVersionProvider)
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
|
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
|
|
}
|
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 StringType::class;
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param StringType $type
|
2020-01-14 17:59:15 +00:00
|
|
|
*/
|
2023-06-16 03:34:15 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new IdentifierTypeNode('string');
|
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 (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
|
2020-01-14 17:59:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-20 20:40:25 +00:00
|
|
|
return new Identifier('string');
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|