CodeGeneratorConfig::getPropertySorting()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
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
	 * Returns whether sorting is enabled
149
	 *
150
	 * @return bool `true` if it is enabled and `false` if not
151
	 */
152 18
	public function isSortingEnabled(): bool {
153 18
		return $this->options['enableSorting'];
154
	}
155
156
	/**
157
	 * Returns whether formatting is enalbed
158
	 *
159
	 * @return bool `true` if it is enabled and `false` if not
160
	 */
161 43
	public function isFormattingEnabled(): bool {
162 43
		return $this->options['enableFormatting'];
163
	}
164
165
	/**
166
	 * Returns the use statement sorting
167
	 *
168
	 * @return string|bool|Comparator|\Closure
169
	 */
170 18
	public function getUseStatementSorting() {
171 18
		return $this->options['useStatementSorting'];
172
	}
173
174
	/**
175
	 * Returns the constant sorting
176
	 *
177
	 * @return string|bool|Comparator|\Closure
178
	 */
179 17
	public function getConstantSorting() {
180 17
		return $this->options['constantSorting'];
181
	}
182
183
	/**
184
	 * Returns the property sorting
185
	 *
186
	 * @return string|bool|Comparator|\Closure
187
	 */
188 16
	public function getPropertySorting() {
189 16
		return $this->options['propertySorting'];
190
	}
191
192
	/**
193
	 * Returns the method sorting
194
	 *
195
	 * @return string|bool|Comparator|\Closure
196
	 */
197 18
	public function getMethodSorting() {
198 18
		return $this->options['methodSorting'];
199
	}
200
201
	/**
202
	 * Sets whether scalar type hints will be generated for method parameters (PHP 7)
203
	 *
204
	 * @param bool $generate `true` if they will be generated and `false` if not
205
	 * @return $this
206
	 */
207 1
	public function setGenerateScalarTypeHints(bool $generate) {
208 1
		$this->options['generateScalarTypeHints'] = $generate;
209 1
		return $this;
210
	}
211
212
	/**
213
	 * Returns whether return type hints will be generated for method parameters (PHP 7)
214
	 *
215
	 * @return bool `true` if they will be generated and `false` if not
216
	 */
217 20
	public function getGenerateReturnTypeHints(): bool {
218 20
		return $this->options['generateReturnTypeHints'];
219
	}
220
221
	/**
222
	 * Sets whether return type hints will be generated for method parameters (PHP 7)
223
	 *
224
	 * @param bool $generate `true` if they will be generated and `false` if not
225
	 * @return $this
226
	 */
227 1
	public function setGenerateReturnTypeHints(bool $generate) {
228 1
		$this->options['generateReturnTypeHints'] = $generate;
229 1
		return $this;
230
	}
231
232
	/**
233
	 * Returns whether return nullable type hints will be generated (PHP 7.3)
234
	 *
235
	 * @return bool `true` if they will be generated and `false` if not
236
	 */
237 23
	public function getGenerateNullableTypes(): bool {
238 23
		return $this->options['generateNullableTypes'];
239
	}
240
241
	/**
242
	 * Sets whether return nullable type hints will be generated (PHP 7.3)
243
	 *
244
	 * @param bool $generate `true` if they will be generated and `false` if not
245
	 * @return $this
246
	 */
247 1
	public function setGenerateNullableTypes(bool $generate) {
248 1
		$this->options['generateNullableTypes'] = $generate;
249 1
		return $this;
250
	}
251
252
	/**
253
	 * Sets whether sorting is enabled
254
	 *
255
	 * @param $enabled bool `true` if it is enabled and `false` if not
256
	 * @return $this
257
	 */
258 1
	public function setSortingEnabled(bool $enabled) {
259 1
		$this->options['enableSorting'] = $enabled;
260 1
		return $this;
261
	}
262
263
	/**
264
	 * Sets whether formatting is enabled
265
	 *
266
	 * @param $enabled bool `true` if it is enabled and `false` if not
267
	 * @return $this
268
	 */
269 1
	public function setFormattingEnabled(bool $enabled) {
270 1
		$this->options['enableFormatting'] = $enabled;
271 1
		return $this;
272
	}
273
274
	/**
275
	 * Returns the use statement sorting
276
	 *
277
	 * @param $sorting string|bool|Comparator|\Closure
278
	 * @return $this
279
	 */
280 1
	public function setUseStatementSorting($sorting) {
281 1
		$this->options['useStatementSorting'] = $sorting;
282 1
		return $this;
283
	}
284
285
	/**
286
	 * Returns the constant sorting
287
	 *
288
	 * @param $sorting string|bool|Comparator|\Closure
289
	 * @return $this
290
	 */
291 1
	public function setConstantSorting($sorting) {
292 1
		$this->options['constantSorting'] = $sorting;
293 1
		return $this;
294
	}
295
296
	/**
297
	 * Returns the property sorting
298
	 *
299
	 * @param $sorting string|bool|Comparator|\Closure
300
	 * @return $this
301
	 */
302 1
	public function setPropertySorting($sorting) {
303 1
		$this->options['propertySorting'] = $sorting;
304 1
		return $this;
305
	}
306
307
	/**
308
	 * Returns the method sorting
309
	 *
310
	 * @param $sorting string|bool|Comparator|\Closure
311
	 * @return $this
312
	 */
313 1
	public function setMethodSorting($sorting) {
314 1
		$this->options['methodSorting'] = $sorting;
315 1
		return $this;
316
	}
317
}
318