rector/src/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php
Tomas Votruba 695c190be3 Updated Rector to commit 94b5561ca87ee6825a098c7c506b774582bf3354
94b5561ca8 chore: bump min version of github actions (fix deprecations) (#5675)
2024-03-01 20:02:28 +00:00

55 lines
1.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
use Rector\Exception\NotImplementedYetException;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use RectorPrefix202403\Webmozart\Assert\Assert;
final class PHPStanStaticTypeMapper
{
/**
* @var TypeMapperInterface[]
* @readonly
*/
private $typeMappers;
/**
* @param TypeMapperInterface[] $typeMappers
*/
public function __construct(array $typeMappers)
{
$this->typeMappers = $typeMappers;
Assert::notEmpty($typeMappers);
}
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
{
foreach ($this->typeMappers as $typeMapper) {
if (!\is_a($type, $typeMapper->getNodeClass(), \true)) {
continue;
}
return $typeMapper->mapToPHPStanPhpDocTypeNode($type);
}
throw new NotImplementedYetException(__METHOD__ . ' for ' . \get_class($type));
}
/**
* @param TypeKind::* $typeKind
* @return \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|null
*/
public function mapToPhpParserNode(Type $type, string $typeKind)
{
foreach ($this->typeMappers as $typeMapper) {
if (!\is_a($type, $typeMapper->getNodeClass(), \true)) {
continue;
}
return $typeMapper->mapToPhpParserNode($type, $typeKind);
}
throw new NotImplementedYetException(__METHOD__ . ' for ' . \get_class($type));
}
}