Completed
Pull Request — master (#41)
by
unknown
09:40
created

CodeGeneratorConfig::setPropertySorting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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