rector/packages/StaticTypeMapper/PhpDocParser/NullableTypeMapper.php
Tomas Votruba 00c000da8f Updated Rector to commit d6b984564dc6ea97b294e54ebb4bfdad76877063
d6b984564d [Php80] Handle crash on nullable scalar with mixed param on MixedTypeRector (#2977)
2022-10-05 15:34:16 +00:00

57 lines
1.9 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\StaticTypeMapper\PhpDocParser;
use PhpParser\Node;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
/**
* @implements PhpDocTypeMapperInterface<NullableTypeNode>
*/
final class NullableTypeMapper implements PhpDocTypeMapperInterface
{
/**
* @readonly
* @var \Rector\StaticTypeMapper\PhpDocParser\IdentifierTypeMapper
*/
private $identifierTypeMapper;
/**
* @readonly
* @var \PHPStan\PhpDoc\TypeNodeResolver
*/
private $typeNodeResolver;
public function __construct(\Rector\StaticTypeMapper\PhpDocParser\IdentifierTypeMapper $identifierTypeMapper, TypeNodeResolver $typeNodeResolver)
{
$this->identifierTypeMapper = $identifierTypeMapper;
$this->typeNodeResolver = $typeNodeResolver;
}
public function getNodeType() : string
{
return NullableTypeNode::class;
}
/**
* @param NullableTypeNode $typeNode
*/
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope) : Type
{
if ($typeNode->type instanceof IdentifierTypeNode) {
$type = $this->identifierTypeMapper->mapToPHPStanType($typeNode->type, $node, $nameScope);
if ($type instanceof UnionType) {
$item1Unpacked = $type->getTypes();
return new UnionType(\array_merge([new NullType()], $item1Unpacked));
}
return new UnionType([new NullType(), $type]);
}
// fallback to PHPStan resolver
return $this->typeNodeResolver->resolve($typeNode, $nameScope);
}
}