@@ -21,10 +21,37 @@ class PropertyGenerator extends AbstractMemberGenerator
21
21
22
22
protected bool $ isConst = false ;
23
23
24
+ protected ?TypeGenerator $ type = null ;
25
+
24
26
protected ?PropertyValueGenerator $ defaultValue = null ;
25
27
26
28
private bool $ omitDefaultValue = false ;
27
29
30
+ /**
31
+ * @param PropertyValueGenerator|string|array|null $defaultValue
32
+ * @param int|int[] $flags
33
+ */
34
+ public function __construct (
35
+ ?string $ name = null ,
36
+ $ defaultValue = null ,
37
+ $ flags = self ::FLAG_PUBLIC ,
38
+ ?TypeGenerator $ type = null
39
+ ) {
40
+ parent ::__construct ();
41
+
42
+ if (null !== $ name ) {
43
+ $ this ->setName ($ name );
44
+ }
45
+ if (null !== $ defaultValue ) {
46
+ $ this ->setDefaultValue ($ defaultValue );
47
+ }
48
+ if ($ flags !== self ::FLAG_PUBLIC ) {
49
+ $ this ->setFlags ($ flags );
50
+ }
51
+
52
+ $ this ->type = $ type ;
53
+ }
54
+
28
55
/** @return static */
29
56
public static function fromReflection (PropertyReflection $ reflectionProperty )
30
57
{
@@ -60,6 +87,11 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
60
87
$ property ->setVisibility (self ::VISIBILITY_PUBLIC );
61
88
}
62
89
90
+ $ property ->setType (TypeGenerator::fromReflectionType (
91
+ $ reflectionProperty ->getType (),
92
+ $ reflectionProperty ->getDeclaringClass ()
93
+ ));
94
+
63
95
$ property ->setSourceDirty (false );
64
96
65
97
return $ property ;
@@ -68,7 +100,7 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
68
100
/**
69
101
* Generate from array
70
102
*
71
- * @configkey name string [required] Class Name
103
+ * @configkey name string [required] Class Name
72
104
* @configkey const bool
73
105
* @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
74
106
* @configkey flags int
@@ -78,9 +110,10 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
78
110
* @configkey visibility string
79
111
* @configkey omitdefaultvalue bool
80
112
* @configkey readonly bool
81
- * @throws Exception\InvalidArgumentException
82
- * @param array $array
113
+ * @configkey type null|TypeGenerator
114
+ * @param array $array
83
115
* @return static
116
+ * @throws Exception\InvalidArgumentException
84
117
*/
85
118
public static function fromArray (array $ array )
86
119
{
@@ -136,43 +169,38 @@ public static function fromArray(array $array)
136
169
137
170
$ property ->setReadonly ($ value );
138
171
break ;
172
+ case 'type ' :
173
+ if (! $ value instanceof TypeGenerator) {
174
+ throw new Exception \InvalidArgumentException (sprintf (
175
+ '%s is expecting %s on key %s. Got %s ' ,
176
+ __METHOD__ ,
177
+ TypeGenerator::class,
178
+ $ name ,
179
+ is_object ($ value ) ? get_class ($ value ) : gettype ($ value )
180
+ ));
181
+ }
182
+ $ property ->setType ($ value );
183
+ break ;
139
184
}
140
185
}
141
186
142
187
return $ property ;
143
188
}
144
189
145
190
/**
146
- * @param PropertyValueGenerator|string|array|null $defaultValue
147
- * @param int|int[] $flags
148
- */
149
- public function __construct (?string $ name = null , $ defaultValue = null , $ flags = self ::FLAG_PUBLIC )
150
- {
151
- parent ::__construct ();
152
-
153
- if (null !== $ name ) {
154
- $ this ->setName ($ name );
155
- }
156
- if (null !== $ defaultValue ) {
157
- $ this ->setDefaultValue ($ defaultValue );
158
- }
159
- if ($ flags !== self ::FLAG_PUBLIC ) {
160
- $ this ->setFlags ($ flags );
161
- }
162
- }
163
-
164
- /**
165
- * @param bool $const
191
+ * @param bool $const
166
192
* @return PropertyGenerator
167
193
*/
168
194
public function setConst ($ const )
169
195
{
170
196
if (true === $ const ) {
171
197
$ this ->setFlags (self ::FLAG_CONSTANT );
198
+
172
199
return $ this ;
173
200
}
174
201
175
202
$ this ->removeFlag (self ::FLAG_CONSTANT );
203
+
176
204
return $ this ;
177
205
}
178
206
@@ -188,10 +216,12 @@ public function setReadonly(bool $readonly): self
188
216
{
189
217
if (true === $ readonly ) {
190
218
$ this ->setFlags (self ::FLAG_READONLY );
219
+
191
220
return $ this ;
192
221
}
193
222
194
223
$ this ->removeFlag (self ::FLAG_READONLY );
224
+
195
225
return $ this ;
196
226
}
197
227
@@ -221,9 +251,17 @@ public function setFlags($flags)
221
251
}
222
252
223
253
/**
224
- * @param PropertyValueGenerator|mixed $defaultValue
225
- * @param string $defaultValueType
226
- * @param string $defaultValueOutputMode
254
+ * @return ?PropertyValueGenerator
255
+ */
256
+ public function getDefaultValue ()
257
+ {
258
+ return $ this ->defaultValue ;
259
+ }
260
+
261
+ /**
262
+ * @param PropertyValueGenerator|mixed $defaultValue
263
+ * @param string $defaultValueType
264
+ * @param string $defaultValueOutputMode
227
265
* @return static
228
266
*/
229
267
public function setDefaultValue (
@@ -241,17 +279,9 @@ public function setDefaultValue(
241
279
}
242
280
243
281
/**
244
- * @return ?PropertyValueGenerator
245
- */
246
- public function getDefaultValue ()
247
- {
248
- return $ this ->defaultValue ;
249
- }
250
-
251
- /**
252
- * @throws Exception\RuntimeException
253
282
* @return string
254
283
* @psalm-return non-empty-string
284
+ * @throws Exception\RuntimeException
255
285
*/
256
286
public function generate ()
257
287
{
@@ -273,20 +303,23 @@ public function generate()
273
303
$ this ->name
274
304
));
275
305
}
306
+
276
307
return $ output
277
- . $ this ->indentation
278
- . ($ this ->isFinal () ? 'final ' : '' )
279
- . $ this ->getVisibility ()
280
- . ' const '
281
- . $ name . ' = '
282
- . ($ defaultValue !== null ? $ defaultValue ->generate () : 'null; ' );
308
+ . $ this ->indentation
309
+ . ($ this ->isFinal () ? 'final ' : '' )
310
+ . $ this ->getVisibility ()
311
+ . ' const '
312
+ . $ name . ' = '
313
+ . ($ defaultValue !== null ? $ defaultValue ->generate () : 'null; ' );
283
314
}
284
315
316
+ $ type = $ this ->type ;
285
317
$ output .= $ this ->indentation
286
- . $ this ->getVisibility ()
287
- . ($ this ->isReadonly () ? ' readonly ' : '' )
288
- . ($ this ->isStatic () ? ' static ' : '' )
289
- . ' $ ' . $ name ;
318
+ . $ this ->getVisibility ()
319
+ . ($ this ->isReadonly () ? ' readonly ' : '' )
320
+ . ($ this ->isStatic () ? ' static ' : '' )
321
+ . ($ type ? ' ' . $ type ->generate () : '' )
322
+ . ' $ ' . $ name ;
290
323
291
324
if ($ this ->omitDefaultValue ) {
292
325
return $ output . '; ' ;
@@ -304,4 +337,14 @@ public function omitDefaultValue(bool $omit = true)
304
337
305
338
return $ this ;
306
339
}
340
+
341
+ public function getType (): ?TypeGenerator
342
+ {
343
+ return $ this ->type ;
344
+ }
345
+
346
+ public function setType (?TypeGenerator $ type ): void
347
+ {
348
+ $ this ->type = $ type ;
349
+ }
307
350
}
0 commit comments