mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-30 22:53:31 +00:00
363a910cf7
a7db1ad94d
use scoper 0.15
55 lines
1.6 KiB
PHP
55 lines
1.6 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;
|
|
final class UnionTypeMapper implements PhpDocTypeMapperInterface
|
|
{
|
|
/**
|
|
* @var PhpDocTypeMapper
|
|
*/
|
|
private $phpDocTypeMapper;
|
|
/**
|
|
* @var TypeFactory
|
|
*/
|
|
private $typeFactory;
|
|
public function __construct(TypeFactory $typeFactory)
|
|
{
|
|
$this->typeFactory = $typeFactory;
|
|
}
|
|
/**
|
|
* @return class-string<TypeNode>
|
|
*/
|
|
public function getNodeType() : string
|
|
{
|
|
return UnionTypeNode::class;
|
|
}
|
|
/**
|
|
* @required
|
|
*/
|
|
public function autowireUnionTypeMapper(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);
|
|
}
|
|
}
|