Completed
Pull Request — master (#41)
by
unknown
12:32 queued 06:48
created

CodeGeneratorConfig::isSortingEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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