File tree Expand file tree Collapse file tree 8 files changed +32
-4
lines changed Expand file tree Collapse file tree 8 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ public function __construct(
23
23
public ?bool $ checkAttributes = null ,
24
24
public ?ConstructorStrategy $ constructorStrategy = null ,
25
25
public ?bool $ allowReadOnlyTargetToPopulate = null ,
26
+ public ?bool $ strictTypes = null ,
26
27
public int $ priority = 0 ,
27
28
public ?string $ dateTimeFormat = null ,
28
29
) {
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ public function __construct(
22
22
public ?bool $ checkAttributes = null ,
23
23
public ?ConstructorStrategy $ constructorStrategy = null ,
24
24
public ?bool $ allowReadOnlyTargetToPopulate = null ,
25
+ public ?bool $ strictTypes = null ,
25
26
) {
26
27
}
27
28
}
Original file line number Diff line number Diff line change @@ -72,6 +72,7 @@ public function __invoke(GenerateMapperEvent $event): void
72
72
$ event ->checkAttributes ??= $ mapper ->checkAttributes ;
73
73
$ event ->constructorStrategy ??= $ mapper ->constructorStrategy ;
74
74
$ event ->allowReadOnlyTargetToPopulate ??= $ mapper ->allowReadOnlyTargetToPopulate ;
75
+ $ event ->strictTypes ??= $ mapper ->strictTypes ;
75
76
$ event ->mapperMetadata ->dateTimeFormat = $ mapper ->dateTimeFormat ;
76
77
}
77
78
}
Original file line number Diff line number Diff line change 35
35
private MapperConstructorGenerator $ mapperConstructorGenerator ;
36
36
private InjectMapperMethodStatementsGenerator $ injectMapperMethodStatementsGenerator ;
37
37
private MapMethodStatementsGenerator $ mapMethodStatementsGenerator ;
38
- private bool $ declareStrictTypes ;
39
38
private bool $ disableGeneratedMapper ;
40
39
41
40
public function __construct (
@@ -56,7 +55,6 @@ public function __construct(
56
55
57
56
$ this ->injectMapperMethodStatementsGenerator = new InjectMapperMethodStatementsGenerator ();
58
57
59
- $ this ->declareStrictTypes = $ configuration ->strictTypes ;
60
58
$ this ->disableGeneratedMapper = !$ configuration ->autoRegister ;
61
59
}
62
60
@@ -75,7 +73,7 @@ public function generate(GeneratorMetadata $metadata): array
75
73
}
76
74
77
75
$ statements = [];
78
- if ($ this -> declareStrictTypes ) {
76
+ if ($ metadata -> strictTypes ) {
79
77
// @phpstan-ignore argument.type
80
78
$ statements [] = new Stmt \Declare_ ([create_declare_item ('strict_types ' , create_scalar_int (1 ))]);
81
79
}
Original file line number Diff line number Diff line change @@ -24,7 +24,8 @@ public function __construct(
24
24
public readonly array $ propertiesMetadata ,
25
25
public readonly bool $ checkAttributes = true ,
26
26
public readonly ConstructorStrategy $ constructorStrategy = ConstructorStrategy::AUTO ,
27
- public bool $ allowReadOnlyTargetToPopulate = false ,
27
+ public readonly bool $ allowReadOnlyTargetToPopulate = false ,
28
+ public readonly bool $ strictTypes = false ,
28
29
public readonly ?string $ provider = null ,
29
30
) {
30
31
$ this ->variableRegistry = new VariableRegistry ();
Original file line number Diff line number Diff line change @@ -312,6 +312,7 @@ private function createGeneratorMetadata(MapperMetadata $mapperMetadata): Genera
312
312
$ mapperEvent ->checkAttributes ?? $ this ->configuration ->attributeChecking ,
313
313
$ mapperEvent ->constructorStrategy ?? $ this ->configuration ->constructorStrategy ,
314
314
$ mapperEvent ->allowReadOnlyTargetToPopulate ?? $ this ->configuration ->allowReadOnlyTargetToPopulate ,
315
+ $ mapperEvent ->strictTypes ?? $ this ->configuration ->strictTypes ,
315
316
$ mapperEvent ->provider ,
316
317
);
317
318
}
Original file line number Diff line number Diff line change @@ -794,6 +794,15 @@ public function testStrictTypes(): void
794
794
$ automapper ->map ($ data , Fixtures \IntDTO::class);
795
795
}
796
796
797
+ public function testStrictTypesFromMapper (): void
798
+ {
799
+ $ this ->expectException (\TypeError::class);
800
+
801
+ $ automapper = AutoMapper::create (new Configuration (strictTypes: false , classPrefix: 'StrictTypesFromMapper_ ' ));
802
+ $ data = ['foo ' => 1.1 ];
803
+ $ automapper ->map ($ data , Fixtures \IntDTOWithMapper::class);
804
+ }
805
+
797
806
public function testWithMixedArray (): void
798
807
{
799
808
$ user = new Fixtures \User (1 , 'yolo ' , '13 ' );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace AutoMapper \Tests \Fixtures ;
6
+
7
+ use AutoMapper \Attribute \Mapper ;
8
+
9
+ #[Mapper(strictTypes: true )]
10
+ readonly class IntDTOWithMapper
11
+ {
12
+ public function __construct (
13
+ public int $ foo ,
14
+ ) {
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments