2020-01-14 13:09:38 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\PHPStanStaticTypeMapper;
|
2020-01-14 13:09:38 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\ComplexType;
|
2022-12-20 20:40:25 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\Type;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Exception\NotImplementedYetException;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\Webmozart\Assert\Assert;
|
2020-01-14 13:09:38 +00:00
|
|
|
final class PHPStanStaticTypeMapper
|
|
|
|
{
|
2020-01-14 17:59:15 +00:00
|
|
|
/**
|
2021-12-30 16:07:15 +00:00
|
|
|
* @var TypeMapperInterface[]
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2020-01-14 17:59:15 +00:00
|
|
|
*/
|
2021-05-12 13:15:45 +00:00
|
|
|
private $typeMappers;
|
2020-01-14 17:59:15 +00:00
|
|
|
/**
|
|
|
|
* @param TypeMapperInterface[] $typeMappers
|
|
|
|
*/
|
2023-08-15 22:27:51 +00:00
|
|
|
public function __construct(array $typeMappers)
|
2020-01-14 13:09:38 +00:00
|
|
|
{
|
2020-01-14 17:59:15 +00:00
|
|
|
$this->typeMappers = $typeMappers;
|
2023-08-08 16:32:19 +00:00
|
|
|
Assert::notEmpty($typeMappers);
|
2020-01-14 13:09:38 +00:00
|
|
|
}
|
2023-06-16 03:34:15 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
|
2020-01-14 13:09:38 +00:00
|
|
|
{
|
2020-01-14 17:59:15 +00:00
|
|
|
foreach ($this->typeMappers as $typeMapper) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\is_a($type, $typeMapper->getNodeClass(), \true)) {
|
2020-01-14 17:59:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-16 03:34:15 +00:00
|
|
|
return $typeMapper->mapToPHPStanPhpDocTypeNode($type);
|
2020-01-14 13:09:38 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new NotImplementedYetException(__METHOD__ . ' for ' . \get_class($type));
|
2020-01-14 13:09:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-29 22:41:07 +00:00
|
|
|
* @param TypeKind::* $typeKind
|
2022-12-20 20:40:25 +00:00
|
|
|
* @return \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|null
|
2020-01-14 13:09:38 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function mapToPhpParserNode(Type $type, string $typeKind)
|
2020-01-14 13:09:38 +00:00
|
|
|
{
|
2020-01-14 17:59:15 +00:00
|
|
|
foreach ($this->typeMappers as $typeMapper) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\is_a($type, $typeMapper->getNodeClass(), \true)) {
|
2020-01-14 17:59:15 +00:00
|
|
|
continue;
|
2020-01-14 13:09:38 +00:00
|
|
|
}
|
2021-07-04 13:13:54 +00:00
|
|
|
return $typeMapper->mapToPhpParserNode($type, $typeKind);
|
2020-01-14 13:09:38 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new NotImplementedYetException(__METHOD__ . ' for ' . \get_class($type));
|
2020-01-14 13:09:38 +00:00
|
|
|
}
|
|
|
|
}
|