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;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
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-02-02 09:24:12 +00:00
|
|
|
use RectorPrefix202302\Symfony\Contracts\Service\Attribute\Required;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<ClosureType>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ClosureTypeMapper implements TypeMapperInterface
|
2020-01-14 21:06:21 +00:00
|
|
|
{
|
2020-12-29 21:22:48 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
2020-12-29 21:22:48 +00:00
|
|
|
*/
|
|
|
|
private $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 ClosureType::class;
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClosureType $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
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$identifierTypeNode = new IdentifierTypeNode($type->getClassName());
|
2021-07-04 13:13:54 +00:00
|
|
|
$returnDocTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType(), $typeKind);
|
2022-09-12 07:25:54 +00:00
|
|
|
$parameterDocTypeNodes = [];
|
|
|
|
foreach ($type->getParameters() as $parameterReflection) {
|
|
|
|
$parameterDocTypeNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($parameterReflection->getType(), $typeKind);
|
|
|
|
}
|
|
|
|
return new SpacingAwareCallableTypeNode($identifierTypeNode, $parameterDocTypeNodes, $returnDocTypeNode);
|
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 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) {
|
2021-06-30 11:36:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new FullyQualified('Closure');
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
2021-05-12 13:15:45 +00:00
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
2020-12-29 21:22:48 +00:00
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|