Completed
Push — master ( d58982...9c12e5 )
by Thomas
03:58
created

CodeGeneratorConfig::setProfile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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