-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCodeGeneratorConfig.php
More file actions
317 lines (285 loc) · 8.39 KB
/
CodeGeneratorConfig.php
File metadata and controls
317 lines (285 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
declare(strict_types=1);
namespace phpowermove\codegen\config;
use phpowermove\code\profiles\Profile;
use phpowermove\codegen\generator\CodeGenerator;
use phootwork\lang\Comparator;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Configuration for code generation
*
* @author Thomas Gossmann
*/
class CodeGeneratorConfig {
protected $options;
/** @var Profile */
protected $profile;
/**
* Creates a new configuration for code generator
*
* @see https://php-code-generator.readthedocs.org/en/latest/generator.html
* @param array $options
*/
public function __construct(array $options = []) {
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$this->options = $resolver->resolve($options);
$this->profile = is_string($this->options['profile']) ? new Profile($this->options['profile']) : $this->options['profile'];
}
protected function configureOptions(OptionsResolver $resolver): void {
$resolver->setDefaults([
'profile' => 'default',
'generateDocblock' => true,
'generateEmptyDocblock' => function (Options $options) {
return $options['generateDocblock'];
},
'generateScalarTypeHints' => false,
'generateReturnTypeHints' => false,
'generateNullableTypes' => false,
'enableFormatting' => false,
'enableSorting' => true,
'useStatementSorting' => CodeGenerator::SORT_USESTATEMENTS_DEFAULT,
'constantSorting' => CodeGenerator::SORT_CONSTANTS_DEFAULT,
'propertySorting' => CodeGenerator::SORT_PROPERTIES_DEFAULT,
'methodSorting' => CodeGenerator::SORT_METHODS_DEFAULT
]);
$resolver->setAllowedTypes('profile', ['string', 'phpowermove\code\profiles\Profile']);
$resolver->setAllowedTypes('generateDocblock', 'bool');
$resolver->setAllowedTypes('generateEmptyDocblock', 'bool');
$resolver->setAllowedTypes('generateScalarTypeHints', 'bool');
$resolver->setAllowedTypes('generateReturnTypeHints', 'bool');
$resolver->setAllowedTypes('generateNullableTypes', 'bool');
$resolver->setAllowedTypes('enableFormatting', 'bool');
$resolver->setAllowedTypes('enableSorting', 'bool');
$resolver->setAllowedTypes('useStatementSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
$resolver->setAllowedTypes('constantSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
$resolver->setAllowedTypes('propertySorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
$resolver->setAllowedTypes('methodSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
}
/**
* Returns the code style profile
*
* @return Profile
*/
public function getProfile(): Profile {
return $this->profile;
}
/**
* Sets the code style profile
*
* @param Profile|string $profile
* @return $this
*/
public function setProfile($profile) {
if (is_string($profile)) {
$profile = new Profile($profile);
}
$this->profile = $profile;
return $this;
}
/**
* Returns whether docblocks should be generated
*
* @return bool `true` if they will be generated and `false` if not
*/
public function getGenerateDocblock(): bool {
return $this->options['generateDocblock'];
}
/**
* Sets whether docblocks should be generated
*
* @param bool $generate `true` if they will be generated and `false` if not
* @return $this
*/
public function setGenerateDocblock(bool $generate) {
$this->options['generateDocblock'] = $generate;
if (!$generate) {
$this->options['generateEmptyDocblock'] = $generate;
}
return $this;
}
/**
* Returns whether empty docblocks are generated
*
* @return bool `true` if they will be generated and `false` if not
*/
public function getGenerateEmptyDocblock(): bool {
return $this->options['generateEmptyDocblock'];
}
/**
* Sets whether empty docblocks are generated
*
* @param bool $generate `true` if they will be generated and `false` if not
* @return $this
*/
public function setGenerateEmptyDocblock(bool $generate) {
$this->options['generateEmptyDocblock'] = $generate;
if ($generate) {
$this->options['generateDocblock'] = $generate;
}
return $this;
}
/**
* Returns whether scalar type hints will be generated for method parameters (PHP 7)
*
* @return bool `true` if they will be generated and `false` if not
*/
public function getGenerateScalarTypeHints(): bool {
return $this->options['generateScalarTypeHints'];
}
/**
* Returns whether sorting is enabled
*
* @return bool `true` if it is enabled and `false` if not
*/
public function isSortingEnabled(): bool {
return $this->options['enableSorting'];
}
/**
* Returns whether formatting is enalbed
*
* @return bool `true` if it is enabled and `false` if not
*/
public function isFormattingEnabled(): bool {
return $this->options['enableFormatting'];
}
/**
* Returns the use statement sorting
*
* @return string|bool|Comparator|\Closure
*/
public function getUseStatementSorting() {
return $this->options['useStatementSorting'];
}
/**
* Returns the constant sorting
*
* @return string|bool|Comparator|\Closure
*/
public function getConstantSorting() {
return $this->options['constantSorting'];
}
/**
* Returns the property sorting
*
* @return string|bool|Comparator|\Closure
*/
public function getPropertySorting() {
return $this->options['propertySorting'];
}
/**
* Returns the method sorting
*
* @return string|bool|Comparator|\Closure
*/
public function getMethodSorting() {
return $this->options['methodSorting'];
}
/**
* Sets whether scalar type hints will be generated for method parameters (PHP 7)
*
* @param bool $generate `true` if they will be generated and `false` if not
* @return $this
*/
public function setGenerateScalarTypeHints(bool $generate) {
$this->options['generateScalarTypeHints'] = $generate;
return $this;
}
/**
* Returns whether return type hints will be generated for method parameters (PHP 7)
*
* @return bool `true` if they will be generated and `false` if not
*/
public function getGenerateReturnTypeHints(): bool {
return $this->options['generateReturnTypeHints'];
}
/**
* Sets whether return type hints will be generated for method parameters (PHP 7)
*
* @param bool $generate `true` if they will be generated and `false` if not
* @return $this
*/
public function setGenerateReturnTypeHints(bool $generate) {
$this->options['generateReturnTypeHints'] = $generate;
return $this;
}
/**
* Returns whether return nullable type hints will be generated (PHP 7.3)
*
* @return bool `true` if they will be generated and `false` if not
*/
public function getGenerateNullableTypes(): bool {
return $this->options['generateNullableTypes'];
}
/**
* Sets whether return nullable type hints will be generated (PHP 7.3)
*
* @param bool $generate `true` if they will be generated and `false` if not
* @return $this
*/
public function setGenerateNullableTypes(bool $generate) {
$this->options['generateNullableTypes'] = $generate;
return $this;
}
/**
* Sets whether sorting is enabled
*
* @param $enabled bool `true` if it is enabled and `false` if not
* @return $this
*/
public function setSortingEnabled(bool $enabled) {
$this->options['enableSorting'] = $enabled;
return $this;
}
/**
* Sets whether formatting is enabled
*
* @param $enabled bool `true` if it is enabled and `false` if not
* @return $this
*/
public function setFormattingEnabled(bool $enabled) {
$this->options['enableFormatting'] = $enabled;
return $this;
}
/**
* Returns the use statement sorting
*
* @param $sorting string|bool|Comparator|\Closure
* @return $this
*/
public function setUseStatementSorting($sorting) {
$this->options['useStatementSorting'] = $sorting;
return $this;
}
/**
* Returns the constant sorting
*
* @param $sorting string|bool|Comparator|\Closure
* @return $this
*/
public function setConstantSorting($sorting) {
$this->options['constantSorting'] = $sorting;
return $this;
}
/**
* Returns the property sorting
*
* @param $sorting string|bool|Comparator|\Closure
* @return $this
*/
public function setPropertySorting($sorting) {
$this->options['propertySorting'] = $sorting;
return $this;
}
/**
* Returns the method sorting
*
* @param $sorting string|bool|Comparator|\Closure
* @return $this
*/
public function setMethodSorting($sorting) {
$this->options['methodSorting'] = $sorting;
return $this;
}
}