20
20
use PhpParser \Node \Stmt ;
21
21
use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
22
22
23
+ use function AutoMapper \PhpParser \create_declare_item ;
24
+ use function AutoMapper \PhpParser \create_scalar_int ;
25
+
23
26
/**
24
27
* Generates code for a mapping class.
25
28
*
32
35
private MapperConstructorGenerator $ mapperConstructorGenerator ;
33
36
private InjectMapperMethodStatementsGenerator $ injectMapperMethodStatementsGenerator ;
34
37
private MapMethodStatementsGenerator $ mapMethodStatementsGenerator ;
38
+ private bool $ declareStrictTypes ;
35
39
private bool $ disableGeneratedMapper ;
36
40
37
41
public function __construct (
@@ -52,28 +56,38 @@ public function __construct(
52
56
53
57
$ this ->injectMapperMethodStatementsGenerator = new InjectMapperMethodStatementsGenerator ();
54
58
59
+ $ this ->declareStrictTypes = $ configuration ->strictTypes ;
55
60
$ this ->disableGeneratedMapper = !$ configuration ->autoRegister ;
56
61
}
57
62
58
63
/**
59
64
* Generate Class AST given metadata for a mapper.
60
65
*
66
+ * @return Stmt[]
67
+ *
61
68
* @throws CompileException
62
69
* @throws InvalidMappingException
63
70
*/
64
- public function generate (GeneratorMetadata $ metadata ): Stmt \ Class_
71
+ public function generate (GeneratorMetadata $ metadata ): array
65
72
{
66
73
if ($ this ->disableGeneratedMapper ) {
67
74
throw new InvalidMappingException ('No mapper found for source ' . $ metadata ->mapperMetadata ->source . ' and target ' . $ metadata ->mapperMetadata ->target );
68
75
}
69
76
70
- return (new Builder \Class_ ($ metadata ->mapperMetadata ->className ))
77
+ $ statements = [];
78
+ if ($ this ->declareStrictTypes ) {
79
+ // @phpstan-ignore argument.type
80
+ $ statements [] = new Stmt \Declare_ ([create_declare_item ('strict_types ' , create_scalar_int (1 ))]);
81
+ }
82
+ $ statements [] = (new Builder \Class_ ($ metadata ->mapperMetadata ->className ))
71
83
->makeFinal ()
72
84
->extend (GeneratedMapper::class)
73
85
->addStmt ($ this ->constructorMethod ($ metadata ))
74
86
->addStmt ($ this ->mapMethod ($ metadata ))
75
87
->addStmt ($ this ->registerMappersMethod ($ metadata ))
76
88
->getNode ();
89
+
90
+ return $ statements ;
77
91
}
78
92
79
93
/**
0 commit comments