diff --git a/src/Attribute/Mapper.php b/src/Attribute/Mapper.php index 6899668..ec99e9d 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 b2e42e7..8f8df89 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 2987d44..d1dda52 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 84fed6c..91f2024 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 56ee9b5..f575d56 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 af9627f..9c21a1f 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 a3ee845..32606da 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 8817b18..565cb8d 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 0000000..bd716a1 --- /dev/null +++ b/tests/Fixtures/IntDTOWithMapper.php @@ -0,0 +1,16 @@ +