diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 748f31e1f19..eadee1f7c9c 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -135,10 +135,8 @@ use Rector\PhpParser\NodeVisitor\SymfonyClosureNodeVisitor; use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface; use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryLiteralStringTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryNonEmptyStringTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryNonFalsyStringTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryNumericStringTypeMapper; +use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryArrayTypeMapper; +use Rector\PHPStanStaticTypeMapper\TypeMapper\AccessoryStringTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ArrayTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\BooleanTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\CallableTypeMapper; @@ -149,19 +147,15 @@ use Rector\PHPStanStaticTypeMapper\TypeMapper\ConstantArrayTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\FloatTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\HasMethodTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\HasOffsetTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\HasOffsetValueTypeTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\HasPropertyTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\IntegerTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\IntersectionTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\IterableTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\MixedTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\NeverTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\NonEmptyArrayTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\NullTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ObjectWithoutClassTypeMapper; -use Rector\PHPStanStaticTypeMapper\TypeMapper\OversizedArrayTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ParentStaticTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\ResourceTypeMapper; use Rector\PHPStanStaticTypeMapper\TypeMapper\StaticTypeMapper; @@ -287,10 +281,8 @@ final class LazyContainerFactory * @var array> */ private const array TYPE_MAPPER_CLASSES = [ - AccessoryLiteralStringTypeMapper::class, - AccessoryNonEmptyStringTypeMapper::class, - AccessoryNonFalsyStringTypeMapper::class, - AccessoryNumericStringTypeMapper::class, + AccessoryArrayTypeMapper::class, + AccessoryStringTypeMapper::class, ConstantArrayTypeMapper::class, ArrayTypeMapper::class, BooleanTypeMapper::class, @@ -301,19 +293,15 @@ final class LazyContainerFactory ConditionalTypeMapper::class, FloatTypeMapper::class, HasMethodTypeMapper::class, - HasOffsetTypeMapper::class, - HasOffsetValueTypeTypeMapper::class, HasPropertyTypeMapper::class, IntegerTypeMapper::class, IntersectionTypeMapper::class, IterableTypeMapper::class, MixedTypeMapper::class, NeverTypeMapper::class, - NonEmptyArrayTypeMapper::class, NullTypeMapper::class, ObjectTypeMapper::class, ObjectWithoutClassTypeMapper::class, - OversizedArrayTypeMapper::class, ParentStaticTypeMapper::class, ResourceTypeMapper::class, StaticTypeMapper::class, diff --git a/src/PHPStanStaticTypeMapper/Contract/TypeMapperInterface.php b/src/PHPStanStaticTypeMapper/Contract/TypeMapperInterface.php index f9b93c09569..36e49ef8019 100644 --- a/src/PHPStanStaticTypeMapper/Contract/TypeMapperInterface.php +++ b/src/PHPStanStaticTypeMapper/Contract/TypeMapperInterface.php @@ -18,9 +18,12 @@ interface TypeMapperInterface { /** - * @return class-string + * A mapper can cover several types at once, when they all map to the same output, + * e.g. every accessory string type maps to "string" + * + * @return array> */ - public function getNodeClass(): string; + public function getNodeClasses(): array; /** * @param TType $type diff --git a/src/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php b/src/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php index c48d970c288..a2a75b7a1d9 100644 --- a/src/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php @@ -28,7 +28,7 @@ public function __construct( public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode { foreach ($this->typeMappers as $typeMapper) { - if (! is_a($type, $typeMapper->getNodeClass(), true)) { + if (! $this->doesTypeMatch($type, $typeMapper)) { continue; } @@ -44,7 +44,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode public function mapToPhpParserNode(Type $type, string $typeKind): Name|ComplexType|Identifier|null { foreach ($this->typeMappers as $typeMapper) { - if (! is_a($type, $typeMapper->getNodeClass(), true)) { + if (! $this->doesTypeMatch($type, $typeMapper)) { continue; } @@ -53,4 +53,12 @@ public function mapToPhpParserNode(Type $type, string $typeKind): Name|ComplexTy throw new NotImplementedYetException(__METHOD__ . ' for ' . $type::class); } + + /** + * @param TypeMapperInterface $typeMapper + */ + private function doesTypeMatch(Type $type, TypeMapperInterface $typeMapper): bool + { + return array_any($typeMapper->getNodeClasses(), fn (string $nodeClass): bool => $type instanceof $nodeClass); + } } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryArrayTypeMapper.php new file mode 100644 index 00000000000..14b073970d3 --- /dev/null +++ b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryArrayTypeMapper.php @@ -0,0 +1,45 @@ + + */ +final class AccessoryArrayTypeMapper implements TypeMapperInterface +{ + /** + * @return array> + */ + public function getNodeClasses(): array + { + return [ + HasOffsetType::class, + HasOffsetValueType::class, + NonEmptyArrayType::class, + OversizedArrayType::class, + ]; + } + + public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode + { + return $type->toPhpDocNode(); + } + + public function mapToPhpParserNode(Type $type, string $typeKind): Identifier + { + return new Identifier('array'); + } +} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryLiteralStringTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryLiteralStringTypeMapper.php deleted file mode 100644 index 3de93d94342..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryLiteralStringTypeMapper.php +++ /dev/null @@ -1,50 +0,0 @@ - - */ -final readonly class AccessoryLiteralStringTypeMapper implements TypeMapperInterface -{ - public function __construct( - private PhpVersionProvider $phpVersionProvider - ) { - } - - public function getNodeClass(): string - { - return AccessoryLiteralStringType::class; - } - - /** - * @param AccessoryLiteralStringType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param AccessoryLiteralStringType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node - { - if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) { - return null; - } - - return new Identifier('string'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNonFalsyStringTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNonFalsyStringTypeMapper.php deleted file mode 100644 index 0b64e264277..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNonFalsyStringTypeMapper.php +++ /dev/null @@ -1,50 +0,0 @@ - - */ -final readonly class AccessoryNonFalsyStringTypeMapper implements TypeMapperInterface -{ - public function __construct( - private PhpVersionProvider $phpVersionProvider - ) { - } - - public function getNodeClass(): string - { - return AccessoryNonFalsyStringType::class; - } - - /** - * @param AccessoryNonFalsyStringType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param AccessoryNonFalsyStringType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node - { - if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) { - return null; - } - - return new Identifier('string'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNumericStringTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNumericStringTypeMapper.php deleted file mode 100644 index f79e8508a7c..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNumericStringTypeMapper.php +++ /dev/null @@ -1,50 +0,0 @@ - - */ -final readonly class AccessoryNumericStringTypeMapper implements TypeMapperInterface -{ - public function __construct( - private PhpVersionProvider $phpVersionProvider - ) { - } - - public function getNodeClass(): string - { - return AccessoryNumericStringType::class; - } - - /** - * @param AccessoryNumericStringType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param AccessoryNumericStringType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): ?Node - { - if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) { - return null; - } - - return new Identifier('string'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNonEmptyStringTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryStringTypeMapper.php similarity index 53% rename from src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNonEmptyStringTypeMapper.php rename to src/PHPStanStaticTypeMapper/TypeMapper/AccessoryStringTypeMapper.php index cf29203d425..1bd48e24b0e 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryNonEmptyStringTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/AccessoryStringTypeMapper.php @@ -7,38 +7,45 @@ use PhpParser\Node; use PhpParser\Node\Identifier; use PHPStan\PhpDocParser\Ast\Type\TypeNode; +use PHPStan\Type\Accessory\AccessoryLiteralStringType; use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; +use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; +use PHPStan\Type\Accessory\AccessoryNumericStringType; use PHPStan\Type\Type; use Rector\Php\PhpVersionProvider; use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface; use Rector\ValueObject\PhpVersionFeature; /** - * @implements TypeMapperInterface + * Every accessory string type narrows "string" with an extra guarantee, so they all map back to "string" + * + * @implements TypeMapperInterface */ -final readonly class AccessoryNonEmptyStringTypeMapper implements TypeMapperInterface +final readonly class AccessoryStringTypeMapper implements TypeMapperInterface { public function __construct( private PhpVersionProvider $phpVersionProvider ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return AccessoryNonEmptyStringType::class; + return [ + AccessoryLiteralStringType::class, + AccessoryNonEmptyStringType::class, + AccessoryNonFalsyStringType::class, + AccessoryNumericStringType::class, + ]; } - /** - * @param AccessoryNonEmptyStringType $type - */ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode { return $type->toPhpDocNode(); } - /** - * @param AccessoryNonEmptyStringType $type - */ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node { if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) { diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php index 43e6b4931b2..871f95d4ce3 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php @@ -49,9 +49,12 @@ public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void $this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper; } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ArrayType::class; + return [ArrayType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/BooleanTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/BooleanTypeMapper.php index 2c380e04ab4..20b4c359686 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/BooleanTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/BooleanTypeMapper.php @@ -24,9 +24,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return BooleanType::class; + return [BooleanType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/CallableTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/CallableTypeMapper.php index ddb6b35e5f8..e953961ea88 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/CallableTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/CallableTypeMapper.php @@ -18,9 +18,12 @@ */ final class CallableTypeMapper implements TypeMapperInterface { - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return CallableType::class; + return [CallableType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ClassStringTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ClassStringTypeMapper.php index 2249fbe94e0..9c74e5ed733 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ClassStringTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ClassStringTypeMapper.php @@ -25,9 +25,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ClassStringType::class; + return [ClassStringType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php index a5f08c244ec..6aa1dfb52bd 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ClosureTypeMapper.php @@ -28,9 +28,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ClosureType::class; + return [ClosureType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeForParameterMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeForParameterMapper.php index bd7c0480cd9..ce6698893cb 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeForParameterMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeForParameterMapper.php @@ -25,9 +25,12 @@ public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void $this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper; } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ConditionalTypeForParameter::class; + return [ConditionalTypeForParameter::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeMapper.php index 27f9aeb08d3..72c567911b0 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ConditionalTypeMapper.php @@ -29,9 +29,12 @@ public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void $this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper; } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ConditionalType::class; + return [ConditionalType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php index 0d993ed34ea..d6628866064 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ConstantArrayTypeMapper.php @@ -23,9 +23,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ConstantArrayType::class; + return [ConstantArrayType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/FloatTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/FloatTypeMapper.php index a61fa0fd531..237f62c0520 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/FloatTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/FloatTypeMapper.php @@ -23,9 +23,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return FloatType::class; + return [FloatType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php index eb707476bc7..7d390862d95 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php @@ -20,9 +20,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return HasMethodType::class; + return [HasMethodType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php deleted file mode 100644 index c9a72e2a9ac..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetTypeMapper.php +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class HasOffsetTypeMapper implements TypeMapperInterface -{ - public function getNodeClass(): string - { - return HasOffsetType::class; - } - - /** - * @param HasOffsetType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param HasOffsetType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): Identifier - { - return new Identifier('array'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php deleted file mode 100644 index 500ccca6690..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/HasOffsetValueTypeTypeMapper.php +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class HasOffsetValueTypeTypeMapper implements TypeMapperInterface -{ - public function getNodeClass(): string - { - return HasOffsetValueType::class; - } - - /** - * @param HasOffsetValueType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param HasOffsetValueType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): Identifier - { - return new Identifier('array'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php index c22cb412218..01ffe7fa2ed 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php @@ -20,9 +20,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return HasPropertyType::class; + return [HasPropertyType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/IntegerTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/IntegerTypeMapper.php index 4762a6e4f41..9fa9664c17e 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/IntegerTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/IntegerTypeMapper.php @@ -23,9 +23,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return IntegerType::class; + return [IntegerType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php index b6143295d8e..8efe6d28bd9 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/IntersectionTypeMapper.php @@ -37,9 +37,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return IntersectionType::class; + return [IntersectionType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php index 9e810fd8067..27db268db96 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/IterableTypeMapper.php @@ -15,9 +15,12 @@ */ final class IterableTypeMapper implements TypeMapperInterface { - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return IterableType::class; + return [IterableType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/MixedTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/MixedTypeMapper.php index 9093fb84268..1c76ee66504 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/MixedTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/MixedTypeMapper.php @@ -24,9 +24,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return MixedType::class; + return [MixedType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/NeverTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/NeverTypeMapper.php index 3d825a4e25f..ea94feb2759 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/NeverTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/NeverTypeMapper.php @@ -15,9 +15,12 @@ */ final class NeverTypeMapper implements TypeMapperInterface { - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return NeverType::class; + return [NeverType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php deleted file mode 100644 index fd1ed0793d9..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/NonEmptyArrayTypeMapper.php +++ /dev/null @@ -1,38 +0,0 @@ - - */ -final class NonEmptyArrayTypeMapper implements TypeMapperInterface -{ - public function getNodeClass(): string - { - return NonEmptyArrayType::class; - } - - /** - * @param NonEmptyArrayType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param NonEmptyArrayType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): Identifier - { - return new Identifier('array'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/NullTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/NullTypeMapper.php index 7687a94ff4a..2f973b12b2b 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/NullTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/NullTypeMapper.php @@ -24,9 +24,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return NullType::class; + return [NullType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php index 78cecd0feb4..5ec5f035fdd 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php @@ -27,9 +27,12 @@ */ final class ObjectTypeMapper implements TypeMapperInterface { - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ObjectType::class; + return [ObjectType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectWithoutClassTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectWithoutClassTypeMapper.php index 479e70fd9be..98abef13aaf 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectWithoutClassTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectWithoutClassTypeMapper.php @@ -27,9 +27,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ObjectWithoutClassType::class; + return [ObjectWithoutClassType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php deleted file mode 100644 index 38b95b500ef..00000000000 --- a/src/PHPStanStaticTypeMapper/TypeMapper/OversizedArrayTypeMapper.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -final class OversizedArrayTypeMapper implements TypeMapperInterface -{ - public function getNodeClass(): string - { - return OversizedArrayType::class; - } - - /** - * @param OversizedArrayType $type - */ - public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode - { - return $type->toPhpDocNode(); - } - - /** - * @param TypeKind::* $typeKind - * @param OversizedArrayType $type - */ - public function mapToPhpParserNode(Type $type, string $typeKind): Identifier - { - return new Identifier('array'); - } -} diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php index 9ef8b7020f0..db27b9dd2b0 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ParentStaticTypeMapper.php @@ -16,9 +16,12 @@ */ final class ParentStaticTypeMapper implements TypeMapperInterface { - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ParentStaticType::class; + return [ParentStaticType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ResourceTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ResourceTypeMapper.php index 0b9620c4b4e..f2b2ebb6e95 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ResourceTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ResourceTypeMapper.php @@ -15,9 +15,12 @@ */ final class ResourceTypeMapper implements TypeMapperInterface { - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return ResourceType::class; + return [ResourceType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php index d59ee36e44a..270a5e06166 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php @@ -28,9 +28,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return StaticType::class; + return [StaticType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php index 71fc70c5bd5..ff5341535db 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php @@ -26,9 +26,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return StrictMixedType::class; + return [StrictMixedType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/StringTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/StringTypeMapper.php index fb67d91c126..5d2c11d4e4b 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/StringTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/StringTypeMapper.php @@ -23,9 +23,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return StringType::class; + return [StringType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/TypeWithClassNameTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/TypeWithClassNameTypeMapper.php index 73c9ff95e8f..264c09b6f5f 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/TypeWithClassNameTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/TypeWithClassNameTypeMapper.php @@ -23,9 +23,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return TypeWithClassName::class; + return [TypeWithClassName::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php index 3e40a981f7f..a610e4a441b 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php @@ -46,9 +46,12 @@ public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void $this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper; } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return UnionType::class; + return [UnionType::class]; } /** diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php index 649d40025b2..94f71a54fbf 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php @@ -26,9 +26,12 @@ public function __construct( ) { } - public function getNodeClass(): string + /** + * @return array> + */ + public function getNodeClasses(): array { - return VoidType::class; + return [VoidType::class]; } /** diff --git a/tests/PHPStanStaticTypeMapper/TypeMapperOrderTest.php b/tests/PHPStanStaticTypeMapper/TypeMapperOrderTest.php index 0b2b779aa0f..9d554611350 100644 --- a/tests/PHPStanStaticTypeMapper/TypeMapperOrderTest.php +++ b/tests/PHPStanStaticTypeMapper/TypeMapperOrderTest.php @@ -19,18 +19,20 @@ public function testChildTypeMapperIsRegisteredBeforeItsParent(): void $typeMappers = $this->resolveTypeMappers(); foreach ($typeMappers as $position => $typeMapper) { - $nodeClass = $typeMapper->getNodeClass(); - - for ($earlierPosition = 0; $earlierPosition < $position; ++$earlierPosition) { - $earlierNodeClass = $typeMappers[$earlierPosition]->getNodeClass(); - - $this->assertFalse(is_a($nodeClass, $earlierNodeClass, true), sprintf( - 'The "%s" is registered after "%s", but "%s" is a "%s". It can never be reached, register it earlier.', - $typeMapper::class, - $typeMappers[$earlierPosition]::class, - $nodeClass, - $earlierNodeClass - )); + foreach ($typeMapper->getNodeClasses() as $nodeClass) { + for ($earlierPosition = 0; $earlierPosition < $position; ++$earlierPosition) { + $earlierTypeMapper = $typeMappers[$earlierPosition]; + + foreach ($earlierTypeMapper->getNodeClasses() as $earlierNodeClass) { + $this->assertFalse(is_a($nodeClass, $earlierNodeClass, true), sprintf( + 'The "%s" is registered after "%s", but "%s" is a "%s". It can never be reached, register it earlier.', + $typeMapper::class, + $earlierTypeMapper::class, + $nodeClass, + $earlierNodeClass + )); + } + } } } }