2020-01-14 21:06:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-05-10 21:02:46 +00:00
|
|
|
use PhpParser\Node\Name;
|
2020-02-02 18:15:36 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
2020-01-14 21:06:21 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\CallableType;
|
2021-01-22 00:17:43 +00:00
|
|
|
use PHPStan\Type\ClosureType;
|
2020-01-14 21:06:21 +00:00
|
|
|
use PHPStan\Type\Type;
|
2020-01-15 02:16:22 +00:00
|
|
|
use PHPStan\Type\VerbosityLevel;
|
2020-02-02 18:15:36 +00:00
|
|
|
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareCallableTypeNode;
|
2020-01-14 21:06:21 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
2020-02-02 18:15:36 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2020-01-14 21:06:21 +00:00
|
|
|
|
|
|
|
final class CallableTypeMapper implements TypeMapperInterface
|
|
|
|
{
|
2020-02-02 18:15:36 +00:00
|
|
|
/**
|
|
|
|
* @var PHPStanStaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2020-10-02 10:29:18 +00:00
|
|
|
public function autowireCallableTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
|
2020-02-02 18:15:36 +00:00
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
|
|
|
|
2020-01-14 21:06:21 +00:00
|
|
|
public function getNodeClass(): string
|
|
|
|
{
|
|
|
|
return CallableType::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CallableType $type
|
|
|
|
*/
|
|
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
|
|
|
{
|
2020-02-02 18:15:36 +00:00
|
|
|
$returnTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType());
|
|
|
|
|
|
|
|
return new AttributeAwareCallableTypeNode(new IdentifierTypeNode('callable'), [], $returnTypeNode);
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-22 00:17:43 +00:00
|
|
|
* @param CallableType|ClosureType $type
|
2020-01-14 21:06:21 +00:00
|
|
|
*/
|
|
|
|
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
|
|
|
|
{
|
|
|
|
if ($kind === 'property') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:02:46 +00:00
|
|
|
return new Name('callable');
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|
2020-01-15 02:16:22 +00:00
|
|
|
|
|
|
|
public function mapToDocString(Type $type, ?Type $parentType = null): string
|
|
|
|
{
|
|
|
|
return $type->describe(VerbosityLevel::typeOnly());
|
|
|
|
}
|
2020-01-14 21:06:21 +00:00
|
|
|
}
|