From f698c8ed8f7617992057cb857fb715ebb328ab24 Mon Sep 17 00:00:00 2001 From: Ilya Orlov Date: Fri, 30 Aug 2024 19:14:29 +0500 Subject: [PATCH] feat: strictTypes configurable form Mapper attribute and true by default --- src/Attribute/Mapper.php | 1 + src/Configuration.php | 2 +- src/Event/GenerateMapperEvent.php | 1 + src/EventListener/MapperListener.php | 1 + src/Generator/MapperGenerator.php | 4 +--- src/Metadata/GeneratorMetadata.php | 3 ++- src/Metadata/MetadataFactory.php | 1 + tests/AutoMapperTest.php | 9 +++++++++ tests/Fixtures/IntDTOWithMapper.php | 16 ++++++++++++++++ 9 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 tests/Fixtures/IntDTOWithMapper.php diff --git a/src/Attribute/Mapper.php b/src/Attribute/Mapper.php index 6899668a..ec99e9d3 100644 --- a/src/Attribute/Mapper.php +++ b/src/Attribute/Mapper.php @@ -23,6 +23,7 @@ public function __construct( public ?bool $checkAttributes = null, public ?ConstructorStrategy $constructorStrategy = null, public ?bool $allowReadOnlyTargetToPopulate = null, + public ?bool $strictTypes = null, public int $priority = 0, public ?string $dateTimeFormat = null, ) { diff --git a/src/Configuration.php b/src/Configuration.php index b2e42e74..8f8df890 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -39,7 +39,7 @@ public function __construct( /** * Add declare(strict_types=1) to generated code. */ - public bool $strictTypes = false, + public bool $strictTypes = true, ) { } } diff --git a/src/Event/GenerateMapperEvent.php b/src/Event/GenerateMapperEvent.php index 2987d44c..d1dda52c 100644 --- a/src/Event/GenerateMapperEvent.php +++ b/src/Event/GenerateMapperEvent.php @@ -22,6 +22,7 @@ public function __construct( public ?bool $checkAttributes = null, public ?ConstructorStrategy $constructorStrategy = null, public ?bool $allowReadOnlyTargetToPopulate = null, + public ?bool $strictTypes = null, ) { } } diff --git a/src/EventListener/MapperListener.php b/src/EventListener/MapperListener.php index 84fed6cd..91f2024f 100644 --- a/src/EventListener/MapperListener.php +++ b/src/EventListener/MapperListener.php @@ -72,6 +72,7 @@ public function __invoke(GenerateMapperEvent $event): void $event->checkAttributes ??= $mapper->checkAttributes; $event->constructorStrategy ??= $mapper->constructorStrategy; $event->allowReadOnlyTargetToPopulate ??= $mapper->allowReadOnlyTargetToPopulate; + $event->strictTypes ??= $mapper->strictTypes; $event->mapperMetadata->dateTimeFormat = $mapper->dateTimeFormat; } } diff --git a/src/Generator/MapperGenerator.php b/src/Generator/MapperGenerator.php index 56ee9b57..f575d56f 100644 --- a/src/Generator/MapperGenerator.php +++ b/src/Generator/MapperGenerator.php @@ -35,7 +35,6 @@ private MapperConstructorGenerator $mapperConstructorGenerator; private InjectMapperMethodStatementsGenerator $injectMapperMethodStatementsGenerator; private MapMethodStatementsGenerator $mapMethodStatementsGenerator; - private bool $declareStrictTypes; private bool $disableGeneratedMapper; public function __construct( @@ -56,7 +55,6 @@ public function __construct( $this->injectMapperMethodStatementsGenerator = new InjectMapperMethodStatementsGenerator(); - $this->declareStrictTypes = $configuration->strictTypes; $this->disableGeneratedMapper = !$configuration->autoRegister; } @@ -75,7 +73,7 @@ public function generate(GeneratorMetadata $metadata): array } $statements = []; - if ($this->declareStrictTypes) { + if ($metadata->strictTypes) { // @phpstan-ignore argument.type $statements[] = new Stmt\Declare_([create_declare_item('strict_types', create_scalar_int(1))]); } diff --git a/src/Metadata/GeneratorMetadata.php b/src/Metadata/GeneratorMetadata.php index af9627fa..9c21a1f8 100644 --- a/src/Metadata/GeneratorMetadata.php +++ b/src/Metadata/GeneratorMetadata.php @@ -24,7 +24,8 @@ public function __construct( public readonly array $propertiesMetadata, public readonly bool $checkAttributes = true, public readonly ConstructorStrategy $constructorStrategy = ConstructorStrategy::AUTO, - public bool $allowReadOnlyTargetToPopulate = false, + public readonly bool $allowReadOnlyTargetToPopulate = false, + public readonly bool $strictTypes = true, public readonly ?string $provider = null, ) { $this->variableRegistry = new VariableRegistry(); diff --git a/src/Metadata/MetadataFactory.php b/src/Metadata/MetadataFactory.php index a3ee8450..32606da8 100644 --- a/src/Metadata/MetadataFactory.php +++ b/src/Metadata/MetadataFactory.php @@ -312,6 +312,7 @@ private function createGeneratorMetadata(MapperMetadata $mapperMetadata): Genera $mapperEvent->checkAttributes ?? $this->configuration->attributeChecking, $mapperEvent->constructorStrategy ?? $this->configuration->constructorStrategy, $mapperEvent->allowReadOnlyTargetToPopulate ?? $this->configuration->allowReadOnlyTargetToPopulate, + $mapperEvent->strictTypes ?? $this->configuration->strictTypes, $mapperEvent->provider, ); } diff --git a/tests/AutoMapperTest.php b/tests/AutoMapperTest.php index 8817b18f..565cb8d8 100644 --- a/tests/AutoMapperTest.php +++ b/tests/AutoMapperTest.php @@ -794,6 +794,15 @@ public function testStrictTypes(): void $automapper->map($data, Fixtures\IntDTO::class); } + public function testStrictTypesFromMapper(): void + { + $this->expectException(\TypeError::class); + + $automapper = AutoMapper::create(new Configuration(strictTypes: false, classPrefix: 'StrictTypesFromMapper_')); + $data = ['foo' => 1.1]; + $automapper->map($data, Fixtures\IntDTOWithMapper::class); + } + public function testWithMixedArray(): void { $user = new Fixtures\User(1, 'yolo', '13'); diff --git a/tests/Fixtures/IntDTOWithMapper.php b/tests/Fixtures/IntDTOWithMapper.php new file mode 100644 index 00000000..bd716a16 --- /dev/null +++ b/tests/Fixtures/IntDTOWithMapper.php @@ -0,0 +1,16 @@ +