mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-28 05:33:30 +00:00
72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Name;
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
use PHPStan\Type\StaticType;
|
|
use PHPStan\Type\ThisType;
|
|
use PHPStan\Type\Type;
|
|
use PHPStan\Type\VerbosityLevel;
|
|
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareThisTypeNode;
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
|
|
/**
|
|
* @see \Rector\NodeTypeResolver\Tests\StaticTypeMapper\StaticTypeMapperTest
|
|
*/
|
|
final class StaticTypeMapper implements TypeMapperInterface
|
|
{
|
|
/**
|
|
* @var PhpVersionProvider
|
|
*/
|
|
private $phpVersionProvider;
|
|
|
|
public function __construct(PhpVersionProvider $phpVersionProvider)
|
|
{
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
}
|
|
|
|
public function getNodeClass(): string
|
|
{
|
|
return StaticType::class;
|
|
}
|
|
|
|
/**
|
|
* @param StaticType $type
|
|
*/
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
|
{
|
|
return new AttributeAwareThisTypeNode();
|
|
}
|
|
|
|
/**
|
|
* @param StaticType $type
|
|
*/
|
|
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
|
|
{
|
|
if ($type instanceof ThisType) {
|
|
// @todo wait for PHPStan to differentiate between self/static
|
|
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE)) {
|
|
return new Name('static');
|
|
}
|
|
|
|
return new Name('self');
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param StaticType $type
|
|
*/
|
|
public function mapToDocString(Type $type, ?Type $parentType = null): string
|
|
{
|
|
return $type->describe(VerbosityLevel::typeOnly());
|
|
}
|
|
}
|