2020-01-14 21:06:21 +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\TypeMapper;
|
2020-01-14 21:06:21 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-02-13 14:43:04 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\CallableType;
|
|
|
|
use PHPStan\Type\ClosureType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Symfony\Contracts\Service\Attribute\Required;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<CallableType>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class CallableTypeMapper implements TypeMapperInterface
|
2020-01-14 21:06:21 +00:00
|
|
|
{
|
2020-02-02 18:15:36 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
2020-02-02 18:15:36 +00:00
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
2020-02-02 18:15:36 +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 21:06:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return CallableType::class;
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-29 22:41:07 +00:00
|
|
|
* @param TypeKind::* $typeKind
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param CallableType $type
|
2020-01-14 21:06:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
|
2020-01-14 21:06:21 +00:00
|
|
|
{
|
2021-07-04 13:13:54 +00:00
|
|
|
$returnTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType(), $typeKind);
|
2022-06-07 08:22:29 +00:00
|
|
|
return new SpacingAwareCallableTypeNode(new IdentifierTypeNode('callable'), [], $returnTypeNode);
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-29 22:41:07 +00:00
|
|
|
* @param TypeKind::* $typeKind
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param CallableType|ClosureType $type
|
2020-01-14 21:06:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
|
2020-01-14 21:06:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($typeKind === TypeKind::PROPERTY) {
|
2020-01-14 21:06:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-02-13 14:43:04 +00:00
|
|
|
return new Identifier('callable');
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
|
|
|
}
|