2020-01-14 20:19:41 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-01-14 20:19:41 +00:00
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-05-10 21:02:46 +00:00
|
|
|
use PhpParser\Node\Name;
|
2020-01-15 02:11:11 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
|
2020-01-14 20:19:41 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
2020-01-15 02:11:11 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
|
2020-01-14 20:19:41 +00:00
|
|
|
use PHPStan\Type\IterableType;
|
|
|
|
use PHPStan\Type\Type;
|
2021-03-20 15:27:18 +00:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
|
2020-01-14 20:19:41 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
2021-10-27 19:25:02 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2020-01-15 02:11:11 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2021-12-10 00:25:09 +00:00
|
|
|
use RectorPrefix20211210\Symfony\Contracts\Service\Attribute\Required;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<IterableType>
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class IterableTypeMapper implements \Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface
|
2020-01-14 20:19:41 +00:00
|
|
|
{
|
2020-01-15 02:11:11 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
2020-01-15 02:11:11 +00:00
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2021-11-28 19:07:44 +00:00
|
|
|
public function autowire(\Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
2020-01-15 02:11:11 +00:00
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
2021-03-06 21:16:18 +00:00
|
|
|
/**
|
|
|
|
* @return class-string<Type>
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeClass() : string
|
2020-01-14 20:19:41 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return \PHPStan\Type\IterableType::class;
|
2020-01-14 20:19:41 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PHPStan\Type\Type $type
|
2021-10-27 19:25:02 +00:00
|
|
|
* @param \Rector\PHPStanStaticTypeMapper\Enum\TypeKind $typeKind
|
2020-01-14 20:19:41 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode($type, $typeKind) : \PHPStan\PhpDocParser\Ast\Type\TypeNode
|
2020-01-14 20:19:41 +00:00
|
|
|
{
|
2021-07-04 13:13:54 +00:00
|
|
|
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getItemType(), $typeKind);
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($itemTypeNode instanceof \PHPStan\PhpDocParser\Ast\Type\UnionTypeNode) {
|
2020-01-15 02:11:11 +00:00
|
|
|
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode($itemTypeNode);
|
2020-01-14 20:19:41 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PHPStan\Type\Type $type
|
2021-10-27 19:25:02 +00:00
|
|
|
* @param \Rector\PHPStanStaticTypeMapper\Enum\TypeKind $typeKind
|
2020-01-14 20:19:41 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function mapToPhpParserNode($type, $typeKind) : ?\PhpParser\Node
|
2020-01-14 20:19:41 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Name('iterable');
|
2020-01-14 20:19:41 +00:00
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
private function convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes(\PHPStan\PhpDocParser\Ast\Type\UnionTypeNode $unionTypeNode) : \Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-01-15 02:11:11 +00:00
|
|
|
$unionedArrayType = [];
|
|
|
|
foreach ($unionTypeNode->types as $unionedType) {
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($unionedType instanceof \PHPStan\PhpDocParser\Ast\Type\UnionTypeNode) {
|
2020-01-15 02:11:11 +00:00
|
|
|
foreach ($unionedType->types as $key => $subUnionedType) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$unionedType->types[$key] = new \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode($subUnionedType);
|
2020-01-15 02:11:11 +00:00
|
|
|
}
|
|
|
|
$unionedArrayType[] = $unionedType;
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$unionedArrayType[] = new \PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode($unionedType);
|
2020-01-15 02:11:11 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode($unionedArrayType);
|
2020-01-15 02:11:11 +00:00
|
|
|
}
|
2020-01-14 20:19:41 +00:00
|
|
|
}
|