@@ -53,14 +53,15 @@ final class ClassPropertyBuilder
5353 */
5454 private ?DocBlock $ docBlock = null ;
5555
56+ private PropertyGenerator $ propertyGenerator ;
57+
5658 private function __construct ()
5759 {
5860 }
5961
6062 public static function fromNode (Node \Stmt \Property $ node , bool $ typed = true ): self
6163 {
6264 $ self = new self ();
63-
6465 $ type = null ;
6566
6667 switch (true ) {
@@ -76,10 +77,33 @@ public static function fromNode(Node\Stmt\Property $node, bool $typed = true): s
7677 }
7778
7879 $ self ->name = $ node ->props [0 ]->name ->name ;
79- $ self ->defaultValue = $ node ->props [0 ]->default ;
8080 $ self ->type = $ type ;
8181 $ self ->visibility = $ node ->flags ;
8282 $ self ->typed = $ typed ;
83+ $ self ->propertyGenerator = new PropertyGenerator ($ self ->name , $ self ->type );
84+
85+ $ defaultValue = $ node ->props [0 ]->default ;
86+
87+ switch (true ) {
88+ case $ defaultValue instanceof Node \Expr \ConstFetch:
89+ $ self ->defaultValue = $ defaultValue ->name ->toString ();
90+
91+ if ($ self ->defaultValue === 'null ' ) {
92+ $ self ->defaultValue = null ;
93+ }
94+ $ self ->propertyGenerator ->setDefaultValue ($ self ->defaultValue );
95+ break ;
96+ case $ defaultValue instanceof Node \Expr \ClassConstFetch:
97+ $ self ->defaultValue = $ defaultValue ->class ->toString () . ':: ' . $ defaultValue ->name ->toString ();
98+ $ self ->propertyGenerator ->setDefaultValue ($ self ->defaultValue );
99+ break ;
100+ default :
101+ if ($ defaultValue !== null ) {
102+ $ self ->defaultValue = $ defaultValue ;
103+ $ self ->propertyGenerator ->setDefaultValue ($ self ->defaultValue );
104+ }
105+ break ;
106+ }
83107
84108 $ comments = $ node ->getAttribute ('comments ' );
85109
@@ -105,6 +129,7 @@ public static function fromScratch(string $name, string $type, bool $typed = tru
105129 $ self ->type = $ type ;
106130 $ self ->typed = $ typed ;
107131 $ self ->visibility = ClassConstGenerator::FLAG_PRIVATE ;
132+ $ self ->propertyGenerator = new PropertyGenerator ($ self ->name , $ self ->type );
108133
109134 return $ self ;
110135 }
@@ -203,6 +228,20 @@ public function overrideDocBlock(?DocBlock $docBlock): self
203228 return $ this ;
204229 }
205230
231+ /**
232+ * @param mixed $defaultValue
233+ */
234+ public function setDefaultValue ($ defaultValue ): void
235+ {
236+ $ this ->defaultValue = $ defaultValue ;
237+ $ this ->propertyGenerator ->setDefaultValue ($ this ->defaultValue );
238+ }
239+
240+ public function getDefaultValue ()
241+ {
242+ return $ this ->defaultValue ;
243+ }
244+
206245 public function generate (): NodeVisitor
207246 {
208247 return new Property ($ this ->propertyGenerator ());
@@ -211,14 +250,12 @@ public function generate(): NodeVisitor
211250 private function propertyGenerator (): PropertyGenerator
212251 {
213252 $ flags = $ this ->visibility ;
253+ $ this ->propertyGenerator ->setFlags ($ flags );
254+ $ this ->propertyGenerator ->setDocBlockComment ($ this ->docBlockComment );
255+ $ this ->propertyGenerator ->setTypeDocBlockHint ($ this ->typeDocBlockHint );
256+ $ this ->propertyGenerator ->overrideDocBlock ($ this ->docBlock );
214257
215- $ propertyGenerator = new PropertyGenerator ($ this ->name , $ this ->type , $ this ->defaultValue , $ this ->typed , $ flags );
216-
217- $ propertyGenerator ->setDocBlockComment ($ this ->docBlockComment );
218- $ propertyGenerator ->setTypeDocBlockHint ($ this ->typeDocBlockHint );
219- $ propertyGenerator ->overrideDocBlock ($ this ->docBlock );
220-
221- return $ propertyGenerator ;
258+ return $ this ->propertyGenerator ;
222259 }
223260
224261 public function injectVisitors (NodeTraverser $ nodeTraverser ): void
0 commit comments