mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-28 05:33:30 +00:00
77 lines
2.2 KiB
PHP
77 lines
2.2 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\ObjectWithoutClassType;
|
|
use PHPStan\Type\Type;
|
|
use PHPStan\Type\VerbosityLevel;
|
|
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareIdentifierTypeNode;
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\PHPStanStaticTypeMapper\Contract\PHPStanStaticTypeMapperAwareInterface;
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
|
|
|
final class ObjectWithoutClassTypeMapper implements TypeMapperInterface, PHPStanStaticTypeMapperAwareInterface
|
|
{
|
|
/**
|
|
* @var PhpVersionProvider
|
|
*/
|
|
private $phpVersionProvider;
|
|
|
|
/**
|
|
* @var PHPStanStaticTypeMapper
|
|
*/
|
|
private $phpStanStaticTypeMapper;
|
|
|
|
public function __construct(PhpVersionProvider $phpVersionProvider)
|
|
{
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
}
|
|
|
|
public function getNodeClass(): string
|
|
{
|
|
return ObjectWithoutClassType::class;
|
|
}
|
|
|
|
/**
|
|
* @param ObjectWithoutClassType $type
|
|
*/
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
|
{
|
|
return new AttributeAwareIdentifierTypeNode('object');
|
|
}
|
|
|
|
/**
|
|
* @param ObjectWithoutClassType $type
|
|
*/
|
|
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
|
|
{
|
|
$subtractedType = $type->getSubtractedType();
|
|
if ($subtractedType !== null) {
|
|
return $this->phpStanStaticTypeMapper->mapToPhpParserNode($subtractedType);
|
|
}
|
|
|
|
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::OBJECT_TYPE)) {
|
|
return null;
|
|
}
|
|
|
|
return new Name('object');
|
|
}
|
|
|
|
public function mapToDocString(Type $type, ?Type $parentType = null): string
|
|
{
|
|
return $type->describe(VerbosityLevel::typeOnly());
|
|
}
|
|
|
|
public function setPHPStanStaticTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
|
|
{
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
}
|
|
}
|