1010
1111namespace OpenCodeModeling \CodeAst \Code ;
1212
13+ use OpenCodeModeling \CodeAst \Code \DocBlock \DocBlock ;
14+ use OpenCodeModeling \CodeAst \Code \DocBlock \Tag \VarTag ;
1315use PhpParser \Comment \Doc ;
1416use PhpParser \Node ;
1517use PhpParser \Node \Stmt \Property ;
@@ -49,6 +51,11 @@ final class PropertyGenerator extends AbstractMemberGenerator
4951 */
5052 private $ typeDocBlockHint ;
5153
54+ /**
55+ * @var DocBlock|null
56+ */
57+ private $ docBlock ;
58+
5259 public function __construct (
5360 string $ name = null ,
5461 string $ type = null ,
@@ -87,7 +94,7 @@ public function getType(): ?TypeGenerator
8794 return $ this ->type ;
8895 }
8996
90- public function docBlockComment (): ?string
97+ public function getDocBlockComment (): ?string
9198 {
9299 return $ this ->docBlockComment ;
93100 }
@@ -97,6 +104,16 @@ public function setDocBlockComment(?string $docBlockComment): void
97104 $ this ->docBlockComment = $ docBlockComment ;
98105 }
99106
107+ /**
108+ * Ignores generation of the doc block and uses provided doc block instead.
109+ *
110+ * @param DocBlock $docBlock
111+ */
112+ public function overrideDocBlock (DocBlock $ docBlock ): void
113+ {
114+ $ this ->docBlock = $ docBlock ;
115+ }
116+
100117 /**
101118 * @param ValueGenerator|mixed $defaultValue
102119 * @param string $defaultValueType
@@ -142,37 +159,6 @@ public function setTypeDocBlockHint(string $typeDocBlockHint): void
142159
143160 public function generate (): Property
144161 {
145- $ docBlockType = $ this ->type ->isNullable ()
146- ? $ this ->type ->type () . '|null '
147- : $ this ->type ->type ();
148-
149- if ($ typeHint = $ this ->getTypeDocBlockHint ()) {
150- $ docBlockType = $ typeHint ;
151- }
152-
153- $ propComment = <<<EOF
154- /**
155- * @var {$ docBlockType }
156- */
157- EOF ;
158- if ($ this ->docBlockComment ) {
159- $ multiLineDocBlockComment = \trim (\preg_replace ("/ \n/ " , "\n * " , $ this ->docBlockComment ));
160-
161- $ propComment = <<<EOF
162- /**
163- * {$ multiLineDocBlockComment }
164- *
165- * @var {$ docBlockType }
166- */
167- EOF ;
168- }
169-
170- $ attributes = [];
171-
172- if ($ this ->typed === false || $ this ->docBlockComment ) {
173- $ attributes = ['comments ' => [new Doc ($ propComment )]];
174- }
175-
176162 return new Property (
177163 $ this ->flags ,
178164 [
@@ -181,8 +167,30 @@ public function generate(): Property
181167 $ this ->defaultValue ? $ this ->defaultValue ->generate () : null
182168 ),
183169 ],
184- $ attributes ,
170+ $ this -> generateAttributes () ,
185171 $ this ->typed ? $ this ->type ->generate () : null
186172 );
187173 }
174+
175+ private function generateAttributes (): array
176+ {
177+ $ attributes = [];
178+
179+ if ($ this ->docBlock ) {
180+ return ['comments ' => [new Doc ($ this ->docBlock ->generate ())]];
181+ }
182+
183+ if ($ this ->typed === false || $ this ->docBlockComment ) {
184+ $ docBlockType = new VarTag ($ this ->type ->types ());
185+
186+ if ($ typeHint = $ this ->getTypeDocBlockHint ()) {
187+ $ docBlockType ->setTypes ($ typeHint );
188+ }
189+ $ docBlock = new DocBlock ($ this ->docBlockComment , $ docBlockType );
190+
191+ $ attributes = ['comments ' => [new Doc ($ docBlock ->generate ())]];
192+ }
193+
194+ return $ attributes ;
195+ }
188196}
0 commit comments