mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-30 22:53:31 +00:00
added $kind
param to TypeMapperInterface->mapToPHPStanPhpDocTypeNode()
(#306)
This commit is contained in:
parent
0b717e1df4
commit
7fde3652ed
|
@ -10,6 +10,7 @@ use PhpParser\Node\NullableType;
|
|||
use PhpParser\Node\UnionType;
|
||||
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
||||
|
||||
interface TypeMapperInterface
|
||||
{
|
||||
|
@ -18,9 +19,14 @@ interface TypeMapperInterface
|
|||
*/
|
||||
public function getNodeClass(): string;
|
||||
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode;
|
||||
/**
|
||||
* @param TypeKind::*|null $kind
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode;
|
||||
|
||||
/**
|
||||
* @param TypeKind::*|null $kind
|
||||
*
|
||||
* @return Name|NullableType|UnionType|null
|
||||
*/
|
||||
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node;
|
||||
|
|
|
@ -11,6 +11,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Exception\NotImplementedYetException;
|
||||
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
||||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
||||
|
||||
final class PHPStanStaticTypeMapper
|
||||
{
|
||||
|
@ -22,19 +23,25 @@ final class PHPStanStaticTypeMapper
|
|||
) {
|
||||
}
|
||||
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
/**
|
||||
* @param TypeKind::*|null $kind
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
foreach ($this->typeMappers as $typeMapper) {
|
||||
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $typeMapper->mapToPHPStanPhpDocTypeNode($type);
|
||||
return $typeMapper->mapToPHPStanPhpDocTypeNode($type, $kind);
|
||||
}
|
||||
|
||||
throw new NotImplementedYetException(__METHOD__ . ' for ' . $type::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TypeKind::*|null $kind
|
||||
*/
|
||||
public function mapToPhpParserNode(Type $type, ?string $kind = null): Name | NullableType | UnionType | null
|
||||
{
|
||||
foreach ($this->typeMappers as $typeMapper) {
|
||||
|
|
|
@ -67,7 +67,7 @@ final class ArrayTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ArrayType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$itemType = $type->getItemType();
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ final class BooleanTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param BooleanType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
if ($this->isFalseBooleanTypeWithUnion($type)) {
|
||||
return new IdentifierTypeNode('false');
|
||||
|
|
|
@ -14,6 +14,7 @@ use PHPStan\Type\Type;
|
|||
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
|
||||
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
||||
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
||||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
||||
use Symfony\Contracts\Service\Attribute\Required;
|
||||
|
||||
final class CallableTypeMapper implements TypeMapperInterface
|
||||
|
@ -37,7 +38,7 @@ final class CallableTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param CallableType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$returnTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType());
|
||||
|
||||
|
@ -49,7 +50,7 @@ final class CallableTypeMapper implements TypeMapperInterface
|
|||
*/
|
||||
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
|
||||
{
|
||||
if ($kind === 'property') {
|
||||
if ($kind === TypeKind::KIND_PROPERTY) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ final class ClassStringTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ClassStringType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$attributeAwareIdentifierTypeNode = new IdentifierTypeNode('class-string');
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ final class ClosureTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ClosureType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$identifierTypeNode = new IdentifierTypeNode($type->getClassName());
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ final class FloatTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param FloatType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('float');
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ final class HasOffsetTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param HasOffsetType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new ArrayTypeNode(new IdentifierTypeNode('mixed'));
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ final class IntegerTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param IntegerType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('int');
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ final class IntersectionTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param IntersectionType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$intersectionTypesNodes = [];
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ final class IterableTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param IterableType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getItemType());
|
||||
if ($itemTypeNode instanceof UnionTypeNode) {
|
||||
|
|
|
@ -24,7 +24,7 @@ final class MixedTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param MixedType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('mixed');
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|||
use PHPStan\Type\NeverType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
||||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
||||
|
||||
final class NeverTypeMapper implements TypeMapperInterface
|
||||
{
|
||||
|
@ -24,8 +25,11 @@ final class NeverTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param NeverType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
if ($kind === TypeKind::KIND_RETURN) {
|
||||
return new IdentifierTypeNode('never');
|
||||
}
|
||||
return new IdentifierTypeNode('mixed');
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ final class NonEmptyArrayTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param NonEmptyArrayType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new SpacingAwareArrayTypeNode(new IdentifierTypeNode('mixed'));
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ final class NullTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param NullType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('null');
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ final class ObjectTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ObjectType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
if ($type instanceof ShortenedObjectType) {
|
||||
return new IdentifierTypeNode($type->getClassName());
|
||||
|
|
|
@ -38,7 +38,7 @@ final class ObjectWithoutClassTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ObjectWithoutClassType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
if ($type instanceof TemplateObjectWithoutClassType) {
|
||||
$attributeAwareIdentifierTypeNode = new IdentifierTypeNode($type->getName());
|
||||
|
|
|
@ -25,7 +25,7 @@ final class ParentStaticTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ParentStaticType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('parent');
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ final class ResourceTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ResourceType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('resource');
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ final class SelfObjectTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param SelfObjectType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('self');
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ final class StaticTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param StaticType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new ThisTypeNode();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ final class StrictMixedTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param StrictMixedType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode(self::MIXED);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ final class StringTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param StringType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('string');
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ final class ThisTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param ThisType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new ThisTypeNode();
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ final class TypeWithClassNameTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param TypeWithClassName $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode('string-class');
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ final class UnionTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param UnionType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
$unionTypesNodes = [];
|
||||
$skipIterable = $this->shouldSkipIterable($type);
|
||||
|
|
|
@ -13,6 +13,7 @@ use PHPStan\Type\VoidType;
|
|||
use Rector\Core\Php\PhpVersionProvider;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
||||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
||||
|
||||
final class VoidTypeMapper implements TypeMapperInterface
|
||||
{
|
||||
|
@ -37,7 +38,7 @@ final class VoidTypeMapper implements TypeMapperInterface
|
|||
/**
|
||||
* @param VoidType $type
|
||||
*/
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
|
||||
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
|
||||
{
|
||||
return new IdentifierTypeNode(self::VOID);
|
||||
}
|
||||
|
@ -51,7 +52,7 @@ final class VoidTypeMapper implements TypeMapperInterface
|
|||
return null;
|
||||
}
|
||||
|
||||
if (in_array($kind, ['param', 'property'], true)) {
|
||||
if (in_array($kind, [TypeKind::KIND_PARAM, TypeKind::KIND_PROPERTY], true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ use PHPStan\Type\Type;
|
|||
use Rector\Core\Exception\NotImplementedYetException;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
||||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
|
||||
use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper;
|
||||
use Rector\StaticTypeMapper\Naming\NameScopeFactory;
|
||||
use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper;
|
||||
|
@ -41,12 +42,17 @@ final class StaticTypeMapper
|
|||
) {
|
||||
}
|
||||
|
||||
public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): TypeNode
|
||||
/**
|
||||
* @param TypeKind::*|null $kind
|
||||
*/
|
||||
public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType, ?string $kind = null): TypeNode
|
||||
{
|
||||
return $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($phpStanType);
|
||||
return $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($phpStanType, $kind);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TypeKind::*|null $kind
|
||||
*
|
||||
* @return Name|NullableType|PhpParserUnionType|null
|
||||
*/
|
||||
public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, ?string $kind = null): ?Node
|
||||
|
|
Loading…
Reference in New Issue
Block a user