added $kind param to TypeMapperInterface->mapToPHPStanPhpDocTypeNode() (#306)

This commit is contained in:
Markus Staab 2021-06-26 22:12:24 +02:00 committed by GitHub
parent 0b717e1df4
commit 7fde3652ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 58 additions and 33 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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();

View File

@ -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');

View File

@ -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;
}

View File

@ -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');

View File

@ -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());

View File

@ -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');
}

View File

@ -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'));
}

View File

@ -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');
}

View File

@ -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 = [];

View File

@ -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) {

View File

@ -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');
}

View File

@ -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');
}

View File

@ -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'));
}

View File

@ -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');
}

View File

@ -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());

View File

@ -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());

View File

@ -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');
}

View File

@ -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');
}

View File

@ -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');
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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');
}

View File

@ -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();
}

View File

@ -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');
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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