rector/packages/StaticTypeMapper/PhpDocParser/UnionTypeMapper.php
Tomas Votruba aae549741f Updated Rector to commit 0cb3fd0feb464b4568e07607a05c794637aa2862
0cb3fd0feb [Php73] Handle crash Type Error on JsonThrowOnErrorRector (#4626)
2023-08-01 10:55:14 +00:00

57 lines
1.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\StaticTypeMapper\PhpDocParser;
use PhpParser\Node;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper;
use RectorPrefix202308\Symfony\Contracts\Service\Attribute\Required;
/**
* @implements PhpDocTypeMapperInterface<UnionTypeNode>
*/
final class UnionTypeMapper implements PhpDocTypeMapperInterface
{
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
*/
private $typeFactory;
/**
* @var \Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper
*/
private $phpDocTypeMapper;
public function __construct(TypeFactory $typeFactory)
{
$this->typeFactory = $typeFactory;
}
public function getNodeType() : string
{
return UnionTypeNode::class;
}
/**
* @required
*/
public function autowire(PhpDocTypeMapper $phpDocTypeMapper) : void
{
$this->phpDocTypeMapper = $phpDocTypeMapper;
}
/**
* @param UnionTypeNode $typeNode
*/
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope) : Type
{
$unionedTypes = [];
foreach ($typeNode->types as $unionedTypeNode) {
$unionedTypes[] = $this->phpDocTypeMapper->mapToPHPStanType($unionedTypeNode, $node, $nameScope);
}
// to prevent missing class error, e.g. in tests
return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($unionedTypes);
}
}