2021-11-14 12:07:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\StaticTypeMapper\PhpDocParser;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PHPStan\Analyser\NameScope;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\IntersectionType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
|
|
|
|
use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper;
|
2021-12-10 00:25:09 +00:00
|
|
|
use RectorPrefix20211210\Symfony\Contracts\Service\Attribute\Required;
|
2021-11-28 18:05:13 +00:00
|
|
|
/**
|
|
|
|
* @implements PhpDocTypeMapperInterface<IntersectionTypeNode>
|
|
|
|
*/
|
2021-11-14 12:07:09 +00:00
|
|
|
final class IntersectionTypeMapper implements \Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper
|
|
|
|
*/
|
|
|
|
private $phpDocTypeMapper;
|
|
|
|
public function getNodeType() : string
|
|
|
|
{
|
|
|
|
return \PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode::class;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2021-11-28 19:07:44 +00:00
|
|
|
public function autowire(\Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper $phpDocTypeMapper) : void
|
2021-11-14 12:07:09 +00:00
|
|
|
{
|
|
|
|
$this->phpDocTypeMapper = $phpDocTypeMapper;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param \PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode
|
|
|
|
* @param \PhpParser\Node $node
|
|
|
|
* @param \PHPStan\Analyser\NameScope $nameScope
|
|
|
|
*/
|
|
|
|
public function mapToPHPStanType($typeNode, $node, $nameScope) : \PHPStan\Type\Type
|
|
|
|
{
|
|
|
|
$intersectionedTypes = [];
|
|
|
|
foreach ($typeNode->types as $intersectionedTypeNode) {
|
|
|
|
$intersectionedTypes[] = $this->phpDocTypeMapper->mapToPHPStanType($intersectionedTypeNode, $node, $nameScope);
|
|
|
|
}
|
|
|
|
return new \PHPStan\Type\IntersectionType($intersectionedTypes);
|
|
|
|
}
|
|
|
|
}
|