@@ -21,10 +21,37 @@ class PropertyGenerator extends AbstractMemberGenerator
2121
2222 protected bool $ isConst = false ;
2323
24+ protected ?TypeGenerator $ type = null ;
25+
2426 protected ?PropertyValueGenerator $ defaultValue = null ;
2527
2628 private bool $ omitDefaultValue = false ;
2729
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+
2855 /** @return static */
2956 public static function fromReflection (PropertyReflection $ reflectionProperty )
3057 {
@@ -60,6 +87,11 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
6087 $ property ->setVisibility (self ::VISIBILITY_PUBLIC );
6188 }
6289
90+ $ property ->setType (TypeGenerator::fromReflectionType (
91+ $ reflectionProperty ->getType (),
92+ $ reflectionProperty ->getDeclaringClass ()
93+ ));
94+
6395 $ property ->setSourceDirty (false );
6496
6597 return $ property ;
@@ -68,7 +100,7 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
68100 /**
69101 * Generate from array
70102 *
71- * @configkey name string [required] Class Name
103+ * @configkey name string [required] Class Name
72104 * @configkey const bool
73105 * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
74106 * @configkey flags int
@@ -78,9 +110,10 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
78110 * @configkey visibility string
79111 * @configkey omitdefaultvalue bool
80112 * @configkey readonly bool
81- * @throws Exception\InvalidArgumentException
82- * @param array $array
113+ * @configkey type null|TypeGenerator
114+ * @param array $array
83115 * @return static
116+ * @throws Exception\InvalidArgumentException
84117 */
85118 public static function fromArray (array $ array )
86119 {
@@ -136,43 +169,38 @@ public static function fromArray(array $array)
136169
137170 $ property ->setReadonly ($ value );
138171 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 ;
139184 }
140185 }
141186
142187 return $ property ;
143188 }
144189
145190 /**
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
166192 * @return PropertyGenerator
167193 */
168194 public function setConst ($ const )
169195 {
170196 if (true === $ const ) {
171197 $ this ->setFlags (self ::FLAG_CONSTANT );
198+
172199 return $ this ;
173200 }
174201
175202 $ this ->removeFlag (self ::FLAG_CONSTANT );
203+
176204 return $ this ;
177205 }
178206
@@ -188,10 +216,12 @@ public function setReadonly(bool $readonly): self
188216 {
189217 if (true === $ readonly ) {
190218 $ this ->setFlags (self ::FLAG_READONLY );
219+
191220 return $ this ;
192221 }
193222
194223 $ this ->removeFlag (self ::FLAG_READONLY );
224+
195225 return $ this ;
196226 }
197227
@@ -221,9 +251,17 @@ public function setFlags($flags)
221251 }
222252
223253 /**
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
227265 * @return static
228266 */
229267 public function setDefaultValue (
@@ -241,17 +279,9 @@ public function setDefaultValue(
241279 }
242280
243281 /**
244- * @return ?PropertyValueGenerator
245- */
246- public function getDefaultValue ()
247- {
248- return $ this ->defaultValue ;
249- }
250-
251- /**
252- * @throws Exception\RuntimeException
253282 * @return string
254283 * @psalm-return non-empty-string
284+ * @throws Exception\RuntimeException
255285 */
256286 public function generate ()
257287 {
@@ -273,20 +303,23 @@ public function generate()
273303 $ this ->name
274304 ));
275305 }
306+
276307 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; ' );
283314 }
284315
316+ $ type = $ this ->type ;
285317 $ 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 ;
290323
291324 if ($ this ->omitDefaultValue ) {
292325 return $ output . '; ' ;
@@ -304,4 +337,14 @@ public function omitDefaultValue(bool $omit = true)
304337
305338 return $ this ;
306339 }
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+ }
307350}
0 commit comments