10
10
11
11
namespace OpenCodeModeling \CodeAst \Code ;
12
12
13
+ use OpenCodeModeling \CodeAst \Code \DocBlock \DocBlock ;
14
+ use OpenCodeModeling \CodeAst \Code \DocBlock \Tag \VarTag ;
13
15
use PhpParser \Comment \Doc ;
14
16
use PhpParser \Node ;
15
17
use PhpParser \Node \Stmt \Property ;
@@ -49,6 +51,11 @@ final class PropertyGenerator extends AbstractMemberGenerator
49
51
*/
50
52
private $ typeDocBlockHint ;
51
53
54
+ /**
55
+ * @var DocBlock|null
56
+ */
57
+ private $ docBlock ;
58
+
52
59
public function __construct (
53
60
string $ name = null ,
54
61
string $ type = null ,
@@ -87,7 +94,7 @@ public function getType(): ?TypeGenerator
87
94
return $ this ->type ;
88
95
}
89
96
90
- public function docBlockComment (): ?string
97
+ public function getDocBlockComment (): ?string
91
98
{
92
99
return $ this ->docBlockComment ;
93
100
}
@@ -97,6 +104,16 @@ public function setDocBlockComment(?string $docBlockComment): void
97
104
$ this ->docBlockComment = $ docBlockComment ;
98
105
}
99
106
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
+
100
117
/**
101
118
* @param ValueGenerator|mixed $defaultValue
102
119
* @param string $defaultValueType
@@ -142,37 +159,6 @@ public function setTypeDocBlockHint(string $typeDocBlockHint): void
142
159
143
160
public function generate (): Property
144
161
{
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
-
176
162
return new Property (
177
163
$ this ->flags ,
178
164
[
@@ -181,8 +167,30 @@ public function generate(): Property
181
167
$ this ->defaultValue ? $ this ->defaultValue ->generate () : null
182
168
),
183
169
],
184
- $ attributes ,
170
+ $ this -> generateAttributes () ,
185
171
$ this ->typed ? $ this ->type ->generate () : null
186
172
);
187
173
}
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
+ }
188
196
}
0 commit comments