Completed
Push — master ( d67b8f...d63c9e )
by Thomas
02:04
created

CodeGeneratorConfig::setGenerateNullableTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\config;
5
6
use gossi\code\profiles\Profile;
7
use gossi\codegen\generator\CodeGenerator;
8
use phootwork\lang\Comparator;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
/**
13
 * Configuration for code generation
14
 *
15
 * @author Thomas Gossmann
16
 */
17
class CodeGeneratorConfig {
18
19
	protected $options;
20
21
	/** @var Profile */
22
	protected $profile;
23
24
	/**
25
	 * Creates a new configuration for code generator
26
	 *
27
	 * @see https://php-code-generator.readthedocs.org/en/latest/generator.html
28
	 * @param array $options
29
	 */
30 49
	public function __construct(array $options = []) {
31 49
		$resolver = new OptionsResolver();
32 49
		$this->configureOptions($resolver);
33 49
		$this->options = $resolver->resolve($options);
34 49
		$this->profile = is_string($this->options['profile']) ? new Profile($this->options['profile']) : $this->options['profile'];
35 49
	}
36
37 49
	protected function configureOptions(OptionsResolver $resolver): void {
38 49
		$resolver->setDefaults([
39 49
			'profile' => 'default',
40
			'generateDocblock' => true,
41
			'generateEmptyDocblock' => function (Options $options) {
42 47
				return $options['generateDocblock'];
43 49
			},
44
			'generateScalarTypeHints' => false,
45
			'generateReturnTypeHints' => false,
46
			'generateNullableTypes' => false,
47
			'enableFormatting' => false,
48
			'enableSorting' => true,
49 49
			'useStatementSorting' => CodeGenerator::SORT_USESTATEMENTS_DEFAULT,
50 49
			'constantSorting' => CodeGenerator::SORT_CONSTANTS_DEFAULT,
51 49
			'propertySorting' => CodeGenerator::SORT_PROPERTIES_DEFAULT,
52 49
			'methodSorting' => CodeGenerator::SORT_METHODS_DEFAULT
53
		]);
54
55 49
		$resolver->setAllowedTypes('profile', ['string', 'gossi\code\profiles\Profile']);
56 49
		$resolver->setAllowedTypes('generateDocblock', 'bool');
57 49
		$resolver->setAllowedTypes('generateEmptyDocblock', 'bool');
58 49
		$resolver->setAllowedTypes('generateScalarTypeHints', 'bool');
59 49
		$resolver->setAllowedTypes('generateReturnTypeHints', 'bool');
60 49
		$resolver->setAllowedTypes('generateNullableTypes', 'bool');
61 49
		$resolver->setAllowedTypes('enableFormatting', 'bool');
62 49
		$resolver->setAllowedTypes('enableSorting', 'bool');
63 49
		$resolver->setAllowedTypes('useStatementSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
64 49
		$resolver->setAllowedTypes('constantSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
65 49
		$resolver->setAllowedTypes('propertySorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
66 49
		$resolver->setAllowedTypes('methodSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
67 49
	}
68
69
	/**
70
	 * Returns the code style profile
71
	 *
72
	 * @return Profile
73
	 */
74 45
	public function getProfile(): Profile {
75 45
		return $this->profile;
76
	}
77
78
	/**
79
	 * Sets the code style profile
80
	 *
81
	 * @param Profile/string $profile
0 ignored issues
show
Documentation introduced by
The doc-type Profile/string could not be parsed: Unknown type name "Profile/string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
82
	 * @return $this
83
	 */
84 1
	public function setProfile($profile) {
85 1
		if (is_string($profile)) {
86 1
			$profile = new Profile($profile);
87
		}
88 1
		$this->profile = $profile;
89 1
		return $this;
90
	}
91
92
	/**
93
	 * Returns whether docblocks should be generated
94
	 *
95
	 * @return bool `true` if they will be generated and `false` if not
96
	 */
97 38
	public function getGenerateDocblock(): bool {
98 38
		return $this->options['generateDocblock'];
99
	}
100
101
	/**
102
	 * Sets whether docblocks should be generated
103
	 *
104
	 * @param bool $generate `true` if they will be generated and `false` if not
105
	 * @return $this
106
	 */
107 1
	public function setGenerateDocblock(bool $generate) {
108 1
		$this->options['generateDocblock'] = $generate;
109 1
		if (!$generate) {
110 1
			$this->options['generateEmptyDocblock'] = $generate;
111
		}
112 1
		return $this;
113
	}
114
115
	/**
116
	 * Returns whether empty docblocks are generated
117
	 *
118
	 * @return bool `true` if they will be generated and `false` if not
119
	 */
120 38
	public function getGenerateEmptyDocblock(): bool {
121 38
		return $this->options['generateEmptyDocblock'];
122
	}
123
124
	/**
125
	 * Sets whether empty docblocks are generated
126
	 *
127
	 * @param bool $generate `true` if they will be generated and `false` if not
128
	 * @return $this
129
	 */
130 1
	public function setGenerateEmptyDocblock(bool $generate) {
131 1
		$this->options['generateEmptyDocblock'] = $generate;
132 1
		if ($generate) {
133 1
			$this->options['generateDocblock'] = $generate;
134
		}
135 1
		return $this;
136
	}
137
138
	/**
139
	 * Returns whether scalar type hints will be generated for method parameters (PHP 7)
140
	 *
141
	 * @return bool `true` if they will be generated and `false` if not
142
	 */
143 15
	public function getGenerateScalarTypeHints(): bool {
144 15
		return $this->options['generateScalarTypeHints'];
145
	}
146
	
147
	
148
149
	/**
150
	 * Returns whether sorting is enabled
151
	 *
152
	 * @return bool `true` if it is enabled and `false` if not
153
	 */
154 18
	public function isSortingEnabled(): bool {
155 18
		return $this->options['enableSorting'];
156
	}
157
158
	/**
159
	 * Returns whether formatting is enalbed
160
	 *
161
	 * @return bool `true` if it is enabled and `false` if not
162
	 */
163 43
	public function isFormattingEnabled(): bool {
164 43
		return $this->options['enableFormatting'];
165
	}
166
167
	/**
168
	 * Returns the use statement sorting
169
	 *
170
	 * @return string|bool|Comparator|\Closure
171
	 */
172 18
	public function getUseStatementSorting() {
173 18
		return $this->options['useStatementSorting'];
174
	}
175
176
	/**
177
	 * Returns the constant sorting
178
	 *
179
	 * @return string|bool|Comparator|\Closure
180
	 */
181 17
	public function getConstantSorting() {
182 17
		return $this->options['constantSorting'];
183
	}
184
185
	/**
186
	 * Returns the property sorting
187
	 *
188
	 * @return string|bool|Comparator|\Closure
189
	 */
190 16
	public function getPropertySorting() {
191 16
		return $this->options['propertySorting'];
192
	}
193
194
	/**
195
	 * Returns the method sorting
196
	 *
197
	 * @return string|bool|Comparator|\Closure
198
	 */
199 18
	public function getMethodSorting() {
200 18
		return $this->options['methodSorting'];
201
	}
202
203
	/**
204
	 * Sets whether scalar type hints will be generated for method parameters (PHP 7)
205
	 *
206
	 * @param bool $generate `true` if they will be generated and `false` if not
207
	 * @return $this
208
	 */
209 1
	public function setGenerateScalarTypeHints(bool $generate) {
210 1
		$this->options['generateScalarTypeHints'] = $generate;
211 1
		return $this;
212
	}
213
214
	/**
215
	 * Returns whether return type hints will be generated for method parameters (PHP 7)
216
	 *
217
	 * @return bool `true` if they will be generated and `false` if not
218
	 */
219 20
	public function getGenerateReturnTypeHints(): bool {
220 20
		return $this->options['generateReturnTypeHints'];
221
	}
222
223
	/**
224
	 * Sets whether return type hints will be generated for method parameters (PHP 7)
225
	 *
226
	 * @param bool $generate `true` if they will be generated and `false` if not
227
	 * @return $this
228
	 */
229 1
	public function setGenerateReturnTypeHints(bool $generate) {
230 1
		$this->options['generateReturnTypeHints'] = $generate;
231 1
		return $this;
232
	}
233
	
234
	/**
235
	 * Returns whether return nullable type hints will be generated (PHP 7.3)
236
	 *
237
	 * @return bool `true` if they will be generated and `false` if not
238
	 */
239 23
	public function getGenerateNullableTypes(): bool {
240 23
	    return $this->options['generateNullableTypes'];
241
	}
242
	
243
	/**
244
	 * Sets whether return nullable type hints will be generated (PHP 7.3)
245
	 *
246
	 * @param bool $generate `true` if they will be generated and `false` if not
247
	 * @return $this
248
	 */
249 1
	public function setGenerateNullableTypes(bool $generate) {
250 1
	    $this->options['generateNullableTypes'] = $generate;
251 1
	    return $this;
252
	}
253
254
	/**
255
	 * Sets whether sorting is enabled
256
	 *
257
	 * @param $enabled bool `true` if it is enabled and `false` if not
258
	 * @return $this
259
	 */
260 1
	public function setSortingEnabled(bool $enabled) {
261 1
		$this->options['enableSorting'] = $enabled;
262 1
		return $this;
263
	}
264
265
	/**
266
	 * Sets whether formatting is enabled
267
	 *
268
	 * @param $enabled bool `true` if it is enabled and `false` if not
269
	 * @return $this
270
	 */
271 1
	public function setFormattingEnabled(bool $enabled) {
272 1
		$this->options['enableFormatting'] = $enabled;
273 1
		return $this;
274
	}
275
276
	/**
277
	 * Returns the use statement sorting
278
	 *
279
	 * @param $sorting string|bool|Comparator|\Closure
280
	 * @return $this
281
	 */
282 1
	public function setUseStatementSorting($sorting) {
283 1
		$this->options['useStatementSorting'] = $sorting;
284 1
		return $this;
285
	}
286
287
	/**
288
	 * Returns the constant sorting
289
	 *
290
	 * @param $sorting string|bool|Comparator|\Closure
291
	 * @return $this
292
	 */
293 1
	public function setConstantSorting($sorting) {
294 1
		$this->options['constantSorting'] = $sorting;
295 1
		return $this;
296
	}
297
298
	/**
299
	 * Returns the property sorting
300
	 *
301
	 * @param $sorting string|bool|Comparator|\Closure
302
	 * @return $this
303
	 */
304 1
	public function setPropertySorting($sorting) {
305 1
		$this->options['propertySorting'] = $sorting;
306 1
		return $this;
307
	}
308
309
	/**
310
	 * Returns the method sorting
311
	 *
312
	 * @param $sorting string|bool|Comparator|\Closure
313
	 * @return $this
314
	 */
315 1
	public function setMethodSorting($sorting) {
316 1
		$this->options['methodSorting'] = $sorting;
317 1
		return $this;
318
	}
319
}
320